| 1 | #!/usr/bin/env bash |
| 2 | # Interactive Debian-family post-install bootstrap. |
| 3 | set -Eeuo pipefail |
| 4 | |
| 5 | SYNC_BASHRC_URL="${SYNC_BASHRC_URL:-https://gist.darto.id/dartokloning/82f93f1f0a294d55bd5f1a0abcb7f63f/download/HEAD/sync-bashrc-all.sh}" |
| 6 | SYNC_BASHRC_FILE="/usr/local/sbin/sync-bashrc-all.sh" |
| 7 | FAIL2BAN_JAIL="/etc/fail2ban/jail.d/sshd.local" |
| 8 | export DEBIAN_FRONTEND=noninteractive |
| 9 | |
| 10 | log() { printf '\n== %s ==\n' "$*"; } |
| 11 | ok() { printf ' [OK] %s\n' "$*"; } |
| 12 | warn() { printf ' [!] %s\n' "$*" >&2; } |
| 13 | die() { printf 'ERROR: %s\n' "$*" >&2; exit 1; } |
| 14 | trap 'warn "Failed at line $LINENO (exit $?). Check the output above."' ERR |
| 15 | |
| 16 | [[ $EUID -eq 0 ]] || die 'Run as root: use su - and then ./debian-post-install-v2.sh, or sudo ./debian-post-install-v2.sh' |
| 17 | [[ -t 0 ]] || die 'Interactive mode requires a terminal (stdin TTY).' |
| 18 | [[ -r /etc/os-release ]] || die '/etc/os-release was not found.' |
| 19 | . /etc/os-release |
| 20 | if [[ "${ID:-}" != debian && " ${ID_LIKE:-} " != *' debian '* ]]; then |
| 21 | warn "Distribution ${PRETTY_NAME:-unknown} does not identify itself as Debian-based." |
| 22 | command -v apt-get >/dev/null && command -v dpkg >/dev/null || die 'APT/dpkg is unavailable.' |
| 23 | read -r -p 'Continue despite potential compatibility issues? [y/N] ' confirm |
| 24 | [[ ${confirm,,} == y || ${confirm,,} == yes ]] || exit 0 |
| 25 | fi |
| 26 | command -v apt-get >/dev/null && command -v dpkg >/dev/null || die 'APT/dpkg is required.' |
| 27 | command -v systemctl >/dev/null || die 'This script requires systemd.' |
| 28 | |
| 29 | ask() { |
| 30 | local reply |
| 31 | while true; do |
| 32 | read -r -p "$1 [Y/n] " reply || die 'Terminal input was closed.' |
| 33 | case "${reply,,}" in |
| 34 | ''|y|yes) return 0 ;; |
| 35 | n|no) return 1 ;; |
| 36 | *) warn 'Please answer y or n.' ;; |
| 37 | esac |
| 38 | done |
| 39 | } |
| 40 | ask_strict() { |
| 41 | local reply |
| 42 | while true; do |
| 43 | read -r -p "$1 [y/N] " reply || die 'Terminal input was closed.' |
| 44 | case "${reply,,}" in |
| 45 | y|yes) return 0 ;; |
| 46 | ''|n|no) return 1 ;; |
| 47 | *) warn 'Please answer y or n.' ;; |
| 48 | esac |
| 49 | done |
| 50 | } |
| 51 | |
| 52 | APT_UPDATED=0 |
| 53 | install_packages() { |
| 54 | if (( ! APT_UPDATED )); then |
| 55 | apt-get update |
| 56 | APT_UPDATED=1 |
| 57 | fi |
| 58 | apt-get install -y "$@" |
| 59 | } |
| 60 | |
| 61 | SSH_PORTS=() |
| 62 | detect_ssh_ports() { |
| 63 | SSH_PORTS=() |
| 64 | local sshd_bin='' p |
| 65 | if [[ -x /usr/sbin/sshd ]]; then sshd_bin=/usr/sbin/sshd |
| 66 | elif command -v sshd >/dev/null 2>&1; then sshd_bin=$(command -v sshd) |
| 67 | fi |
| 68 | if [[ -n $sshd_bin ]]; then |
| 69 | while read -r p; do |
| 70 | [[ $p =~ ^[0-9]+$ ]] && (( p >= 1 && p <= 65535 )) && SSH_PORTS+=("$p") |
| 71 | done < <("$sshd_bin" -T 2>/dev/null | awk '$1=="port" {print $2}' | sort -un) |
| 72 | fi |
| 73 | # No silent port 22 fallback: user must verify when detection fails. |
| 74 | if (( ${#SSH_PORTS[@]} == 0 )); then |
| 75 | warn 'SSH ports could not be reliably detected.' |
| 76 | read -r -p 'Enter the SSH TCP port in use (e.g., 22): ' p |
| 77 | [[ $p =~ ^[0-9]+$ ]] && (( 10#$p >= 1 && 10#$p <= 65535 )) || die 'Invalid SSH port.' |
| 78 | SSH_PORTS=("$((10#$p))") |
| 79 | fi |
| 80 | ok "Port SSH: ${SSH_PORTS[*]}" |
| 81 | } |
| 82 | |
| 83 | status=() |
| 84 | record() { status+=("$1: $2"); } |
| 85 | |
| 86 | log "Debian Post Install v2 — ${PRETTY_NAME:-unknown}" |
| 87 | printf 'You will be prompted before each task. Press Enter to select Yes.\n' |
| 88 | |
| 89 | if ask '1. Install sudo and select users to grant sudo access?'; then |
| 90 | install_packages sudo |
| 91 | mapfile -t users < <(getent passwd | awk -F: '$3 >= 1000 && $3 < 65534 && $7 !~ /(nologin|false)$/ {print $1}' | sort -u) |
| 92 | if (( ${#users[@]} == 0 )); then |
| 93 | warn 'No regular login users found. Sudo was installed, but no users were added.' |
| 94 | record sudo 'installed; no users selected' |
| 95 | else |
| 96 | printf 'Available local users:\n' |
| 97 | for i in "${!users[@]}"; do printf ' %d) %s\n' "$((i+1))" "${users[i]}"; done |
| 98 | read -r -p 'User numbers (separate with spaces or commas; blank to skip): ' selection |
| 99 | selection=${selection//,/ } |
| 100 | declare -A seen=() |
| 101 | selected=() |
| 102 | for number in $selection; do |
| 103 | [[ $number =~ ^[0-9]+$ ]] || die "Selection is not a number: $number" |
| 104 | (( number >= 1 && number <= ${#users[@]} )) || die "Selection is out of range: $number" |
| 105 | user=${users[number-1]} |
| 106 | [[ -v seen[$user] ]] && continue |
| 107 | seen[$user]=1 |
| 108 | selected+=("$user") |
| 109 | done |
| 110 | if (( ${#selected[@]} )); then |
| 111 | printf 'Accounts to add to the sudo group: %s\n' "${selected[*]}" |
| 112 | if ask_strict 'Confirm granting administrative privileges?'; then |
| 113 | for user in "${selected[@]}"; do |
| 114 | usermod -aG sudo "$user" |
| 115 | ok "$user added to the sudo group (effective at next login)." |
| 116 | done |
| 117 | record sudo "user: ${selected[*]}" |
| 118 | else |
| 119 | record sudo 'user additions canceled' |
| 120 | fi |
| 121 | else |
| 122 | record sudo 'installed; user additions skipped' |
| 123 | fi |
| 124 | fi |
| 125 | else record sudo 'skipped'; fi |
| 126 | |
| 127 | if ask '2. Install and enable vnStat?'; then |
| 128 | install_packages vnstat |
| 129 | systemctl enable --now vnstat |
| 130 | record vnStat "$(systemctl is-active vnstat || true)" |
| 131 | else record vnStat 'skipped'; fi |
| 132 | |
| 133 | if ask '3. Install Vim?'; then |
| 134 | install_packages vim vim-common |
| 135 | command -v vim >/dev/null || die 'Vim was not found.' |
| 136 | record Vim 'installed' |
| 137 | else record Vim 'skipped'; fi |
| 138 | |
| 139 | if ask '4. Download and run global Bash/Fastfetch synchronization?'; then |
| 140 | warn 'This task runs an external script as root and may change system-wide settings.' |
| 141 | if ask_strict 'Do you trust the source and want to execute it?'; then |
| 142 | install_packages curl ca-certificates |
| 143 | tmp_sync=$(mktemp) |
| 144 | if curl -fsSL "$SYNC_BASHRC_URL" -o "$tmp_sync" && [[ -s $tmp_sync ]]; then |
| 145 | bash -n "$tmp_sync" || die 'The synchronization script has invalid Bash syntax.' |
| 146 | install -o root -g root -m 0755 "$tmp_sync" "$SYNC_BASHRC_FILE" |
| 147 | rm -f "$tmp_sync" |
| 148 | "$SYNC_BASHRC_FILE" |
| 149 | record Bash/Fastfetch 'synchronization completed' |
| 150 | else |
| 151 | rm -f "$tmp_sync" |
| 152 | die 'Failed to download the synchronization script.' |
| 153 | fi |
| 154 | else record Bash/Fastfetch 'canceled'; fi |
| 155 | else record Bash/Fastfetch 'skipped'; fi |
| 156 | |
| 157 | if ask '5. Install and configure UFW?'; then |
| 158 | install_packages ufw |
| 159 | detect_ssh_ports |
| 160 | for port in "${SSH_PORTS[@]}"; do ufw allow "${port}/tcp" comment 'SSH'; done |
| 161 | warn 'Review SSH ports and application rules (HAProxy, HTTP/HTTPS, etc.) before enabling UFW.' |
| 162 | ufw status verbose || true |
| 163 | if ask_strict 'Enable UFW now? Make sure your remote connection will remain accessible.'; then |
| 164 | ufw --force enable |
| 165 | systemctl enable --now ufw |
| 166 | ufw status verbose |
| 167 | record UFW 'enabled' |
| 168 | else record UFW 'installed; not enabled by this script'; fi |
| 169 | else record UFW 'skipped'; fi |
| 170 | |
| 171 | if ask '6. Install and configure Fail2ban for SSH?'; then |
| 172 | install_packages fail2ban python3-systemd |
| 173 | detect_ssh_ports |
| 174 | if [[ -e $FAIL2BAN_JAIL ]]; then |
| 175 | backup="${FAIL2BAN_JAIL}.bak.$(date +%Y%m%d-%H%M%S)" |
| 176 | cp -a "$FAIL2BAN_JAIL" "$backup" |
| 177 | ok "Configuration backup: $backup" |
| 178 | fi |
| 179 | if command -v ufw >/dev/null && ufw status 2>/dev/null | grep -q '^Status: active'; then |
| 180 | ban_action=ufw |
| 181 | else |
| 182 | ban_action=iptables-multiport |
| 183 | install_packages iptables |
| 184 | warn 'UFW is inactive; using the iptables-multiport ban action.' |
| 185 | fi |
| 186 | mkdir -p /etc/fail2ban/jail.d |
| 187 | ports_csv=$(IFS=,; echo "${SSH_PORTS[*]}") |
| 188 | cat > "$FAIL2BAN_JAIL" <<JAIL |
| 189 | [sshd] |
| 190 | enabled = true |
| 191 | backend = systemd |
| 192 | port = ${ports_csv} |
| 193 | bantime = 4w |
| 194 | maxretry = 2 |
| 195 | banaction = ${ban_action} |
| 196 | JAIL |
| 197 | chmod 0644 "$FAIL2BAN_JAIL" |
| 198 | fail2ban-client -t || die 'Fail2ban validation failed; inspect the jail file and backup.' |
| 199 | systemctl enable --now fail2ban |
| 200 | systemctl restart fail2ban |
| 201 | systemctl is-active --quiet fail2ban || die 'Fail2ban failed to start.' |
| 202 | record Fail2ban "active; ports ${ports_csv}; action ${ban_action}" |
| 203 | else record Fail2ban 'skipped'; fi |
| 204 | |
| 205 | log 'Summary' |
| 206 | for line in "${status[@]}"; do printf ' - %s\n' "$line"; done |
| 207 | ok 'Completed. Sudo group membership changes take effect after users log in again.' |
| 208 |
dartokloning / Debian Post Installation
Last active 4 weeks ago
Debian Post Installation