防止單用戶模式繞過認證。GRUB2 配置:
# /etc/grub.d/40_custom
set superusers="admin"
password_pbkdf2 admin grub.pbkdf2.sha512.10000.XXXXXXXX # 使用 grub-mkpasswd-pbkdf2 生成
# 更新配置
grub2-mkconfig -o /boot/grub2/grub.cfg
舊版 LILO 需設置 password= 並 chmod 600 /etc/lilo.conf,但 LILO 已淘汰。
# /etc/login.defs
PASS_MAX_DAYS 90 # 密碼最大使用天數
PASS_MIN_DAYS 7 # 密碼最小修改間隔
PASS_MIN_LEN 12 # 最小長度 (建議 12+)
PASS_WARN_AGE 14 # 過期前警告天數
# /etc/pam.d/system-auth (或 password-auth)
password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= minlen=12 dcredit=-1 ucredit=-1 ocredit=-1 lcredit=-1
# 要求:至少 1 數字、1 大寫、1 特殊字符、1 小寫
# /etc/profile.d/autologout.sh (需可執行權限)
readonly TMOUT=600 # 10 分鐘無操作自動退出登錄 Shell
export TMOUT
對於 SSH 會話,另需配置 /etc/ssh/sshd_config:ClientAliveInterval 300、ClientAliveCountMax 2。
chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/sudoers /etc/ssh/sshd_config /etc/fstab
# 查看屬性
lsattr /etc/passwd
# 臨時解除修改
chattr -i /etc/passwd
# 修改後恢復
chattr +i /etc/passwd
防止 Rootkit 竄改關鍵配置,即使 root 也需先移除屬性。
# /etc/hosts.deny (默認拒絕所有)
ALL: ALL
# /etc/hosts.allow (白名單放行)
sshd: 192.168.1.0/255.255.255.0 10.0.0.5
vsftpd: .example.com
# 驗證
tcpdchk -v
注意:僅對鏈接 libwrap 的服務生效 (如 sshd、vsftpd、portmap),現代服務多用 firewall/iptables/nftables。
# /etc/profile
export HISTSIZE=1000 # 內存中保留條數
export HISTFILESIZE=2000 # 文件中保留條數
export HISTCONTROL=ignoredups:erasedups # 忽略重複、擦除重複
export HISTIGNORE="ls:cd:pwd:exit:history" # 忽略常用無敏感命令
# 登出時清空歷史 (可選,/etc/skel/.bash_logout)
rm -f ~/.bash_history
# 查找所有 SUID/SGID 文件
find / -type f \( -perm -4000 -o -perm -2000 \) -ls 2>/dev/null
# 移除不需要的 SUID (如不需要 ping 的 suid)
chmod u-s /bin/ping # 現代內核支援 CAP_NET_RAW capability 替代
# 保留必要的:su, sudo, passwd, mount, umount, ping (如需)、crontab
# /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
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv6.conf.all.disable_ipv6 = 1 # 若不使用 IPv6
# 文件系統
fs.suid_dumpable = 0 # 禁止 SUID 程序產生 core dump
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
# 內核
kernel.kptr_restrict = 2 # 隱藏內核符號地址
kernel.dmesg_restrict = 1 # 限制 dmesg 訪問
kernel.yama.ptrace_scope = 1 # 限制 ptrace (僅父進程可調試)
# 應用
sysctl --system
# 安裝 auditd
auditctl -w /etc/passwd -p wa -k passwd_changes # 監聽寫入/屬性變更
auditctl -w /etc/shadow -p wa -k shadow_changes
auditctl -w /etc/sudoers -p wa -k sudoers_changes
auditctl -w /bin/su -p x -k su_exec
# 查看報告
aureport --auth --summary
ausearch -k passwd_changes
# 列出啟用服務
systemctl list-unit-files --state=enabled
# 禁用無用服務
systemctl disable --now postfix cups avahi-daemon bluetooth # 視業務而定
# firewalld 白名單策略
firewall-cmd --permanent --set-default-zone=drop
firewall-cmd --permanent --zone=trusted --add-source=192.168.1.0/24
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-port=80/tcp --add-port=443/tcp
firewall-cmd --reload