Linux 核心本身設計嚴謹,但預設安裝往往開啟過多服務、使用弱預設值。以下十招為經驗驗證的「高性價比」硬化手段,適用於絕大多數發布版。
ss -tuln 或 netstat -tuln 監聽端口。systemctl disable --now avahi-daemon cups rpcbind nfs-server/etc/inetd.conf(舊版)或 /etc/xinetd.d/ 註釋掉 finger、tftp、imap、pop3、daytime 等。/etc/passwd 密碼欄為 x,真實雜湊在 /etc/shadow(權限 600)。libpam-pwquality 強制密碼複雜度:password requisite pam_pwquality.so retry=3 minlen=12 difok=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1pam_faillock.so 或 pam_tally2.so 鎖定暴力破解帳號。su 僅允許 wheel 群組:auth required pam_wheel.so use_uid group=wheeldebian-security-announce、redhat-watch-list)。apt install unattended-upgrades / dnf install dnf-automatic/etc/sysctl.d/99-hardening.conf:net.ipv4.tcp_syncookies = 1 net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.all.send_redirects = 0 kernel.kptr_restrict = 2 kernel.dmesg_restrict = 1 fs.protected_hardlinks = 1 fs.protected_symlinks = 1
/etc/login.defs:PASS_MAX_DAYS 90 PASS_MIN_DAYS 7 PASS_WARN_AGE 14pam_pwhistory.so remember=5/etc/ssh/sshd_config:PermitRootLogin no、PermitEmptyPasswords no、PasswordAuthentication no(強制金鑰)。/etc/securetty 僅保留 tty1~tty6,移除 pts/*。sudo 替代直接 root 登入,配置 /etc/sudoers.d/ 最小權限:backup ALL=(root) NOPASSWD: /usr/bin/rsync, /bin/tarrsh-server、rsh-client、talk、ntalk 等套件。/etc/hosts.equiv 與 ~/.rhosts 不存在或為空。/etc/pam.d/rlogin 加入 auth required pam_deny.so。Protocol 2Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.comMACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.comAllowUsers/AllowGroups 白名單。/etc/sudoers.d/,每檔一角色。Defaults logfile=/var/log/sudo.log Defaults log_input, log_output(需較新 sudo 版本)。sudo -l -U username 檢查實際權限。aideinit && mv /var/lib/aide/aide.db.new /var/lib/aide/aide.dbcrontab -e # 0 3 * * * /usr/bin/aide --check | mail -s "AIDE Report" root/bin /sbin /usr/bin /usr/sbin /etc /boot。auditctl -w /etc/passwd -p wa -k passwd_chgauditctl -w /etc/sudoers -p wa -k sudoers_chg這十招並非一次性清單,而應納入持續安全生命週期:基線配置 → 自動化合規檢查 → 異常告警 → 事件響應 → 經驗回饋更新基線。唯有將安全融入日常運維,才能在攻防對抗中保持主動。