伺服器是網路安全的核心資產。Linux 雖具開源透明、社群修補快等優勢,但**配置不當才是最大漏洞來源**。本文提供生產環境就緒的安全加固清單,涵蓋啟動/登入、帳號/認證、檔案系統、網路存取、服務最小化、入侵偵測六大維度,附指令腳本與驗證方法。
systemctl mask ctrl-alt-del.target (systemd)/etc/inittab 註解 ca::ctrlaltdel:/sbin/shutdown -t3 -r now (SysVinit)/etc/ssh/sshd_config → PermitRootLogin nosudo 提權,配置 /etc/sudoers (visudo) 最小權限原則passwd -l root (以 sudo 管理)PASS_MAX_DAYS 90
PASS_MIN_DAYS 7
PASS_MIN_LEN 12
PASS_WARN_AGE 14
# pwquality: minlen=12 dcredit=-1 ucredit=-1 ocredit=-1 lcredit=-1 retry=3
# 查閱無登入殼層帳號
awk -F: '($7 !~ /nologin|false/ && $3 >= 1000) {print $1}' /etc/passwd
# 刪除預設服務帳號 (games、gopher、ftp 等若未使用)
userdel -r games; userdel -r gopher; groupdel games; groupdel gopher
chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/sudoers
# 需修改時:chattr -i ; 修改後 chattr +i
# /etc/pam.d/su
auth required pam_wheel.so use_uid group=wheel
# 將管理員加入 wheel 群組
usermod -aG wheel adminuser
Port 2222 # 非標準埠減少掃描噪音
Protocol 2
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no # 強制金鑰認證
PermitEmptyPasswords no
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers adminuser@192.168.1.0/24 # 限制用戶與來源 IP
Banner /etc/ssh/banner.txt # 法律警告橫幅
# 移除 /etc/issue、/etc/issue.net、/etc/motd 中的版本資訊
> /etc/issue; > /etc/issue.net; > /etc/motd
# 或自訂最小化提示
echo "Authorized access only. All activities monitored." > /etc/issue
| 掛載選項 | 適用分區 | 效果 |
|---|---|---|
| noexec | /tmp、/var/tmp、/dev/shm、/home | 禁止執行二進制檔,防惡意腳本落地執行 |
| nosuid | /tmp、/home、/var | 忽略 SUID/SGID 位元,防權限提升 |
| nodev | /tmp、/home、/var | 禁止裝置檔,防建立字元/區塊裝置 |
| noatime | /、/home、/var | 不更新存取時間,減少 I/O、延長 SSD 壽命 |
# /etc/fstab 範例
tmpfs /tmp tmpfs defaults,noexec,nosuid,nodev,size=2G 0 0
/dev/sda2 /home ext4 defaults,noexec,nosuid,nodev,noatime 0 2
# /etc/hosts.deny
ALL: ALL
# /etc/hosts.allow
sshd: 192.168.1.0/255.255.255.0, 10.0.0.5
vsftpd: 192.168.1.0/24
# 驗證
tcpdchk -v
# 僅開放必要埠口
iptables -A INPUT -p tcp --dport 2222 -s 192.168.1.0/24 -j ACCEPT # SSH
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # HTTP
iptables -A INPUT -p tcp --dport 443 -j ACCEPT # HTTPS
# 其餘預設拒絕 (見上節完整腳本)
# 持久化
service iptables save # 或 iptables-save > /etc/sysconfig/iptables
# /etc/default/grub
GRUB_CMDLINE_LINUX="ipv6.disable=1"
update-grub
# 或 sysctl
echo "net.ipv6.conf.all.disable_ipv6=1" >> /etc/sysctl.conf
sysctl -p
# 防 IP 欺騙
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# 忽略 ICMP 重定向
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
# 忽略 ICMP 回應請求 (可選,影響 ping)
# net.ipv4.icmp_echo_ignore_all = 1
# 忽略廣播 ICMP
net.ipv4.icmp_echo_ignore_broadcasts = 1
# 記錄異常封包 (Martians)
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1
# SYN Flood 防護
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
# 禁用來源路由
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
sysctl --system # 套用
systemctl disable --now cups avahi-daemon bluetooth postfix nfs-server rpcbindss -tunlp 對照授權清單auditctl -w /etc/passwd -p wa -k passwd_changesauditctl -w /etc/sudoers -p wa -k sudoers_changes| 工具 | 類型 | 部署建議 |
|---|---|---|
| Fail2ban | HIDS (日誌分析+防火牆封鎖) | 保護 SSH、vsftpd、Postfix、Dovecot、Apache 登入失敗封鎖 |
| RKHunter / Chkrootkit | Rootkit 掃描 | 每日排程掃描,核心模組、二進制檔、網路介面異常檢測 |
| Lynis | 安全稽核 | 每週執行 lynis audit system,產生加固建議報告 |
| OSSEC / Wazuh | HIDS 平台 | 企業級:檔案完整性、日誌分析、Rootkit 檢測、主動回應、合規儀表板 |
# 1. 確認 SSH 設定
sshd -T | grep -E 'permitrootlogin|passwordauthentication|pubkeyauthentication'
# 2. 確認開放埠口
ss -tunlp | grep LISTEN
# 3. 確認核心參數
sysctl net.ipv4.tcp_syncookies net.ipv4.conf.all.rp_filter net.ipv4.icmp_echo_ignore_broadcasts
# 4. 確認檔案屬性
lsattr /etc/passwd /etc/shadow /etc/sudoers
# 5. 確認 sudo 規則
sudo -l -U adminuser
# 6. 確認 auditd 規則
auditctl -l
# 7. 掃描 Rootkit
rkhunter --check --sk
# 8. 安全稽核
lynis audit system --quick
完成上述加固後,您的 Linux 伺服器已具備**企業級基線防護能力**。但請記住:
**「永無止境的加固,才是最好的防禦。」**