Dernière activité 1789871818

Debian Post Installation

debian-post-install.sh Brut
1#!/bin/bash
2#
3# debian-post-install.sh
4#
5# Bootstrap Debian fresh install:
6# - sudo + add user to sudo group
7# - vnStat
8# - Vim
9# - sync global .bashrc / shell prompt / Fastfetch config
10# - UFW firewall
11# - Fail2ban for SSH
12#
13# Jalankan sebagai root setelah:
14# su -
15#
16# Contoh:
17# chmod +x bootstrap-debian.sh
18# ./debian-post-install.sh
19#
20# Aman dijalankan ulang (idempotent).
21
22set -Eeuo pipefail
23
24# ---------------------------------------------------------------------------
25# Configuration
26# ---------------------------------------------------------------------------
27
28ADMIN_USER="${ADMIN_USER:-darto}"
29
30SYNC_BASHRC_URL="https://gist.darto.id/dartokloning/82f93f1f0a294d55bd5f1a0abcb7f63f/download/HEAD/sync-bashrc-all.sh"
31SYNC_BASHRC_FILE="/usr/local/sbin/sync-bashrc-all.sh"
32
33FAIL2BAN_JAIL="/etc/fail2ban/jail.d/sshd.local"
34
35# Port web yang biasa dibuka.
36ALLOW_HTTP="${ALLOW_HTTP:-yes}"
37ALLOW_HTTPS="${ALLOW_HTTPS:-yes}"
38
39# ---------------------------------------------------------------------------
40# Helpers
41# ---------------------------------------------------------------------------
42
43log() {
44 printf '\n== %s ==\n' "$*"
45}
46
47ok() {
48 printf ' -> %s\n' "$*"
49}
50
51die() {
52 printf 'ERROR: %s\n' "$*" >&2
53 exit 1
54}
55
56if [[ "$(id -u)" -ne 0 ]]; then
57 die "Jalankan script ini sebagai root. Gunakan: su -"
58fi
59
60if [[ ! -r /etc/os-release ]]; then
61 die "/etc/os-release tidak ditemukan."
62fi
63
64. /etc/os-release
65
66if [[ "${ID:-}" != "debian" ]]; then
67 die "Script ini khusus Debian. Sistem terdeteksi: ${PRETTY_NAME:-unknown}"
68fi
69
70if ! id "$ADMIN_USER" >/dev/null 2>&1; then
71 die "User '$ADMIN_USER' tidak ditemukan."
72fi
73
74export DEBIAN_FRONTEND=noninteractive
75
76# ---------------------------------------------------------------------------
77# 1. Update + packages
78# ---------------------------------------------------------------------------
79
80log "1. Update repository dan install paket dasar"
81
82apt-get update
83
84apt-get install -y \
85 sudo \
86 curl \
87 ca-certificates \
88 inxi \
89 vnstat \
90 vim \
91 vim-common \
92 ufw \
93 fail2ban
94
95ok "Paket dasar sudah terinstall."
96
97# ---------------------------------------------------------------------------
98# 2. Sudo access
99# ---------------------------------------------------------------------------
100
101log "2. Pastikan $ADMIN_USER masuk group sudo"
102
103if id -nG "$ADMIN_USER" | tr ' ' '\n' | grep -qx sudo; then
104 ok "$ADMIN_USER sudah menjadi anggota group sudo."
105else
106 usermod -aG sudo "$ADMIN_USER"
107 ok "$ADMIN_USER ditambahkan ke group sudo."
108fi
109
110printf ' -> Group %s: %s\n' "$ADMIN_USER" "$(id -nG "$ADMIN_USER")"
111
112# ---------------------------------------------------------------------------
113# 3. vnStat
114# ---------------------------------------------------------------------------
115
116log "3. Aktifkan vnStat"
117
118systemctl enable --now vnstat
119systemctl is-active --quiet vnstat \
120 && ok "vnstat aktif." \
121 || die "vnstat gagal aktif."
122
123# ---------------------------------------------------------------------------
124# 4. Vim
125# ---------------------------------------------------------------------------
126
127log "4. Verifikasi Vim"
128
129if command -v vim >/dev/null 2>&1; then
130 ok "Vim tersedia: $(vim --version | head -n1)"
131else
132 die "Vim tidak ditemukan setelah instalasi."
133fi
134
135# ---------------------------------------------------------------------------
136# 5. Sync .bashrc / prompt / Fastfetch
137# ---------------------------------------------------------------------------
138
139log "5. Download dan jalankan sync-bashrc-all.sh"
140
141tmp_sync="$(mktemp)"
142trap 'rm -f "$tmp_sync"' EXIT
143
144curl -fL --silent --show-error "$SYNC_BASHRC_URL" -o "$tmp_sync"
145
146if [[ ! -s "$tmp_sync" ]]; then
147 die "sync-bashrc-all.sh hasil download kosong."
148fi
149
150install -o root -g root -m 0755 "$tmp_sync" "$SYNC_BASHRC_FILE"
151
152ok "sync-bashrc-all.sh disimpan di $SYNC_BASHRC_FILE"
153
154"$SYNC_BASHRC_FILE"
155
156ok "Konfigurasi Bash/Fastfetch selesai disinkronkan."
157
158# ---------------------------------------------------------------------------
159# 6. Detect SSH port
160# ---------------------------------------------------------------------------
161
162log "6. Deteksi port SSH"
163
164SSH_PORT="22"
165
166if command -v sshd >/dev/null 2>&1; then
167 detected_port="$(sshd -T 2>/dev/null | awk '$1=="port" {print $2; exit}' || true)"
168 if [[ "$detected_port" =~ ^[0-9]+$ ]]; then
169 SSH_PORT="$detected_port"
170 fi
171fi
172
173ok "Port SSH terdeteksi: $SSH_PORT/tcp"
174
175# ---------------------------------------------------------------------------
176# 7. UFW
177# ---------------------------------------------------------------------------
178
179log "7. Konfigurasi UFW"
180
181# Jangan reset UFW agar rule custom yang mungkin sudah ada tidak hilang.
182ufw allow "${SSH_PORT}/tcp" comment 'SSH'
183
184#if [[ "$ALLOW_HTTP" == "yes" ]]; then
185# ufw allow 80/tcp comment 'HTTP'
186#fi
187
188#if [[ "$ALLOW_HTTPS" == "yes" ]]; then
189# ufw allow 443/tcp comment 'HTTPS'
190#fi
191
192# ufw --force enable aman untuk noninteractive.
193ufw --force enable
194systemctl enable --now ufw
195
196ok "UFW aktif."
197
198ufw status verbose
199
200# ---------------------------------------------------------------------------
201# 8. Fail2ban
202# ---------------------------------------------------------------------------
203
204log "8. Konfigurasi Fail2ban"
205
206mkdir -p /etc/fail2ban/jail.d
207
208cat > "$FAIL2BAN_JAIL" <<EOF
209[sshd]
210enabled = true
211backend = systemd
212port = ${SSH_PORT}
213bantime = 4w
214maxretry = 2
215banaction = ufw
216EOF
217
218chmod 0644 "$FAIL2BAN_JAIL"
219
220ok "Konfigurasi dibuat: $FAIL2BAN_JAIL"
221
222# Validasi konfigurasi sebelum restart.
223if ! fail2ban-client -t; then
224 die "Validasi konfigurasi Fail2ban gagal."
225fi
226
227systemctl enable --now fail2ban
228systemctl restart fail2ban
229
230sleep 1
231
232systemctl is-active --quiet fail2ban \
233 && ok "Fail2ban aktif." \
234 || die "Fail2ban gagal aktif."
235
236# ---------------------------------------------------------------------------
237# 9. Summary
238# ---------------------------------------------------------------------------
239
240log "9. Ringkasan"
241
242printf 'User admin : %s\n' "$ADMIN_USER"
243printf 'Group : %s\n' "$(id -nG "$ADMIN_USER")"
244printf 'SSH port : %s/tcp\n' "$SSH_PORT"
245printf 'vnStat : %s\n' "$(systemctl is-active vnstat || true)"
246printf 'UFW : %s\n' "$(systemctl is-active ufw || true)"
247printf 'Fail2ban : %s\n' "$(systemctl is-active fail2ban || true)"
248printf 'Fail2ban jail : %s\n' "$FAIL2BAN_JAIL"
249printf 'Bash sync : %s\n' "$SYNC_BASHRC_FILE"
250
251echo
252echo "Fail2ban SSH jail:"
253fail2ban-client status sshd 2>/dev/null || true
254
255echo
256echo "============================================================"
257echo "BOOTSTRAP DEBIAN SELESAI"
258echo "============================================================"
259echo
260echo "PENTING:"
261echo "User '$ADMIN_USER' baru memperoleh group sudo pada login session berikutnya."
262echo "Disconnect SSH lalu login kembali sebagai '$ADMIN_USER'."
263echo
264echo "Setelah login ulang, cek:"
265echo " sudo -v"
266echo " groups"
267echo " fastfetch --config /etc/fastfetch/config.jsonc"
268echo " sudo ufw status verbose"
269echo " sudo fail2ban-client status sshd"
270echo
271