Linux 預設配置並不等於安全。本文梳理 24 條經過生產環境驗證的加固措施,涵蓋物理、存儲、服務、網絡、訪問控制、審計、內核等七大維度,助你構建纵深防禦體系。
# 生成加密密碼
grub2-mkpasswd-pbkdf2
# 輸出寫入 /etc/grub.d/40_custom
set superusers="root"
password_pbkdf2 root grub.pbkdf2.sha512.10000.xxx
# 重新生成配置
grub2-mkconfig -o /boot/grub2/grub.cfg
| 分區 | 掛載點 | 效益 |
|---|---|---|
| / | 根 | 核心系統 |
| /boot | /boot | 內核文件保護,防填滿 |
| /usr | /usr | 只讀掛載選項可用 |
| /var | /var | 日志/郵件/數據庫隔離 |
| /home | /home | 用戶數據隔離,可加 quotas |
| /tmp | /tmp | 掛載選項 noexec,nosuid,nodev |
| /opt | /opt | 第三方軟件隔離 |
掛載選項範例 /etc/fstab:
/dev/sda7 /tmp ext4 defaults,noexec,nosuid,nodev 0 0
# 列出運行級別 3 啟動的服務
chkconfig --list | grep '3:on'
# 關閉無用服務
chkconfig serviceName off
# 卸載無用 RPM
yum remove package-name
# 或 dpkg/apt
apt-get remove package-name
netstat -tulpn
ss -tulpn # 現代替代
關閉非業務端口對應服務。
/etc/ssh/sshd_configPort 2222 # 修改默認端口
PermitRootLogin no # 禁用 root 登錄
PubkeyAuthentication yes
PasswordAuthentication no
PermitEmptyPasswords no
Protocol 2
AllowUsers admin ops # 白名單用戶
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
重啟生效:systemctl restart sshd。
yum update -y
# 或
apt update && apt upgrade -y
訂閱安全郵件列表,關鍵漏洞 24 小時內修補。
/etc/sysctl.d/99-hardening.confnet.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.exec-shield = 1
kernel.randomize_va_space = 2
fs.suid_dumpable = 0
應用:sysctl --system。
echo "ALL" >> /etc/cron.deny
# 僅允許特定用戶
echo "root" > /etc/cron.allow
echo "backup" >> /etc/cron.allow
echo "install usb-storage /bin/true" > /etc/modprobe.d/no-usb.conf
# 即時生效
modprobe -r usb_storage
getenforce
setenforce 1
# 永久
sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
# 修改運行級別
sed -i 's/^id:.*:initdefault:/id:3:initdefault:/' /etc/inittab
# 或徹底卸載
yum groupremove "X Window System" "GNOME Desktop Environment" "KDE Desktop Environment"
echo "NETWORKING_IPV6=no" >> /etc/sysconfig/network
echo "IPV6INIT=no" >> /etc/sysconfig/network
/etc/pam.d/system-auth (RHEL) / /etc/pam.d/common-password (Debian)# 禁止重用最近 5 個密碼
password sufficient pam_unix.so remember=5
# 複雜度
password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= minlen=12 lcredit=-1 ucredit=-1 dcredit=-1 ocredit=-1
# 查看
chage -l username
# 設置:最長 60 天、最短 7 天、過期前 7 天警告
chage -M 60 -m 7 -W 7 username
yum install audit
systemctl enable --now auditd
# 關鍵規則 /etc/audit/rules.d/audit.rules
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k scope
-a always,exit -F arch=b64 -S execve -k exec
yum install aide
aide --init
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
# 定時檢查腳本加入 cron
rsyslog 轉發至日志中心。chattr +a /var/log/secure /var/log/audit/audit.log 僅追加屬性。
# 使用 OpenSCAP
yum install openscap-scanner
oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_standard --results results.xml /usr/share/xml/scap/ssg/content/ssg-centos7-ds.xml
安全是過程而非產品。將上述 24 條納入基線模板,配合 CI/CD 流水線自動化驗證,定期複盤演練,方能構建可信的 Linux 基礎設施。