前言
Linux 開放原始碼雖透明,但錯誤配置與未修補漏洞是入侵主因。本文提煉十大高效加固策略,涵蓋服務、存取、認證、核心、審計、工具鏈,助您建立纏深防禦體系。
策略 1:精簡服務 — 減少攻擊面
- 檢查
/etc/inetd.conf (或 xinetd.d/),註解 (`#`) 非必要服務:tftp、imap/ipop、gopher、daytime、time、finger、systat、netstat 等資訊洩露服務。
- 檢查
/etc/services 確認埠號對應正確。
- Systemd 時代:`systemctl list-unit-files --state=enabled` 審視啟用服務,`systemctl disable --now 服務名` 關閉並停用。
策略 2:強化存取控制 — 堡壘機與最小權限
- 禁用 root 遠端登入:`/etc/ssh/sshd_config` 設 `PermitRootLogin no`、`PermitEmptyPasswords no`、`MaxAuthTries 3`。
- 鍵對認證:強制 `PubkeyAuthentication yes`、`PasswordAuthentication no`,管理員僅持私鑰登入。
- 防火牆白名單:`firewall-cmd`/`iptables` 僅開放必要埠 (22/80/443),SSH 限制來源 IP 段。
- TCP Wrappers:`/etc/hosts.allow` (允許清單) + `/etc/hosts.deny` (預設拒絕 `ALL:ALL`)。
策略 3:影子密碼與 PAM 強化認證
- 啟用 `/etc/shadow` (預設已啟),權限 `600 root:root`。
- 配置 PAM (`/etc/pam.d/system-auth`、`password-auth`) 強制密碼複雜度 (`pam_pwquality.so`)、鎖定策略 (`pam_faillock.so`)、限制資源 (`pam_limits.so`)。
- 可選:雙因子認證 (Google Authenticator PAM 模組)。
策略 4:核心與軟體持續修補
策略 5:密碼審計與定期輪換
- 離線破解測試:`john --wordlist=rockyou.txt /etc/shadow` 或 `hashcat -m 1800`,發現弱密碼強制重設。
- 設定密碼存活期:`chage -M 90 -m 7 -W 14 user` (90 天過期、7 天不可改、14 天預警)。
- 禁止重用近 5 代密碼:PAM `remember=5`。
策略 6:帳號生命週期與最小權限
- 建立帳號指定 shell:`useradd -s /sbin/nologin -M -r service_user` (服務帳號無登入殼層)。
- sudo 精細授權:`/etc/sudoers.d/` 以命令級別授權,如 `backup ALL=(root) NOPASSWD: /usr/bin/rsync`。
- 定期審計 `/etc/passwd`:鎖定/刪除離職、長期未登入帳號 (`lastlog -b 90`)。
- `/etc/securetty` 僅保留 `tty1`、`console`,禁止遠端終端直接登入 root。
策略 7:移除危險的 r* 指令與信任關係
- 移除 `rsh`、`rlogin`、`rexec`、`rcp` 客戶端/服務端套件。
- 確保 `/root/.rhosts`、`/etc/hosts.equiv` 不存在或為空。
- PAM 封鎖:`/etc/pam.d/rlogin` 加入 `auth required pam_deny.so`。
策略 8:部署 SSH 替代傳統明文協議
- 全站強制 SSH/SFTP/SCP,淘汰 Telnet/FTP/RSH。
- Windows 端使用 WinSCP、FileZilla (SFTP 模式)、PuTTY/Xshell。
- SSH 進階:`Port 2222` (非標準埠)、`AllowUsers admin@192.168.1.0/24`、`ClientAliveInterval 300`、`ClientAliveCountMax 2`。
策略 9:系統完整性監控與入侵偵測
- AIDE / Tripwire:建立檔案指紋資料庫,每日排程比對關鍵目錄 (`/bin`, `/sbin`, `/etc`, `/usr/bin`)。
- auditd:審計關鍵系統呼叫 (`auditctl -w /etc/passwd -p wa -k passwd_changes`)。
- Logwatch / Logcheck:每日郵件摘要異常日誌 (失敗登入、sudo 使用、防火牆阻擋)。
- Rootkit 掃描:`rkhunter --check`、`chkrootkit` 每週執行。
策略 10:備份與災難恢復演練
- 3-2-1 原則:3 份副本、2 種介質、1 份異地 (含離線冷備份)。
- 資料庫熱備:`mysqldump --single-transaction --routines --triggers` 或 Percona XtraBackup。
- 每季進行全量恢復演練,驗證備份可用性與 RTO/RPO 指標。
總結檢查清單 (可直接複製執行)
# 1. 服務精簡
systemctl disable --now $(systemctl list-unit-files --state=enabled | grep -vE 'sshd|network|crond|syslog|irqbalance' | awk '{print $1}')
# 2. SSH 強化
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
systemctl reload sshd
# 3. 防火牆預設拒絕
firewall-cmd --set-default-zone=drop
firewall-cmd --permanent --add-service=ssh --add-service=http --add-service=https
firewall-cmd --reload
# 4. 核心參數
cat > /etc/sysctl.d/99-hardening.conf <<'EOF'
net.ipv4.tcp_syncookies=1
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.all.accept_redirects=0
kernel.randomize_va_space=2
fs.suid_dumpable=0
EOF
sysctl --system
安全是過程而非產品。持續監控、定期演練、最小權限、纏深防禦 — 才是 Linux 伺服器長治久安之道。