Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 系統安全加固 24 條實戰軍規:從物理到應用層纵深防禦

編輯:Linux 教學

Linux 預設配置並不等於安全。本文梳理 24 條經過生產環境驗證的加固措施,涵蓋物理、存儲、服務、網絡、訪問控制、審計、內核等七大維度,助你構建纵深防禦體系。

一、 物理與啟動安全

1. BIOS/UEFI 加鎖

  • 禁用 CD/DVD/USB/軟驅啟動順序。
  • 設置 BIOS 管理員密碼,防篡改啟動項。

2. GRUB 引導加密

# 生成加密密碼
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

二、 磁盤分區與文件系統

3. 關鍵目錄獨立分區

分區掛載點效益
/根核心系統
/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

三、 服務最小化

4. 只裝必需包

# 列出運行級別 3 啟動的服務
chkconfig --list | grep '3:on'
# 關閉無用服務
chkconfig serviceName off
# 卸載無用 RPM
yum remove package-name
# 或 dpkg/apt
apt-get remove package-name

四、 網絡攻擊面收斂

5. 監聽端口審計

netstat -tulpn
ss -tulpn  # 現代替代

關閉非業務端口對應服務。

6. SSH 強化配置 /etc/ssh/sshd_config

Port 2222              # 修改默認端口
PermitRootLogin no     # 禁用 root 登錄
PubkeyAuthentication yes
PasswordAuthentication no
PermitEmptyPasswords no
Protocol 2
AllowUsers admin ops   # 白名單用戶
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2

重啟生效:systemctl restart sshd。

五、 补丁與內核加固

7. 保持系統最新

yum update -y
# 或
apt update && apt upgrade -y

訂閱安全郵件列表,關鍵漏洞 24 小時內修補。

8. 內核參數加固 /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.exec-shield = 1
kernel.randomize_va_space = 2
fs.suid_dumpable = 0

應用:sysctl --system。

六、 訪問控制與認證

9. Cron 任務鎖定

echo "ALL" >> /etc/cron.deny
# 僅允許特定用戶
echo "root" > /etc/cron.allow
echo "backup" >> /etc/cron.allow

10. 禁用 USB 存儲

echo "install usb-storage /bin/true" > /etc/modprobe.d/no-usb.conf
# 即時生效
modprobe -r usb_storage

11. 啟用 SELinux Enforcing 模式

getenforce
setenforce 1
# 永久
sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config

12. 移除桌面環境(服務器)

# 修改運行級別
sed -i 's/^id:.*:initdefault:/id:3:initdefault:/' /etc/inittab
# 或徹底卸載
yum groupremove "X Window System" "GNOME Desktop Environment" "KDE Desktop Environment"

13. 關閉 IPv6(不使用時)

echo "NETWORKING_IPV6=no" >> /etc/sysconfig/network
echo "IPV6INIT=no" >> /etc/sysconfig/network

14. 密碼策略強化 /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

15. 密碼過期策略

# 查看
chage -l username
# 設置:最長 60 天、最短 7 天、過期前 7 天警告
chage -M 60 -m 7 -W 7 username

七、 審計與入侵檢測

16. 審計系統 auditd

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

17. 文件完整性監控 AIDE


yum install aide
aide --init
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
# 定時檢查腳本加入 cron

18. 日志集中化與不可篡改

  • 配置 rsyslog 轉發至日志中心。
  • chattr +a /var/log/secure /var/log/audit/audit.log 僅追加屬性。

八、 進階防禦

19. 入侵檢測 Tripwire / Samhain

20. 強制訪問控制 AppArmor (Ubuntu/Debian) / SELinux (RHEL)

21. 容器/虛擬化隔離業務

22. 定期滲透測試與漏洞掃描

23. 應急響應預案與演練

24. 安全基線自動化檢查


# 使用 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 基礎設施。

Copyright © Windows教程網 All Rights Reserved