-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellSetup.sh
More file actions
executable file
·1921 lines (1658 loc) · 72.8 KB
/
shellSetup.sh
File metadata and controls
executable file
·1921 lines (1658 loc) · 72.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Cross-Platform Linux Setup Script (Stow, Zsh, OMZ, OMP, Tmux)
main() {
# 1. RECLAIM TTY: Prevent curl | bash from breaking interactive prompts
exec < /dev/tty
# --- COLORS & FORMATTING ---
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
msg_info() { echo -e "${BLUE}[*] $1${NC}"; }
msg_success() { echo -e "${GREEN}[+] $1${NC}"; }
msg_warn() { echo -e "${YELLOW}[!] $1${NC}"; }
msg_error() { echo -e "${RED}[x] $1${NC}"; }
msg_header() { echo -e "\n${CYAN}=== $1 ===${NC}"; }
# --- ASCII ART HEADER ---
clear
echo -e "${CYAN}"
cat << "EOF"
__ _ _ _ _
/ / (_)_ __ _ ___ | |_ ___(_) |_
/ / | | '_ \| | | \ \/ / '_ \ | __|
/ /___ | | | | | |_| |> <| |_) | | |_
\____/ |_|_| |_|\__,_/_/\_\ .__/|_|\__|
|_|
Created by Alex Ivantsov @Exploitacious
EOF
echo -e "${NC}"
if [ "$EUID" -eq 0 ]; then
msg_warn "Running as root."
echo
echo " This works on single-user / root-only systems (VMs, containers,"
echo " pen-testing rigs) but is NOT recommended on machines with regular"
echo " user accounts. As root, every package postinstall (pnpm/npm/curl|bash)"
echo " runs with full system privileges, and configs deploy to /root only."
echo
echo " If your machine has a standard user, exit and re-run as that user."
echo
msg_info "Continuing in 10s. Press 'q' or 'n' (then Enter) to abort, any other key to continue now."
local secs=10 reply=""
while [ "$secs" -gt 0 ]; do
printf "\r [%2ds] continuing as root... " "$secs"
if read -r -t 1 -n 1 reply 2>/dev/null; then
echo
case "$reply" in
q|Q|n|N) msg_error "Aborted by user."; exit 1 ;;
*) break ;;
esac
fi
secs=$((secs - 1))
done
echo
msg_success "Proceeding as root."
fi
# 2. CONTEXT AWARENESS & BOOTSTRAPPING
# If BASH_SOURCE is empty (piped) or doesn't have a .git folder, we are remote.
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$PWD}")" &> /dev/null && pwd)"
if [ ! -d "$REPO_DIR/.git" ]; then
msg_info "Remote execution detected. Bootstrapping environment..."
sudo -v
# Validate/Install Git
if ! command -v git &> /dev/null; then
msg_warn "Git missing. Installing..."
if [ -f /etc/debian_version ]; then
sudo apt-get update && sudo apt-get install -y git
elif [ -f /etc/arch-release ]; then
sudo pacman -S --noconfirm --needed git
elif [ -f /etc/redhat-release ]; then
sudo dnf install -y git
else
msg_error "Unsupported OS for automatic Git installation. Install Git manually."
exit 1
fi
fi
# Configure Global Git Identity
EXISTING_GIT_NAME=$(git config --global user.name 2>/dev/null)
EXISTING_GIT_EMAIL=$(git config --global user.email 2>/dev/null)
if [ -n "$EXISTING_GIT_NAME" ] && [ -n "$EXISTING_GIT_EMAIL" ]; then
msg_info "Git identity already configured: $EXISTING_GIT_NAME <$EXISTING_GIT_EMAIL>"
else
msg_header "Configuring Git Global Identity"
read -p "Enter Git User Name: " GIT_NAME
read -p "Enter Git Email: " GIT_EMAIL
if [ -z "$GIT_NAME" ] || [ -z "$GIT_EMAIL" ]; then
msg_warn "Git identity not set — you'll need to configure it before committing."
else
git config --global user.name "$GIT_NAME"
git config --global user.email "$GIT_EMAIL"
msg_success "Git identity set to $GIT_NAME <$GIT_EMAIL>"
fi
fi
# Clone Repository
TARGET_DIR="$HOME/linuxploitacious"
if [ ! -d "$TARGET_DIR" ]; then
msg_info "Cloning repository to $TARGET_DIR..."
git clone https://github.com/Exploitacious/linuxploitacious.git "$TARGET_DIR"
else
msg_warn "Directory $TARGET_DIR already exists. Pulling latest..."
cd "$TARGET_DIR" && git pull
fi
# 3. THE HANDOFF: Execute the local script and kill the remote stream
msg_info "Handoff to local repository execution..."
cd "$TARGET_DIR" || exit 1
chmod +x shellSetup.sh
exec ./shellSetup.sh
fi
# --- LOCAL EXECUTION CONTINUES HERE ---
sudo -v
# --- AUTO-SYNC WITH UPSTREAM ---
# Re-runs from inside an existing clone never hit the bootstrap pull above,
# so they used to drift behind origin. Sync now if the tree is clean.
if [ -d "$REPO_DIR/.git" ]; then
cd "$REPO_DIR" || exit 1
# Mark the repo safe even when invoked under an unexpected uid
git config --global --add safe.directory "$REPO_DIR" 2>/dev/null || true
if [ -n "$(git status --porcelain 2>/dev/null)" ]; then
msg_warn "Working tree has uncommitted changes — skipping auto-pull."
msg_warn "Commit/stash and re-run to sync with upstream."
else
msg_info "Syncing with upstream (git pull --ff-only)..."
if git pull --ff-only 2>&1; then
msg_success "Repository up to date."
else
msg_warn "Auto-pull failed (network, diverged history, or missing upstream). Continuing with local copy."
fi
fi
fi
# --- OS DETECTION ---
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_ID=$ID
OS_LIKE=$ID_LIKE
else
msg_error "Cannot detect Operating System."
exit 1
fi
# Verify whiptail
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
if ! command -v whiptail &> /dev/null; then sudo apt-get update && sudo apt-get install -y whiptail; fi
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
if ! command -v whiptail &> /dev/null; then sudo pacman -S --noconfirm --needed libnewt; fi
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
if ! command -v whiptail &> /dev/null; then sudo dnf install -y newt; fi
fi
# --- PACKAGE MANAGERS ---
install_nerd_fonts() {
local FONT_DIR="/usr/local/share/fonts/NerdFonts"
if [ -d "$FONT_DIR" ] && fc-list | grep -qi "Nerd Font"; then
msg_info "Nerd Fonts already installed. Skipping."
return 0
fi
msg_header "Installing Nerd Fonts (MesloLG - recommended for Oh My Posh)"
sudo mkdir -p "$FONT_DIR"
local MESLO_URL
MESLO_URL=$(curl -s https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest | jq -r '.assets[] | select(.name == "Meslo.zip") | .browser_download_url')
if [[ -z "$MESLO_URL" || "$MESLO_URL" == "null" ]]; then
msg_error "Could not find Meslo Nerd Font download URL."
return 1
fi
msg_info "Downloading MesloLG Nerd Font..."
if wget -q "$MESLO_URL" -O /tmp/meslo-nf.zip; then
sudo unzip -o /tmp/meslo-nf.zip -d "$FONT_DIR/" > /dev/null 2>&1
rm /tmp/meslo-nf.zip
sudo fc-cache -f
msg_success "MesloLG Nerd Font installed. Set your terminal font to 'MesloLGM Nerd Font' for full symbol support."
else
msg_error "Failed to download Meslo Nerd Font."
return 1
fi
}
install_debian_base() {
msg_header "Configuring Debian/Kali base"
sudo apt-get update
# Ensure software-properties-common is installed first
sudo apt-get install -y software-properties-common
msg_info "Upgrading system..."
sudo apt-get update && sudo apt-get full-upgrade -y
# Install base packages (stow, jq, etc.) first to ensure they exist for later steps
local DEBIAN_PKGS=(zsh stow git curl unzip tmux fzf gnupg2 xclip ffmpeg nmap build-essential wget jq btop tree)
if [[ "$OS_ID" == "kali" ]]; then DEBIAN_PKGS+=(kali-win-kex); fi
msg_info "Installing packages: ${DEBIAN_PKGS[*]}"
sudo apt-get install -y "${DEBIAN_PKGS[@]}"
# Attempt Fastfetch installation separately to avoid blocking other packages
if ! command -v fastfetch &> /dev/null; then
msg_info "Attempting to install Fastfetch..."
# 1. Try PPA for Ubuntu
if [[ "$OS_ID" == "ubuntu" ]]; then
msg_info "Trying Fastfetch PPA..."
if ! grep -rq "zhangsongcui3336/fastfetch" /etc/apt/sources.list.d/ 2>/dev/null; then
sudo add-apt-repository -y ppa:zhangsongcui3336/fastfetch 2>/dev/null && \
sudo apt-get update && sudo apt-get install -y fastfetch
else
sudo apt-get install -y fastfetch
fi
# 2. Try direct install (available in newer Debian/Kali)
else
sudo apt-get install -y fastfetch 2>/dev/null
fi
# 3. Manual Fallback using jq (which we just installed)
if ! command -v fastfetch &> /dev/null; then
msg_warn "Fastfetch not found in repos. Attempting robust manual install..."
local FASTFETCH_URL
FASTFETCH_URL=$(curl -s https://api.github.com/repos/fastfetch-cli/fastfetch/releases/latest | jq -r '.assets[] | select(.name | test("linux-amd64.deb")) | .browser_download_url' | head -n 1)
if [[ -n "$FASTFETCH_URL" && "$FASTFETCH_URL" != "null" ]]; then
msg_info "Downloading Fastfetch from: $FASTFETCH_URL"
if wget -q "$FASTFETCH_URL" -O /tmp/fastfetch.deb; then
sudo apt-get install -y /tmp/fastfetch.deb
rm /tmp/fastfetch.deb
msg_success "Fastfetch installed manually."
else
msg_error "Failed to download Fastfetch .deb"
fi
else
msg_error "Could not find Fastfetch download URL."
fi
fi
fi
# Install Nerd Fonts for Oh My Posh / terminal symbol support
install_nerd_fonts
msg_success "Base packages installed."
}
install_arch_base() {
msg_header "Configuring Arch base"
sudo pacman -Syu --noconfirm
local ARCH_PKGS=(zsh stow git curl wget unzip tmux fzf fastfetch gnupg2 xclip ffmpeg nmap base-devel jq btop tree)
msg_info "Installing packages: ${ARCH_PKGS[*]}"
sudo pacman -S --noconfirm --needed "${ARCH_PKGS[@]}"
if ! command -v yay &> /dev/null; then
msg_info "Installing yay..."
git clone https://aur.archlinux.org/yay.git /tmp/yay
cd /tmp/yay && makepkg -si --noconfirm
cd "$REPO_DIR" || exit 1
fi
# Install Nerd Fonts for Oh My Posh / terminal symbol support
install_nerd_fonts
msg_success "Base packages installed."
}
install_fedora_base() {
msg_header "Configuring Fedora/RHEL base"
sudo dnf upgrade -y
local FEDORA_PKGS=(zsh stow git curl unzip tmux fzf gnupg2 xclip nmap wget jq btop tree)
msg_info "Installing packages: ${FEDORA_PKGS[*]}"
sudo dnf install -y "${FEDORA_PKGS[@]}"
# Install Development Tools group (equivalent of build-essential)
msg_info "Installing Development Tools group..."
sudo dnf group install -y "Development Tools"
# FFmpeg: try ffmpeg-free (Fedora default repos) then ffmpeg (RPM Fusion)
if ! command -v ffmpeg &> /dev/null; then
sudo dnf install -y ffmpeg-free 2>/dev/null || sudo dnf install -y ffmpeg 2>/dev/null || \
msg_warn "FFmpeg unavailable in current repos. Enable RPM Fusion for full codec support."
fi
# Attempt Fastfetch installation separately
if ! command -v fastfetch &> /dev/null; then
msg_info "Attempting to install Fastfetch..."
sudo dnf install -y fastfetch 2>/dev/null
# Manual Fallback using jq (which we just installed)
if ! command -v fastfetch &> /dev/null; then
msg_warn "Fastfetch not found in repos. Attempting manual install..."
local FASTFETCH_URL
FASTFETCH_URL=$(curl -s https://api.github.com/repos/fastfetch-cli/fastfetch/releases/latest | jq -r '.assets[] | select(.name | test("linux-amd64\\.rpm$")) | .browser_download_url' | head -n 1)
if [[ -n "$FASTFETCH_URL" && "$FASTFETCH_URL" != "null" ]]; then
msg_info "Downloading Fastfetch from: $FASTFETCH_URL"
if wget -q "$FASTFETCH_URL" -O /tmp/fastfetch.rpm; then
sudo dnf install -y /tmp/fastfetch.rpm
rm /tmp/fastfetch.rpm
msg_success "Fastfetch installed manually."
else
msg_error "Failed to download Fastfetch .rpm"
fi
else
msg_error "Could not find Fastfetch download URL."
fi
fi
fi
# Install Nerd Fonts for Oh My Posh / terminal symbol support
install_nerd_fonts
msg_success "Base packages installed."
}
install_cloudflared() {
msg_header "Installing cloudflared (Cloudflare Tunnel + Access)"
if command -v cloudflared >/dev/null 2>&1; then
msg_info "cloudflared already installed: $(cloudflared --version 2>&1 | head -1)"
return 0
fi
local CF_ARCH="amd64"
case "$(uname -m)" in
aarch64|arm64) CF_ARCH="arm64" ;;
x86_64) CF_ARCH="amd64" ;;
esac
local CF_TMP
CF_TMP=$(mktemp -d)
msg_info "Downloading cloudflared-linux-${CF_ARCH} from GitHub releases..."
if ! curl -fsSL -o "$CF_TMP/cloudflared" \
"https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-${CF_ARCH}"; then
msg_error "cloudflared download failed."
rm -rf "$CF_TMP"
return 1
fi
sudo install -m 0755 "$CF_TMP/cloudflared" /usr/local/bin/cloudflared
rm -rf "$CF_TMP"
msg_success "cloudflared installed: $(cloudflared --version 2>&1 | head -1)"
}
install_brave() {
msg_header "Installing Brave Browser"
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
sudo apt-get install -y curl
sudo curl -fsSLo /usr/share/keyrings/brave-browser-archive-keyring.gpg https://brave-browser-apt-release.s3.brave.com/brave-browser-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/brave-browser-archive-keyring.gpg] https://brave-browser-apt-release.s3.brave.com/ stable main" | sudo tee /etc/apt/sources.list.d/brave-browser-release.list > /dev/null
sudo apt-get update && sudo apt-get install -y brave-browser
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
if ! command -v yay &> /dev/null; then
msg_error "yay not found. Please install yay first or run BASE option."
return 1
fi
yay -S --noconfirm brave-bin
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
sudo rpm --import https://brave-browser-rpm-release.s3.brave.com/brave-core.asc
cat << 'BRAVEREPO' | sudo tee /etc/yum.repos.d/brave-browser.repo > /dev/null
[brave-browser]
name=Brave Browser
baseurl=https://brave-browser-rpm-release.s3.brave.com/$basearch
enabled=1
gpgcheck=1
gpgkey=https://brave-browser-rpm-release.s3.brave.com/brave-core.asc
BRAVEREPO
sudo dnf install -y brave-browser
fi
msg_success "Brave Browser installed."
}
setup_root_profile() {
msg_header "Setting up Root Profile"
if [ "$EUID" -eq 0 ]; then
msg_error "This option must be run as a regular user, not as root."
return 1
fi
msg_info "Ensuring system packages are installed..."
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
sudo apt-get install -y btop tmux fzf zsh
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
sudo pacman -S --noconfirm --needed btop tmux fzf zsh
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
sudo dnf install -y btop tmux fzf zsh
fi
if ! grep -q "/usr/bin/zsh" /etc/shells; then
echo "/usr/bin/zsh" | sudo tee -a /etc/shells
fi
sudo chsh -s /usr/bin/zsh root
msg_success "Root shell set to zsh"
# Use `sudo test` — /root isn't readable as the primary user, so a bare
# `[ -d /root/... ]` always returns false and we end up trying to clone
# into a populated dir, which fails non-idempotently.
if ! sudo test -d "/root/.oh-my-zsh"; then
msg_info "Installing Oh My Zsh for root..."
sudo git clone https://github.com/ohmyzsh/ohmyzsh.git /root/.oh-my-zsh
fi
local ROOT_ZSH_CUSTOM="/root/.oh-my-zsh/custom"
declare -A ROOT_OMZ_PLUGINS=(
[zsh-autosuggestions]="https://github.com/zsh-users/zsh-autosuggestions"
[zsh-syntax-highlighting]="https://github.com/zsh-users/zsh-syntax-highlighting"
[zsh-completions]="https://github.com/zsh-users/zsh-completions"
[fzf-tab]="https://github.com/Aloxaf/fzf-tab"
)
for plugin in "${!ROOT_OMZ_PLUGINS[@]}"; do
if ! sudo test -d "$ROOT_ZSH_CUSTOM/plugins/$plugin"; then
msg_info "Installing OMZ plugin for root: $plugin"
sudo git clone --depth 1 "${ROOT_OMZ_PLUGINS[$plugin]}" "$ROOT_ZSH_CUSTOM/plugins/$plugin" -q
fi
done
if ! sudo test -f "/root/.local/bin/oh-my-posh"; then
msg_info "Installing Oh My Posh for root..."
sudo mkdir -p /root/.local/bin
sudo curl -s https://ohmyposh.dev/install.sh | sudo bash -s -- -d /root/.local/bin
fi
if ! sudo test -d "/root/.tmux/plugins/tpm"; then
msg_info "Installing Tmux Plugin Manager for root..."
sudo git clone https://github.com/tmux-plugins/tpm /root/.tmux/plugins/tpm
fi
# Ensure Nerd Fonts are available for root's OMP themes
install_nerd_fonts
msg_info "Deploying configs to /root via stow..."
cd "$REPO_DIR" || exit 1
# NOTE: claude/ is excluded — ROOT shares ~/.claude via symlink (AI_DIRS block below)
local PACKAGES=("fastfetch" "omp" "rustscan" "scripts" "tmux" "zsh" "bash" "btop")
local TS_ROOT
TS_ROOT="$(date +%Y%m%d_%H%M%S)"
for pkg in "${PACKAGES[@]}"; do
if [ -d "$pkg" ]; then
# Phase 1: drop any prior stow links
sudo stow -D -t /root "$pkg" 2>/dev/null || true
# Phase 2/3: back up real files in /root that would conflict with stow.
# /root/.bashrc ships as a real file from skel; without this, stow aborts.
while IFS= read -r src_file; do
local rel_path="${src_file#$pkg/}"
local dest_file="/root/$rel_path"
if sudo test -e "$dest_file" && ! sudo test -L "$dest_file"; then
local backup_file="${dest_file}.backup_${TS_ROOT}"
msg_warn "Backing up /root real file: $dest_file -> $backup_file"
sudo mv "$dest_file" "$backup_file"
fi
done < <(find "$pkg" -type f)
# Phase 4: ensure parent dirs exist (skip pkg-root)
while IFS= read -r src_dir; do
local rel_path="${src_dir#$pkg/}"
[ -n "$rel_path" ] && sudo mkdir -p "/root/$rel_path"
done < <(find "$pkg" -mindepth 1 -type d)
if sudo stow -v -t /root "$pkg" 2>&1; then
msg_success "Stowed to /root: $pkg"
else
msg_error "Failed to stow to /root: $pkg"
fi
fi
done
msg_info "Sharing AI tool data with root (symlinks + ACLs)..."
local PRIMARY_USER="$USER"
local PRIMARY_HOME="$HOME"
# Install ACL utilities for proper cross-user file access
if ! command -v setfacl &> /dev/null; then
msg_info "Installing ACL utilities..."
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
sudo apt-get install -y acl
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
sudo pacman -S --noconfirm acl
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
sudo dnf install -y acl
fi
fi
local AI_DIRS=(".claude" ".local/share/opencode")
local AI_FILES=(".claude.json")
# Install Node/pnpm and AI CLIs for root BEFORE symlinking AI_DIRS.
# Installers may stat/clobber their own config dirs; doing this first keeps
# them away from the user's live state, and the AI_DIRS merge step below
# will fold any data they wrote into the primary user's home.
if ! sudo test -d "/root/.nvm"; then
msg_info "Installing NVM for root..."
sudo -i bash -c 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash'
fi
msg_info "Installing Node.js and pnpm for root..."
sudo -i bash << 'ROOTNVM'
export NVM_DIR="/root/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install --lts
nvm use --lts
corepack enable pnpm
ROOTNVM
if ! sudo test -d "/root/.pyenv"; then
msg_info "Installing pyenv for root..."
sudo -i bash -c 'curl -fsSL https://pyenv.run | bash'
else
msg_info "pyenv already installed for root; skipping."
fi
msg_info "Installing Python for root..."
sudo -i bash << 'ROOTPY'
export PYENV_ROOT="/root/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
LATEST_PY=$(pyenv install --list | grep -E '^\s+3\.[0-9]+\.[0-9]+$' | tail -1 | tr -d ' ')
if ! pyenv versions --bare | grep -qF "$LATEST_PY"; then
pyenv install "$LATEST_PY"
fi
pyenv global "$LATEST_PY"
# pip-as-root is fine inside a pyenv prefix (per-version, not system-wide);
# silence the standard warning.
pip install --upgrade --root-user-action ignore pip setuptools wheel
ROOTPY
msg_info "Installing AI CLIs for root (Claude Code, OpenCode)..."
sudo -i bash << 'ROOTAI'
export NVM_DIR="/root/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
export PNPM_HOME="/root/.local/share/pnpm"
# Prepend ~/.local/bin so the Claude installer's post-install PATH check passes
# (it warns when its install dir is missing from PATH).
export PATH="$HOME/.local/bin:$PNPM_HOME:$PATH"
if command -v pnpm &> /dev/null; then
pnpm remove -g @anthropic-ai/claude-code opencode-ai @google/gemini-cli >/dev/null 2>&1 || true
fi
if command -v npm &> /dev/null; then
npm uninstall -g @anthropic-ai/claude-code opencode-ai @google/gemini-cli >/dev/null 2>&1 || true
fi
for shim in /root/.local/share/pnpm/claude /root/.local/share/pnpm/opencode /root/.local/share/pnpm/gemini; do
if [ -e "$shim" ] || [ -L "$shim" ]; then
rm -f "$shim"
fi
done
if [ -d /root/.nvm/versions/node ]; then
find /root/.nvm/versions/node -maxdepth 3 \( -name claude -o -name opencode -o -name gemini \) -delete 2>/dev/null || true
fi
curl -fsSL https://claude.ai/install.sh | bash
curl -fsSL https://opencode.ai/install | bash
ROOTAI
if [ -L /root/.gemini ] || [ -e /root/.gemini ]; then
msg_warn "Removing deprecated /root/.gemini..."
sudo rm -rf /root/.gemini
fi
for dir in "${AI_DIRS[@]}"; do
local SRC="$PRIMARY_HOME/$dir"
local DST="/root/$dir"
# Skip if source doesn't exist (tool not installed yet)
if [ ! -d "$SRC" ]; then
msg_info "Skipping $dir (not present in $PRIMARY_HOME)"
continue
fi
# Already a correct symlink — just refresh ACLs
if [ -L "$DST" ] && [ "$(readlink -f "$DST")" = "$(readlink -f "$SRC")" ]; then
msg_info "$DST already linked correctly"
else
# Merge any unique data from root's copy into user's copy before replacing
if [ -d "$DST" ] && [ ! -L "$DST" ]; then
msg_info "Merging root's $dir data into $SRC before linking..."
sudo cp -rn "$DST"/* "$SRC"/ 2>/dev/null || true
sudo mv "$DST" "${DST}.bak.$(date +%Y%m%d_%H%M%S)"
msg_warn "Root's original $dir backed up to ${DST}.bak.*"
else
sudo rm -rf "$DST"
fi
sudo mkdir -p "$(dirname "$DST")"
sudo ln -s "$SRC" "$DST"
msg_success "Linked: $DST -> $SRC"
fi
# Fix ownership and set ACLs for seamless cross-user access
sudo chown -R "$PRIMARY_USER:$PRIMARY_USER" "$SRC"
if command -v setfacl &> /dev/null; then
sudo setfacl -R -m u:root:rwX "$SRC"
sudo setfacl -R -d -m u:root:rwX "$SRC"
sudo setfacl -R -m u:"$PRIMARY_USER":rwX "$SRC"
sudo setfacl -R -d -m u:"$PRIMARY_USER":rwX "$SRC"
msg_success "ACLs set on $SRC (both root and $PRIMARY_USER have full access)"
else
sudo chmod -R g+rw "$SRC"
msg_warn "setfacl not available — falling back to group permissions on $SRC"
fi
done
for file in "${AI_FILES[@]}"; do
local SRC="$PRIMARY_HOME/$file"
local DST="/root/$file"
if [ -f "$SRC" ] || [ -L "$SRC" ]; then
if [ -L "$DST" ] && [ "$(readlink -f "$DST")" = "$(readlink -f "$SRC")" ]; then
msg_info "$DST already linked correctly"
else
# Back up root's copy if it's a real file
if [ -f "$DST" ] && [ ! -L "$DST" ]; then
sudo mv "$DST" "${DST}.bak.$(date +%Y%m%d_%H%M%S)"
else
sudo rm -f "$DST"
fi
sudo ln -s "$SRC" "$DST"
msg_success "Linked: $DST -> $SRC"
fi
sudo chown "$PRIMARY_USER:$PRIMARY_USER" "$SRC"
if command -v setfacl &> /dev/null; then
sudo setfacl -m u:root:rw "$SRC"
sudo setfacl -m u:"$PRIMARY_USER":rw "$SRC"
fi
fi
done
msg_success "Root profile setup complete. Run 'sudo -i' to use."
}
setup_swapfile() {
msg_header "Setting up Swapfile"
if [ -n "$(swapon --show=NAME --noheadings 2>/dev/null)" ]; then
msg_info "Swap already active:"
swapon --show
return 0
fi
# Size = round(RAM in GB), min 2, max 32
local RAM_GB
RAM_GB=$(awk '/MemTotal/ {printf "%d", ($2/1024/1024)+0.5}' /proc/meminfo)
[ "$RAM_GB" -lt 2 ] && RAM_GB=2
[ "$RAM_GB" -gt 32 ] && RAM_GB=32
local SWAPFILE=/swapfile
if sudo test -e "$SWAPFILE"; then
msg_warn "$SWAPFILE exists but is not active. Investigate manually; not touching it."
return 1
fi
# Check free space — need RAM_GB + 1GB headroom on /
local FREE_GB
FREE_GB=$(df -BG --output=avail / | tail -1 | tr -dc '0-9')
if [ "$FREE_GB" -lt $((RAM_GB + 1)) ]; then
msg_error "Not enough free space on / (need ${RAM_GB}G + 1G headroom, have ${FREE_GB}G). Skipping."
return 1
fi
# Detect FS type — btrfs needs special handling (chattr +C, no holes)
local FS_TYPE
FS_TYPE=$(df -T / | awk 'NR==2 {print $2}')
if [ "$FS_TYPE" = "btrfs" ]; then
msg_warn "Root is btrfs — fallocate creates holes; using btrfs-safe path."
sudo touch "$SWAPFILE"
sudo chattr +C "$SWAPFILE" 2>/dev/null || true
sudo dd if=/dev/zero of="$SWAPFILE" bs=1M count=$((RAM_GB*1024)) status=progress
else
msg_info "Allocating ${RAM_GB}G swapfile at $SWAPFILE (fs: $FS_TYPE)..."
if ! sudo fallocate -l "${RAM_GB}G" "$SWAPFILE" 2>/dev/null; then
msg_warn "fallocate failed, falling back to dd..."
sudo dd if=/dev/zero of="$SWAPFILE" bs=1M count=$((RAM_GB*1024)) status=progress
fi
fi
sudo chmod 600 "$SWAPFILE"
sudo mkswap "$SWAPFILE"
sudo swapon "$SWAPFILE"
if ! grep -qE "^${SWAPFILE}[[:space:]]" /etc/fstab; then
echo "${SWAPFILE} none swap sw 0 0" | sudo tee -a /etc/fstab > /dev/null
msg_success "Added swapfile entry to /etc/fstab."
fi
# Tune swappiness — low so swap is emergency-only on a workstation/VM
if [ ! -f /etc/sysctl.d/99-swappiness.conf ]; then
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf > /dev/null
sudo sysctl -q --system
msg_success "vm.swappiness=10 (swap reserved for memory pressure)."
fi
msg_success "Swap active: $(swapon --show=NAME,SIZE --noheadings | head -1)"
}
install_docker() {
msg_header "Installing Docker Engine"
if command -v docker &> /dev/null && docker --version &> /dev/null; then
msg_info "Docker already installed: $(docker --version)"
else
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
msg_info "Installing Docker via official APT repo..."
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
# Kali ships with Debian-derived sources; force docker repo to match Debian codename
local DOCKER_CODENAME="$VERSION_CODENAME"
if [[ "$OS_ID" == "kali" ]] || [ -z "$DOCKER_CODENAME" ]; then
DOCKER_CODENAME="bookworm"
fi
local DOCKER_OS="debian"
if [[ "$OS_ID" == "ubuntu" ]]; then DOCKER_OS="ubuntu"; fi
curl -fsSL "https://download.docker.com/linux/${DOCKER_OS}/gpg" | \
sudo gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/${DOCKER_OS} ${DOCKER_CODENAME} stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
sudo pacman -S --noconfirm --needed docker docker-compose
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
else
msg_error "Unsupported OS for Docker auto-install."
return 1
fi
msg_success "Docker installed: $(docker --version)"
fi
# Enable + start daemon
if command -v systemctl &> /dev/null; then
sudo systemctl enable --now docker.service containerd.service 2>/dev/null || true
fi
# Add current user to docker group (skip if root)
if [ "$EUID" -ne 0 ]; then
local TARGET_USER="${SUDO_USER:-$USER}"
if ! id -nG "$TARGET_USER" | tr ' ' '\n' | grep -qx docker; then
sudo usermod -aG docker "$TARGET_USER"
msg_warn "Added $TARGET_USER to 'docker' group. Log out + back in (or 'newgrp docker') for it to take effect."
else
msg_info "$TARGET_USER already in docker group."
fi
fi
# Smoke test (won't fail script if daemon isn't ready yet)
if sudo docker run --rm hello-world &> /dev/null; then
msg_success "Docker smoke test passed."
else
msg_warn "Docker smoke test skipped or failed — daemon may need a moment."
fi
}
setup_tmux_persistence() {
msg_header "Setting up Tmux Persistence (auto-attach + systemd boot)"
if ! command -v tmux &> /dev/null; then
msg_info "Installing tmux..."
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
sudo apt-get install -y tmux
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
sudo pacman -S --noconfirm --needed tmux
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
sudo dnf install -y tmux
fi
fi
# The .zshrc/.bashrc shipped by this repo already contains the SSH auto-attach snippet,
# so re-stow is enough to wire it up. Just inform the user.
msg_info "SSH auto-attach snippet ships in stowed .zshrc/.bashrc (deploys via STOW step)."
if [ "$EUID" -eq 0 ]; then
msg_warn "Running as root — skipping systemd --user unit setup."
return 0
fi
# systemd user unit so the 'main' session survives reboots
local UNIT_DIR="$HOME/.config/systemd/user"
local UNIT_FILE="$UNIT_DIR/tmux-main.service"
mkdir -p "$UNIT_DIR"
local TMUX_BIN
TMUX_BIN="$(command -v tmux)"
cat > "$UNIT_FILE" <<EOF
[Unit]
Description=tmux default session (main)
Documentation=man:tmux(1)
After=network.target
[Service]
Type=forking
ExecStart=${TMUX_BIN} new-session -d -s main
ExecStop=${TMUX_BIN} kill-session -t main
KillMode=none
Restart=on-failure
[Install]
WantedBy=default.target
EOF
msg_success "Wrote $UNIT_FILE"
systemctl --user daemon-reload
systemctl --user enable --now tmux-main.service && \
msg_success "tmux-main.service enabled + started" || \
msg_warn "Failed to enable tmux-main.service (check 'systemctl --user status tmux-main')"
# Enable linger so user services run without an active login (survive reboots)
local TARGET_USER="${SUDO_USER:-$USER}"
if loginctl show-user "$TARGET_USER" 2>/dev/null | grep -q "Linger=yes"; then
msg_info "Linger already enabled for $TARGET_USER."
else
sudo loginctl enable-linger "$TARGET_USER" && \
msg_success "Linger enabled for $TARGET_USER (user services run at boot)." || \
msg_warn "Failed to enable linger."
fi
msg_info "Usage:"
echo " SSH login: session picker shows on connect (attach existing / new / plain shell)"
echo " Detach: Ctrl-a d (keeps session alive — prefix is Ctrl-a)"
echo " Switch: Ctrl-a s (visual session picker inside tmux)"
echo " List: tmux ls"
echo " Attach: tmux a -t <name>"
echo " New session: tmux new -s <name>"
echo " Kill session: tmux kill-session -t <name>"
}
configure_passwordless_sudo() {
msg_header "Configuring Passwordless Sudo"
if [ "$EUID" -eq 0 ]; then
msg_warn "Running as root — passwordless sudo not applicable. Skipping."
return 0
fi
if ! command -v sudo &> /dev/null; then
msg_error "sudo not installed. Skipping."
return 1
fi
local TARGET_USER="${SUDO_USER:-$USER}"
local SUDOERS_FILE="/etc/sudoers.d/90-${TARGET_USER}-nopasswd"
local SUDOERS_LINE="${TARGET_USER} ALL=(ALL:ALL) NOPASSWD:ALL"
if sudo test -f "$SUDOERS_FILE" && sudo grep -qxF "$SUDOERS_LINE" "$SUDOERS_FILE"; then
msg_info "Passwordless sudo already configured for ${TARGET_USER}."
return 0
fi
msg_warn "About to grant ${TARGET_USER} passwordless sudo (NOPASSWD: ALL)."
msg_warn "This means anyone with shell access as ${TARGET_USER} gets full root."
echo -ne "${YELLOW}Proceed? [y/N]: ${NC}"
read -r REPLY
if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then
msg_info "Skipping passwordless sudo."
return 0
fi
# Write via a tmpfile + visudo -cf so we never install a broken sudoers entry
local TMP_SUDOERS
TMP_SUDOERS="$(mktemp)"
printf '%s\n' "$SUDOERS_LINE" > "$TMP_SUDOERS"
if sudo visudo -cf "$TMP_SUDOERS" > /dev/null; then
sudo install -m 0440 -o root -g root "$TMP_SUDOERS" "$SUDOERS_FILE"
rm -f "$TMP_SUDOERS"
msg_success "Passwordless sudo enabled for ${TARGET_USER} (${SUDOERS_FILE})."
else
rm -f "$TMP_SUDOERS"
msg_error "visudo validation failed. Sudoers not modified."
return 1
fi
}
install_github_cli() {
msg_header "Installing GitHub CLI"
if command -v gh &> /dev/null; then
msg_info "GitHub CLI already installed: $(gh --version | head -1)"
return 0
fi
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
msg_info "Adding GitHub CLI repository..."
sudo mkdir -p -m 755 /etc/apt/keyrings
wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt-get update
sudo apt-get install -y gh
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
sudo pacman -S --noconfirm --needed github-cli
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
sudo dnf install -y gh
else
msg_error "Unsupported OS for GitHub CLI auto-install"
return 1
fi
if command -v gh &> /dev/null; then
msg_success "GitHub CLI installed: $(gh --version | head -1)"
else
msg_error "GitHub CLI installation failed"
return 1
fi
}
setup_github_ssh() {
msg_header "Setting up GitHub SSH Key"
local SSH_KEY="$HOME/.ssh/id_ed25519"
local GIT_EMAIL=$(git config --global user.email 2>/dev/null)
GIT_EMAIL=${GIT_EMAIL:-"email@example.com"}
local NEED_KEY_UPLOAD=true
if [ -f "$SSH_KEY" ]; then
msg_info "SSH key already exists at $SSH_KEY"
echo -ne "${YELLOW}Is this key already added to your GitHub account? [y/N]: ${NC}"
read -r REPLY
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
NEED_KEY_UPLOAD=false
msg_info "Skipping SSH key upload"
fi
else
msg_info "Generating new ed25519 SSH key..."
mkdir -p "$HOME/.ssh"
ssh-keygen -t ed25519 -C "$GIT_EMAIL" -f "$SSH_KEY" -N ""
msg_success "SSH key generated"
fi
msg_info "Configuring SSH for GitHub..."
mkdir -p "$HOME/.ssh"
if ! grep -q "Host \*" "$HOME/.ssh/config" 2>/dev/null; then
cat >> "$HOME/.ssh/config" << 'EOF'
Host *
AddKeysToAgent yes
ServerAliveInterval 60
ServerAliveCountMax 3
EOF
msg_success "SSH global defaults added"
fi
if ! grep -q "Host github.com" "$HOME/.ssh/config" 2>/dev/null; then
cat >> "$HOME/.ssh/config" << 'EOF'
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host ssh.github.com
HostName ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
EOF
msg_success "GitHub SSH host configured"
else
msg_info "GitHub host already configured in SSH config"
fi
chmod 600 "$HOME/.ssh/config"
msg_info "Switching git remotes to SSH..."
if [ -d "$REPO_DIR/.git" ]; then
cd "$REPO_DIR" || exit 1
local REMOTE_URL=$(git remote get-url origin 2>/dev/null)
if [[ "$REMOTE_URL" == https://github.com/* ]]; then
local REPO_PATH="${REMOTE_URL#https://github.com/}"
git remote set-url origin "git@github.com:$REPO_PATH"
msg_success "Switched to SSH: git@github.com:$REPO_PATH"
elif [[ "$REMOTE_URL" == git@github.com:* ]]; then
msg_info "Remote already using SSH"
fi
fi