萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 系統安全加固實戰清單:從最小化安裝到核心參數調優

「沒有絕對安全的系統,只有持續加固的過程。」本文提供從安裝部署到運行維護的分層加固清單,涵蓋磁碟分區、服務精簡、權限最小化、核心參數、審計日誌等關鍵面向。

階段零:安裝前規劃

  • 用途界定:Web 伺服器? 資料庫? 內部跳板機? 決定安裝什麼套件
  • 威脅建模:面對外網? 內網? 處理機敏資料? 合規要求 (等保、GDPR、PCI-DSS)?
  • 分區策略:/、/boot、/var、/tmp、/home、/opt 分區隔離,/tmp 掛載 noexec,nodev,nosuid
  • 最小化安裝:僅勾選「最小安裝」+ 必需套件組,拒絕圖形介面

階段一:安裝後基線加固

1. BIOS/UEFI 與開機鎖定

# 設定 BIOS 密碼、禁用 USB/網路開機、啟用 Secure Boot
# GRUB 設定密碼
grub2-setpassword
# 產生 /boot/grub2/user.cfg

2. 核心參數調優 (/etc/sysctl.d/99-hardening.conf)

# 網路層防護
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

3. 服務精簡 (systemd)

# 列出啟用服務
systemctl list-unit-files --state=enabled

# 停用非必要服務 (視用途而定)
systemctl disable --now postfix cups avahi-daemon bluetooth \
  rpcbind nfs-server chronyd (若用 ntpd) ...

# 檢查監聽埠
ss -tulnp

4. 檔案系統權限與加密

  • 關鍵設定檔 chmod 600 + chattr +i
  • 敏感資料分區掛載 nodev,nosuid,noexec (視應用相容性)
  • 全盤加密 (LUKS) 或目錄級加密 (eCryptfs/fscrypt)

5. 使用者與認證強化

# 密碼策略 /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 ...

階段二:網路邊界防護

1. iptables/nftables 基線規則

# 預設拒絕
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

2. TCP Wrappers 第二層防線 (/etc/hosts.allow, /etc/hosts.deny)

# /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/Graylog
  • 入侵偵測:OSSEC/Wazuh (HIDS)、Suricata (NIDS)
  • 漏洞掃描:OpenVAS、Lynis 定期基線掃描

系統加固流程圖

自動化加固工具

  • OpenSCAP:oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_stig /usr/share/xml/scap/ssg/content/ssg-centos7-ds.xml
  • Ansible Hardening Roles:dev-sec.hardening, konstruktoid.hardening
  • Lynis:lynis audit system 快速健康檢查

安全是過程而非產品。建立基線、自動化合規、持續監控、快速響應,才是長久之計。

copyright © 萬盛學電腦網 all rights reserved