-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebian-server-post-install.sh
More file actions
executable file
·3157 lines (2781 loc) · 112 KB
/
Copy pathdebian-server-post-install.sh
File metadata and controls
executable file
·3157 lines (2781 loc) · 112 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
: '
Debian Server Post-Installation Script
Automates system configuration, hardening, and tooling setup for Debian servers with profile-based defaults and safe security configurations.
Designed for Debian Server, but can be used on Debian and Ubuntu Server distributions.
For Ubuntu Desktop, see: github.com/franckferman/ubuntu-post-install
Author : Franck FERMAN
Created : 16/05/2026
Updated : 16/05/2026
Version : 1.0.0
'
# ----------------------------------------------
# URLs
# ----------------------------------------------
URL_OHMYZSH="https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh"
URL_VIM_PLUG="https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"
URL_POWERLEVEL10K="https://github.com/romkatv/powerlevel10k.git"
URL_PLUGIN_AUTOSUGGESTIONS="https://github.com/zsh-users/zsh-autosuggestions"
URL_PLUGIN_SYNTAX_HIGHLIGHTING="https://github.com/zsh-users/zsh-syntax-highlighting"
URL_PLUGIN_COMPLETIONS="https://github.com/zsh-users/zsh-completions"
URL_LAZYVIM="https://github.com/LazyVim/starter"
URL_NERD_FONTS_API="https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest"
URL_DOCKER_GPG="https://download.docker.com/linux/debian/gpg"
URL_HASHICORP_GPG="https://apt.releases.hashicorp.com/gpg"
URL_MULLVAD="https://mullvad.net/download/app/deb/latest"
URL_MULLVAD_KEYRING="https://repository.mullvad.net/deb/mullvad-keyring.asc"
URL_MULLVAD_REPO="https://repository.mullvad.net/deb/stable"
URL_MULLVAD_GITHUB_API="https://api.github.com/repos/mullvad/mullvadvpn-app/releases/latest"
# ----------------------------------------------
# Output Symbols
# ----------------------------------------------
USE_EMOJIS=true
ICON_INFO=""
ICON_OK=""
ICON_SKIP=""
ICON_WARN=""
ICON_ERR=""
SEPARATOR="-----------------------------"
SELECTED_STEPS=""
SERVER_PROFILE="default"
EDITOR_MODE="both"
VIM_PRESET="full"
COLOR_SCHEME="dark"
VIM_COLORSCHEME="desert"
FIREWALL="ufw"
FIREWALL_PROFILE="hardened"
ALLOW_SSH=true
HARDENING_PROFILE="server"
KEEP_AVAHI=false
KEEP_CUPS=false
LOCK_ROOT=false
INSTALL_FAIL2BAN=true
INSTALL_USBGUARD=false
HARDEN_SERVICES=true
HARDEN_PACKAGES=true
HARDEN_NETWORK=true
DISABLE_IPV6=true
DISABLE_ICMP_REDIRECTS=false
DISABLE_SOURCE_ROUTING=false
DISABLE_MARTIANS_LOGGING=true
DISABLE_ICMP_PROTECTION=false
DISABLE_TCP_PROTECTION=false
DISABLE_ANTISPOOFING=true
DISABLE_CONNECTION_LIMITS=true
DISABLE_MODERN_SECURITY=true
DISABLE_KEXEC=false
SKIP_SERVICES=""
SKIP_PACKAGES=""
CUSTOM_SERVICES_LIST=""
CUSTOM_PACKAGES_LIST=""
_LOCK_ROOT_EXPLICIT=false
_USBGUARD_EXPLICIT=false
_EDITOR_MODE_EXPLICIT=false
_FIREWALL_EXPLICIT=false
_FIREWALL_PROFILE_EXPLICIT=false
_VIM_PRESET_EXPLICIT=false
_HARDEN_NETWORK_EXPLICIT=false
APPS_PROFILE="default"
EXTRA_PACKAGES=""
SKIP_APT_PACKAGES=""
INSTALL_DOCKER=false
DOCKER_COMPOSE=true
DOCKER_TYPE="io"
INSTALL_MONITORING=true
ENABLE_LOGGING=true
CLEAR_BASH_HISTORY=true
ZSH_PLUGINS_PROFILE="default"
P10K_PRESET="classic"
P10K_CUSTOM=true
P10K_SEGMENTS=false
INSTALL_MULLVAD=false
MULLVAD_SOURCE="apt"
_MULLVAD_SOURCE_EXPLICIT=false
INSTALL_NERD_FONTS=false
NERD_FONTS_PROFILE="default"
EXTRA_REPOS=""
SKIP_EXTRAS=""
ENABLE_SSH_HARDENING=true
ALLOW_ROOT=false
SSH_PORT=22
SSH_KEY_ONLY=false
DISABLE_ROOT_SSH=false
SSH_IPV6_ENABLED=true
SSH_IPV4_ENABLED=true
SSH_LISTEN_ADDRESSES=""
SSH_MODERN_ONLY=false
SSH_RSA_DISABLED=false
log_section() {
echo "$SEPARATOR"
echo "${ICON_INFO} $1"
echo "$SEPARATOR"
}
_init_symbols() {
if $USE_EMOJIS; then
ICON_INFO="ℹ️ "
ICON_OK="✅"
ICON_SKIP="➡️ "
ICON_WARN="⚠️ "
ICON_ERR="❌"
else
ICON_INFO="[*]"
ICON_OK="[+]"
ICON_SKIP="[=]"
ICON_WARN="[!]"
ICON_ERR="[x]"
fi
}
show_help() {
# Print usage information and exit.
cat << EOF
Debian Server Post-Installation Script v1.0.0
Automates system configuration, hardening, and tooling setup for Debian servers with profile-based defaults and safe security configurations.
Supported distributions:
- Debian (11/12/testing)
- Ubuntu Server (20.04/22.04/24.04)
- Other Debian derivatives
Usage:
$(basename "$0") [options]
Options:
-h, --help Show this help message and exit.
--no-emojis Use plain text prefixes instead of emoji output symbols.
--steps <spec> Run only the specified steps (default: all).
Accepts numbers, ranges, and combinations:
--steps 1 Run step 1 only.
--steps 1,2,4 Run steps 1, 2, and 4.
--steps 2-8 Run steps 2 through 8.
--steps 1,3-12 Run step 1 and steps 3 through 12.
--server-profile <p> Server deployment profile (default: default).
Sets intelligent defaults for editor, firewall, and hardening:
default Balanced configuration for general use
prod Production-ready with performance focus
dev Development-friendly with convenience
minimal Lightweight, essential tools only
hardened Maximum security, minimal attack surface
--editor <mode> Select which editor(s) to install (default: both).
both Install Vim config and LazyVim (Neovim).
vim Install Vim config only.
neovim Install LazyVim only.
none Skip editor installation entirely.
--firewall <engine> Firewall engine to configure (default: ufw).
ufw UFW — simple, recommended for servers.
nftables Native nftables — modern kernel-level firewall.
iptables Legacy iptables — widely known, still supported.
--firewall-profile <p> Firewall ruleset profile (default: hardened).
hardened Drop all incoming, allow outgoing + established.
transparent Allow all traffic — for testing or trusted networks.
--allow-root Allow script execution as root user (typical for VPS fresh install).
--allow-ssh Open SSH port (default: enabled for servers).
Configurable with --ssh-port.
--ssh-port <port> SSH port number (default: 22).
--ssh-key-only Disable password authentication, keys only.
--disable-root-ssh Disable root SSH login.
--no-disable-root-ssh Allow root SSH login (default: enabled for remote access safety).
--ssh-disable-ipv6 Force SSH to IPv4 only (AddressFamily inet).
--ssh-disable-ipv4 Force SSH to IPv6 only (AddressFamily inet6).
--ssh-enable-ipv6 Explicitly enable IPv6 (default: enabled).
--ssh-enable-ipv4 Explicitly enable IPv4 (default: enabled).
--ssh-listen-address <ip> Bind SSH to specific IP address (can be used multiple times).
--ssh-modern-only Remove legacy SSH options (Protocol 2, etc.).
--no-ssh-modern-only Keep legacy SSH compatibility (default: enabled).
--ssh-rsa Enable RSA host key for legacy compatibility (default: enabled).
--no-ssh-rsa Disable RSA host key for modern clients only.
--vim-preset <preset> Vim configuration depth (default: varies by profile).
full vim-plug + gruvbox + NERDTree + airline + extras.
minimal gruvbox (native packages) + settings only.
bare Settings only, built-in colorscheme.
--vim-colorscheme <name> Built-in vim colorscheme for bare preset (default: desert).
Available: blue, darkblue, default, delek, desert, elflord,
evening, industry, koehler, morning, murphy, pablo, peachpuff,
ron, shine, slate, torte, zellner.
--hardening-profile <p> Hardening baseline (default: server).
server Server-oriented hardening. Preserves: apache2, bind9,
nginx, postfix, mysql, postgresql. Root lock disabled.
workstation Maximum hardening. All non-essential services disabled.
enterprise Conservative. Preserves corporate services. USB controlled.
--lock-root Lock the root account (disable password login).
--no-lock-root Keep root account unlocked (default: enabled for operational safety).
--install-usbguard Install USBGuard (disabled by default for servers).
--no-install-usbguard Skip USBGuard installation (default for servers).
--harden-services Stop and mask risky services (default: enabled).
--no-harden-services Skip stopping and masking risky services.
--harden-services-list <services> Custom list of services to harden (comma-separated).
Overrides profile defaults. Example: --harden-services-list "cups,bluetooth"
--harden-packages Remove legacy/insecure packages (default: enabled).
--no-harden-packages Skip removing legacy/insecure packages.
--harden-packages-list <packages> Custom list of packages to remove (comma-separated).
Overrides profile defaults. Example: --harden-packages-list "telnet,tftp"
--no-harden-network Skip network hardening (sysctl tweaks).
--disable-ipv6 Disable IPv6 completely (default).
--no-disable-ipv6 Keep IPv6 enabled with security hardening.
--disable-icmp-redirects Disable ICMP redirect protection.
--no-disable-icmp-redirects Enable ICMP redirect protection (default).
--disable-source-routing Disable source routing protection.
--no-disable-source-routing Enable source routing protection (default).
--disable-martians-logging Disable martians packet logging (default).
--no-disable-martians-logging Enable martians packet logging.
--disable-icmp-protection Disable ICMP security protection.
--no-disable-icmp-protection Enable ICMP security protection (default).
--disable-tcp-protection Disable TCP security protection.
--no-disable-tcp-protection Enable TCP security protection (default).
--disable-antispoofing Disable anti-spoofing protection (default).
--no-disable-antispoofing Enable anti-spoofing protection.
--disable-connection-limits Disable connection limits tuning (default).
--no-disable-connection-limits Enable TCP connection limits tuning.
--disable-modern-security Disable modern security features (default).
--no-disable-modern-security Enable modern security features.
--disable-kexec Allow kexec system call (specialized environments).
--no-disable-kexec Disable kexec system call (default, security hardening).
--skip-services <list> Comma-separated services to keep when hardening.
--skip-packages <list> Comma-separated packages to keep when hardening.
--apps-profile <p> APT package selection profile (default: default).
minimal Survival only: git, curl, vim, fail2ban, tmux (5 pkg).
default Minimal + comfort + infrastructure: wget, zsh, build-essential (26 pkg).
server Default + server mgmt: logrotate, monitoring (34 pkg).
minimal-development Server + light dev: python3-dev, make, golang-go (32 pkg).
development Minimal-dev + full stack: nodejs, golang, docker (43 pkg).
security Server + network security: nmap, tcpdump (37 pkg).
defense Security + blue team: lynis, wireshark, aide (45 pkg).
offsec Security + red team: netcat-openbsd (38 pkg).
full Development + Defense + Offsec (55 pkg).
enterprise Full + compliance + backup: auditd, backup-manager (66 pkg).
--extra-packages <l> Comma-separated APT packages to add to profile.
--skip-apt-packages <l> Comma-separated APT packages to remove from profile.
--install-docker Force Docker installation.
--no-docker Skip Docker installation.
--no-docker-compose Skip Docker Compose installation.
--docker-type <type> Docker package type (default: io).
io - docker.io (Debian/Ubuntu repos, stable)
ce - docker-ce (Docker official repos, latest)
--no-monitoring Skip monitoring tools installation.
--no-logging Skip enhanced logging setup.
--install-mullvad Install Mullvad VPN client.
--mullvad-source <m> Mullvad install method (default: apt with auto-fallback).
apt Official APT repository (most secure).
direct Direct .deb from mullvad.net.
github GitHub releases (third-party CDN).
--no-mullvad Skip Mullvad VPN installation.
--install-nerd-fonts Install Nerd Fonts for terminal enhancement.
--nerd-fonts-profile <p> Nerd Fonts selection (default: default).
default FiraCode and JetBrains Mono.
minimal FiraCode only.
full Multiple popular fonts.
--no-fail2ban Skip Fail2ban installation.
--no-ssh-hardening Skip SSH hardening configuration.
--extras <l> Comma-separated extras to install.
Available: docker, gh, hashicorp, monitoring, red-team.
--skip-extras <l> Extras to exclude from installation.
Steps executed:
1. System update (apt update, full-upgrade, autoclean, autoremove)
2. Network & Firewall (firewall config + network hardening)
3. SSH hardening (secure SSH configuration + key management)
4. System hardening (CIS compliance, sysctl, service hardening)
5. Core server packages (essential server tools)
6. Security monitoring (fail2ban, lynis, intrusion detection)
7. Logging & monitoring (rsyslog, logrotate, monitoring tools)
8. Container runtime (Docker + Docker Compose)
9. Vim configuration (vim-plug + themes + plugins)
10. Zsh & terminal (Oh My Zsh + Powerlevel10k + plugins)
11. Extra software (additional repos + packages, Mullvad VPN)
Author : Franck FERMAN
EOF
exit 0
}
parse_step_selection() {
# Expand a step specification (e.g. "1,3-5,8") into a sorted list of unique integers.
# Args: $1 = step spec string.
# Returns: space-separated list of step numbers, or exits with code 1 on invalid input.
local input="$1"
local -a result=()
IFS=',' read -ra parts <<< "$input"
for part in "${parts[@]}"; do
if [[ "$part" =~ ^([0-9]+)-([0-9]+)$ ]]; then
local start="${BASH_REMATCH[1]}"
local end="${BASH_REMATCH[2]}"
if (( start > end )); then
echo "Invalid range: $part (start must be <= end)."
exit 1
fi
for (( i=start; i<=end; i++ )); do
result+=("$i")
done
elif [[ "$part" =~ ^[0-9]+$ ]]; then
result+=("$part")
else
echo "Invalid step specification: '$part'."
echo "Run '$(basename "$0") --help' for usage."
exit 1
fi
done
# Sort and deduplicate
printf '%s\n' "${result[@]}" | sort -nu | tr '\n' ' '
}
detect_distribution() {
# Detect the current Debian-based distribution
# Returns: distribution name and version
if [[ ! -f /etc/os-release ]]; then
echo "${ICON_ERR} Cannot detect distribution: /etc/os-release not found."
exit 1
fi
source /etc/os-release
case "$ID" in
debian)
DISTRO="debian"
DISTRO_VERSION="$VERSION_ID"
DISTRO_CODENAME="$VERSION_CODENAME"
;;
ubuntu)
DISTRO="ubuntu"
DISTRO_VERSION="$VERSION_ID"
DISTRO_CODENAME="$UBUNTU_CODENAME"
;;
*)
if [[ "$ID_LIKE" == *"debian"* ]]; then
DISTRO="debian-like"
DISTRO_VERSION="$VERSION_ID"
DISTRO_CODENAME="$VERSION_CODENAME"
echo "${ICON_WARN} Detected Debian-like distribution: $PRETTY_NAME"
echo "${ICON_WARN} Proceeding with Debian compatibility mode."
else
echo "${ICON_ERR} Unsupported distribution: $PRETTY_NAME"
echo "${ICON_ERR} This script supports Debian and Ubuntu Server only."
exit 1
fi
;;
esac
echo "${ICON_OK} Detected: $PRETTY_NAME"
}
check_root() {
# Check if script is run as root (not allowed unless --allow-root)
if [[ $EUID -eq 0 ]] && ! $ALLOW_ROOT; then
echo "${ICON_ERR} This script should not be run as root."
echo "${ICON_ERR} Please run as a regular user with sudo privileges."
echo "${ICON_ERR} Or use --allow-root flag if you know what you're doing."
exit 1
elif [[ $EUID -eq 0 ]] && $ALLOW_ROOT; then
echo "${ICON_OK} Running as root with --allow-root flag."
fi
}
check_sudo() {
# Check if user has sudo privileges
if ! sudo -n true 2>/dev/null; then
echo "${ICON_ERR} This script requires sudo privileges."
echo "${ICON_ERR} Please ensure your user can run sudo commands."
exit 1
fi
}
show_banner() {
# Print ASCII art banner at script startup.
# Colors for terminal (if supported)
local RED='\033[0;31m'
local NC='\033[0m' # No Color
# Check if terminal supports colors (allow colors in most environments)
if [[ "${TERM:-}" != "dumb" ]] && command -v tput >/dev/null 2>&1 && [[ $(tput colors 2>/dev/null || echo 0) -ge 8 ]]; then
# ---[ Display Debian-themed ASCII art banner in red ]---
echo -e "${RED} _,met\$\$\$\$\$gg.
,g\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$P.
,g\$\$P\"\" \"\"\"Y\$\$.\".
,\$\$P' \`\$\$\$.
',\$\$P ,ggs. \`\$\$b:
\`d\$\$' ,\$P\"' . \$\$\$
\$\$P d\$' , \$\$P
\$\$: \$\$. - ,d\$\$'
\$\$; Y\$b._ _,d\$P'
Y\$\$. \`.\`\"Y\$\$\$\$P\"'
\`\$\$b \"-.__
\`Y\$\$b
\`Y\$\$.
\`\$\$b.
\`Y\$\$b.
\`\"Y\$b._
\`\"\"\"\"${NC}
Debian Server Post-Installation Script v1.0.0
Security-focused server configuration for Debian-based systems
"
else
# Fallback: plain ASCII without colors
cat << "EOF"
_,met$$$$$gg.
,g$$$$$$$$$$$$$$$P.
,g$$P"" """Y$$.".
,$$P' `$$$.
',$$P ,ggs. `$$b:
`d$$' ,$P"' . $$$
$$P d$' , $$P
$$: $$. - ,d$$'
$$; Y$b._ _,d$P'
Y$$. `.`"Y$$$$P"'
`$$b "-.__
`Y$$b
`Y$$.
`$$b.
`Y$$b.
`"Y$b._
`""""
Debian Server Post-Installation Script v1.0.0
Security-focused server configuration for Debian-based systems
EOF
fi
}
check_internet_connectivity() {
# Check internet connectivity by pinging a host (default: 1.1.1.1).
# Args: $1 (optional) — host to ping.
# Returns: 0 if reachable, 1 otherwise.
local host="${1:-1.1.1.1}"
ping -c 2 -W 5 "$host" > /dev/null 2>&1
return $?
}
# Initialize symbols based on emoji preference
_init_symbols
# Exit codes
readonly EXIT_SUCCESS=0
readonly EXIT_ERROR=1
readonly EXIT_INTERRUPT=130
parse_args() {
# Parse script arguments and set global variables
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
show_help
;;
--no-emojis)
USE_EMOJIS=false
;;
--steps)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --steps requires a value (e.g. --steps 1,3-5)."
exit 1
fi
SELECTED_STEPS="$2"
shift
;;
--server-profile)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --server-profile requires a value."
exit 1
fi
case "$2" in
default|prod|dev|minimal|hardened) SERVER_PROFILE="$2" ;;
*)
echo "Error: Invalid server profile '$2'."
echo " Valid profiles:"
echo " default - Balanced configuration for general use"
echo " prod - Production-ready with performance focus"
echo " dev - Development-friendly with convenience"
echo " minimal - Lightweight, essential tools only"
echo " hardened - Maximum security, minimal attack surface"
exit 1
;;
esac
shift
;;
--editor)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --editor requires a value."
exit 1
fi
case "$2" in
both|vim|neovim|none) EDITOR_MODE="$2" ;;
*) echo "Error: Invalid editor mode '$2'." ; exit 1 ;;
esac
_EDITOR_MODE_EXPLICIT=true
shift
;;
--vim-preset)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --vim-preset requires a value."
exit 1
fi
case "$2" in
full|minimal|bare) VIM_PRESET="$2" ;;
*) echo "Error: Invalid vim preset '$2'." ; exit 1 ;;
esac
_VIM_PRESET_EXPLICIT=true
shift
;;
--vim-colorscheme)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --vim-colorscheme requires a value."
exit 1
fi
VIM_COLORSCHEME="$2"
shift
;;
--firewall)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --firewall requires a value."
exit 1
fi
case "$2" in
ufw|nftables|iptables) FIREWALL="$2" ;;
*) echo "Error: Invalid firewall engine '$2'." ; exit 1 ;;
esac
_FIREWALL_EXPLICIT=true
shift
;;
--firewall-profile)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --firewall-profile requires a value."
exit 1
fi
case "$2" in
hardened|transparent) FIREWALL_PROFILE="$2" ;;
*) echo "Error: Invalid firewall profile '$2'." ; exit 1 ;;
esac
_FIREWALL_PROFILE_EXPLICIT=true
shift
;;
--allow-ssh)
ALLOW_SSH=true
;;
--allow-root)
ALLOW_ROOT=true
;;
--ssh-port)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --ssh-port requires a value (e.g. --ssh-port 2222)."
exit 1
fi
if [[ ! "$2" =~ ^[0-9]+$ ]]; then
echo "Error: invalid SSH port '$2'. Expected an integer between 1 and 65535."
echo " Examples: --ssh-port 22, --ssh-port 2222"
exit 1
fi
if (( $2 < 1 || $2 > 65535 )); then
echo "Error: SSH port '$2' is out of valid range."
echo " Expected: integer between 1 and 65535."
echo " Common secure ports: 2222, 2200, 22000"
exit 1
fi
if (( $2 < 1024 )) && [[ $2 != "22" ]]; then
echo "Warning: Port '$2' is in privileged range (< 1024)."
echo " Consider using a port > 1024 for better security."
fi
SSH_PORT="$2"
shift
;;
--ssh-key-only)
SSH_KEY_ONLY=true
;;
--disable-root-ssh)
DISABLE_ROOT_SSH=true
;;
--no-disable-root-ssh)
DISABLE_ROOT_SSH=false
;;
--ssh-disable-ipv6)
SSH_IPV6_ENABLED=false
;;
--ssh-disable-ipv4)
SSH_IPV4_ENABLED=false
;;
--ssh-enable-ipv6)
SSH_IPV6_ENABLED=true
;;
--ssh-enable-ipv4)
SSH_IPV4_ENABLED=true
;;
--ssh-listen-address)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --ssh-listen-address requires an IP address."
exit 1
fi
SSH_LISTEN_ADDRESSES="$SSH_LISTEN_ADDRESSES $2"
shift
;;
--ssh-modern-only)
SSH_MODERN_ONLY=true
;;
--no-ssh-modern-only)
SSH_MODERN_ONLY=false
;;
--ssh-rsa)
SSH_RSA_DISABLED=false
;;
--no-ssh-rsa)
SSH_RSA_DISABLED=true
;;
--hardening-profile)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --hardening-profile requires a value (server|workstation|enterprise)."
exit 1
fi
case "$2" in
server|workstation|enterprise) HARDENING_PROFILE="$2" ;;
*)
echo "Error: invalid --hardening-profile value '$2'."
echo " Valid profiles:"
echo " server - Server-optimized hardening (default)"
echo " workstation - Maximum hardening for desktops"
echo " enterprise - Conservative hardening for corporate"
exit 1
;;
esac
shift
;;
--lock-root)
LOCK_ROOT=true
_LOCK_ROOT_EXPLICIT=true
;;
--no-lock-root)
LOCK_ROOT=false
_LOCK_ROOT_EXPLICIT=true
;;
--install-usbguard)
INSTALL_USBGUARD=true
_USBGUARD_EXPLICIT=true
;;
--no-install-usbguard)
INSTALL_USBGUARD=false
_USBGUARD_EXPLICIT=true
;;
--harden-services)
HARDEN_SERVICES=true
;;
--no-harden-services)
HARDEN_SERVICES=false
CUSTOM_SERVICES_LIST="" # Clear custom list if user says NO
;;
--harden-services-list)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --harden-services-list requires a value (comma-separated services)."
exit 1
fi
CUSTOM_SERVICES_LIST="$2"
HARDEN_SERVICES=true # Force enable hardening with custom list
shift
;;
--harden-packages)
HARDEN_PACKAGES=true
;;
--no-harden-packages)
HARDEN_PACKAGES=false
CUSTOM_PACKAGES_LIST="" # Clear custom list if user says NO
;;
--harden-packages-list)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --harden-packages-list requires a value (comma-separated packages)."
exit 1
fi
CUSTOM_PACKAGES_LIST="$2"
HARDEN_PACKAGES=true # Force enable hardening with custom list
shift
;;
--no-harden-network)
HARDEN_NETWORK=false
_HARDEN_NETWORK_EXPLICIT=true
;;
--disable-ipv6)
DISABLE_IPV6=true
;;
--no-disable-ipv6)
DISABLE_IPV6=false
;;
--disable-icmp-redirects)
DISABLE_ICMP_REDIRECTS=true
;;
--no-disable-icmp-redirects)
DISABLE_ICMP_REDIRECTS=false
;;
--disable-source-routing)
DISABLE_SOURCE_ROUTING=true
;;
--no-disable-source-routing)
DISABLE_SOURCE_ROUTING=false
;;
--disable-martians-logging)
DISABLE_MARTIANS_LOGGING=true
;;
--no-disable-martians-logging)
DISABLE_MARTIANS_LOGGING=false
;;
--disable-icmp-protection)
DISABLE_ICMP_PROTECTION=true
;;
--no-disable-icmp-protection)
DISABLE_ICMP_PROTECTION=false
;;
--disable-tcp-protection)
DISABLE_TCP_PROTECTION=true
;;
--no-disable-tcp-protection)
DISABLE_TCP_PROTECTION=false
;;
--disable-antispoofing)
DISABLE_ANTISPOOFING=true
;;
--no-disable-antispoofing)
DISABLE_ANTISPOOFING=false
;;
--disable-connection-limits)
DISABLE_CONNECTION_LIMITS=true
;;
--no-disable-connection-limits)
DISABLE_CONNECTION_LIMITS=false
;;
--disable-modern-security)
DISABLE_MODERN_SECURITY=true
;;
--no-disable-modern-security)
DISABLE_MODERN_SECURITY=false
;;
--disable-kexec)
DISABLE_KEXEC=true
;;
--no-disable-kexec)
DISABLE_KEXEC=false
;;
--skip-services)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --skip-services requires a value."
exit 1
fi
SKIP_SERVICES="$2"
shift
;;
--skip-packages)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --skip-packages requires a value."
exit 1
fi
SKIP_PACKAGES="$2"
shift
;;
--apps-profile)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --apps-profile requires a value."
exit 1
fi
case "$2" in
minimal|default|server|minimal-development|development|security|defense|offsec|full|enterprise) APPS_PROFILE="$2" ;;
*)
echo "Error: invalid --apps-profile value '$2'."
echo " Valid profiles:"
echo " minimal - Essential tools only (git, vim, curl, tmux)"
echo " default - Minimal + infrastructure tools (python, build)"
echo " server - Default + server management tools"
echo " minimal-development - Server + light dev tools (python-dev, make)"
echo " development - Minimal-dev + full stack (node, golang, docker)"
echo " security - Server + general security tools (nmap, tcpdump)"
echo " defense - Security + blue team tools (lynis, wireshark)"
echo " offsec - Security + red team tools (netcat, etc.)"
echo " full - Development + Defense + Offsec (everything)"
echo " enterprise - Full + enterprise tools (compliance, audit)"
exit 1
;;
esac
shift
;;
--extra-packages)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --extra-packages requires a comma-separated list of package names."
echo " Example: --extra-packages htop,ncdu,jq,bat"
exit 1
fi
# Validate package names (basic check)
if [[ ! "$2" =~ ^[a-zA-Z0-9._+-]+(,[a-zA-Z0-9._+-]+)*$ ]]; then
echo "Error: invalid package names in '$2'."
echo " Package names should contain only letters, numbers, dots, hyphens, plus signs."
echo " Example: --extra-packages htop,ncdu,jq"
exit 1
fi
EXTRA_PACKAGES="$2"
shift
;;
--skip-apt-packages)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --skip-apt-packages requires a comma-separated list of package names."
echo " Example: --skip-apt-packages taskwarrior,rssguard"
exit 1
fi
# Validate package names
if [[ ! "$2" =~ ^[a-zA-Z0-9._+-]+(,[a-zA-Z0-9._+-]+)*$ ]]; then
echo "Error: invalid package names in '$2'."
echo " Package names should contain only letters, numbers, dots, hyphens, plus signs."
exit 1
fi
SKIP_APT_PACKAGES="$2"
shift
;;
--install-docker)
INSTALL_DOCKER=true
;;
--no-docker)
INSTALL_DOCKER=false
;;
--no-docker-compose)
DOCKER_COMPOSE=false
;;
--docker-type)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --docker-type requires a value."
exit 1
fi
case "$2" in
io|ce)
DOCKER_TYPE="$2"
INSTALL_DOCKER=true # Auto-enable Docker when type specified
;;
*)
echo "Error: Invalid docker type '$2'."
echo " Valid types:"
echo " io - docker.io (Debian/Ubuntu repos, stable)"
echo " ce - docker-ce (Docker official repos, latest)"
exit 1
;;
esac
shift
;;
--no-monitoring)
INSTALL_MONITORING=false
;;
--no-logging)
ENABLE_LOGGING=false
;;
--no-fail2ban)
INSTALL_FAIL2BAN=false
;;
--no-ssh-hardening)
ENABLE_SSH_HARDENING=false
;;
--install-mullvad)
INSTALL_MULLVAD=true
;;
--mullvad-source)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --mullvad-source requires a value (apt|direct|github)."
exit 1
fi
case "$2" in
apt|direct|github)
MULLVAD_SOURCE="$2"
_MULLVAD_SOURCE_EXPLICIT=true
;;
*)
echo "Error: invalid --mullvad-source value '$2'."
echo " Valid sources:"
echo " apt - Official APT repository (most secure)"
echo " direct - Direct .deb download from mullvad.net"
echo " github - GitHub releases (third-party CDN)"
exit 1
;;
esac
shift
;;
--no-mullvad)
INSTALL_MULLVAD=false
;;
--install-nerd-fonts)
INSTALL_NERD_FONTS=true
;;
--nerd-fonts-profile)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --nerd-fonts-profile requires a value (default|minimal|full)."
exit 1
fi
case "$2" in
default|minimal|full) NERD_FONTS_PROFILE="$2" ;;
*)
echo "Error: invalid --nerd-fonts-profile value '$2'."
echo " Valid profiles:"
echo " default - FiraCode and JetBrains Mono"
echo " minimal - FiraCode only"
echo " full - Multiple popular fonts"
exit 1
;;
esac
shift
;;
--extras)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --extras requires a comma-separated list."
echo " Example: --extras gh,hashicorp,monitoring"
exit 1
fi
EXTRA_REPOS="$2"
shift
;;
--skip-extras)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --skip-extras requires a comma-separated list."
echo " Example: --skip-extras podman,azurecli"
exit 1
fi
SKIP_EXTRAS="$2"
shift
;;
*)
echo "Error: Unknown option '$1'."
echo "Run '$(basename "$0") --help' for usage."
exit 1
;;
esac
shift
done
}
# ==============================================
# STEP FUNCTIONS
# ==============================================
step_01_system_update() {
# Step 1: System update
log_section "Step 1: System update process initiated."
if check_internet_connectivity; then
echo "${ICON_OK} Internet connectivity confirmed. Proceeding with system updates..."
echo "${ICON_OK} Updating package lists (apt update)..."
sudo apt update
echo "${ICON_OK} Configuring debconf for non-interactive installation..."
echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selections
sudo dpkg-reconfigure -f noninteractive debconf
echo "${ICON_OK} Upgrading all packages (apt full-upgrade)..."
DEBIAN_FRONTEND=noninteractive sudo apt full-upgrade -y
echo "${ICON_OK} Cleaning up package cache (apt autoclean)..."
sudo apt autoclean -y
echo "${ICON_OK} Removing unused packages (apt autoremove)..."
sudo apt autoremove -y
echo "${ICON_OK} System update process completed successfully."
else
echo "${ICON_WARN} Skipping system update: No internet connection detected."
fi
}
step_02_network_firewall() {
# Step 2: Network & Firewall configuration
log_section "Step 2: Network & Firewall hardening."
# Disable all firewalls first for clean state
_fw_disable_all
case "$FIREWALL" in
ufw)
_fw_configure_ufw
;;
nftables)
_fw_configure_nftables
;;