「沒有絕對安全的系統,只有持續加固的過程。」本文提供從安裝部署到運行維護的分層加固清單,涵蓋磁碟分區、服務精簡、權限最小化、核心參數、審計日誌等關鍵面向。
/、/boot、/var、/tmp、/home、/opt 分區隔離,/tmp 掛載 noexec,nodev,nosuid# 設定 BIOS 密碼、禁用 USB/網路開機、啟用 Secure Boot
# GRUB 設定密碼
grub2-setpassword
# 產生 /boot/grub2/user.cfg
# 網路層防護
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.log_martians = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.tcp_syncookies = 1
# 核心防護
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
kernel.yama.ptrace_scope = 1
fs.suid_dumpable = 0
kernel.kexec_load_disabled = 1
# 生效
sysctl --system
# 列出啟用服務
systemctl list-unit-files --state=enabled
# 停用非必要服務 (視用途而定)
systemctl disable --now postfix cups avahi-daemon bluetooth \
rpcbind nfs-server chronyd (若用 ntpd) ...
# 檢查監聽埠
ss -tulnp
chmod 600 + chattr +inodev,nosuid,noexec (視應用相容性)# 密碼策略 /etc/security/pwquality.conf
minlen = 14
dcredit = -1
ucredit = -1
ocredit = -1
lcredit = -1
maxrepeat = 3
# 登入失敗鎖定 /etc/pam.d/system-auth
auth required pam_faillock.so preauth silent audit deny=5 unlock_time=900
auth required pam_faillock.so authfail audit deny=5 unlock_time=900
# 移除不必要帳號
userdel games; userdel gopher; userdel lp ...
groupdel games; groupdel uucp ...
# 預設拒絕
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 允許本地、已建立連線、ICMP 基礎型別
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
# 允許 SSH (修改後埠)
iptables -A INPUT -p tcp --dport 2222 -j ACCEPT
# 允許業務埠 (80/443 等)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 記錄拒絕封包
iptables -A INPUT -j LOG --log-prefix "IPT-DROP: "
iptables -A INPUT -j DROP
# /etc/hosts.allow
sshd: 192.168.1.0/24 10.0.0.5
vsftpd: 192.168.1.0/24
# /etc/hosts.deny
sshd: ALL
vsftpd: ALL
auditd 監控關鍵檔案變更、權限提升、系統呼叫AIDE / Tripwire 定期掃描檔案變更rsyslog + logrotate → ELK/Splunk/GraylogOSSEC/Wazuh (HIDS)、Suricata (NIDS)OpenVAS、Lynis 定期基線掃描
oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_stig /usr/share/xml/scap/ssg/content/ssg-centos7-ds.xmldev-sec.hardening, konstruktoid.hardeninglynis audit system 快速健康檢查安全是過程而非產品。建立基線、自動化合規、持續監控、快速響應,才是長久之計。