Linux 伺服器安全加固:10 個必做步驟防止黑客入侵

Linux 伺服器安全加固:10 個必做步驟防止黑客入侵 做 IT 嘅你一定知,Linux 伺服器係企業 infrastructure 嘅 backbone。但好多 admin 裝完 OS 就即刻 deploy 應用,忽略咗基本嘅安全加固(hardening)。今日同大家分享 10 個 Linux 伺服器安全加固必做步驟,由淺入深,跟住做就可以大幅降低被入侵嘅風險。 👉 延伸閱讀:Linux Se…

Linux 伺服器安全加固:10 個必做步驟防止黑客入侵

做 IT 嘅你一定知,Linux 伺服器係企業 infrastructure 嘅 backbone。但好多 admin 裝完 OS 就即刻 deploy 應用,忽略咗基本嘅安全加固(hardening)。今日同大家分享 10 個 Linux 伺服器安全加固必做步驟,由淺入深,跟住做就可以大幅降低被入侵嘅風險。

👉 延伸閱讀:Linux Server 安全加固 6 步搞掂 — Defense in Depth 實戰指南

Linux 伺服器安全加固第一步:更新系統同關閉不必要服務

第一步永遠係保持系統更新。黑客最鍾意攻擊已知漏洞,而 patch management 就係最基本嘅防線。

# Debian/Ubuntu
sudo apt update && sudo apt upgrade -y

# RHEL/CentOS/Rocky sudo dnf update -y

# 啟用自動安全更新 sudo apt install unattended-upgrades -y sudo dpkg-reconfigure --priority=low unattended-upgrades

更新完之後,check 下有咩 service 行緊,關閉唔需要嘅:

# 列出所有 listening ports
sudo ss -tlnp

# 停用唔需要嘅 service sudo systemctl disable --now cups bluetooth avahi-daemon

Linux 伺服器安全加固第二步:SSH 安全配置

SSH 係最常見嘅攻擊入口。預設 port 22 日日俾 botnet scan,一定要改。

sudo vim /etc/ssh/sshd_config

# 修改以下設定 Port 2222 # 改非標準 port PermitRootLogin no # 禁止 root 直接登入 PasswordAuthentication no # 只用 key-based auth MaxAuthTries 3 # 最多試 3 次 ClientAliveInterval 300 # 5 分鐘無反應就 timeout

sudo systemctl restart sshd

Linux 伺服器安全加固第三步:防火牆配置(iptables / nftables)

用 iptables 或 nftables 建立白名單式防火牆規則,只開放必要 port。

# 用 ufw 簡化管理(Ubuntu/Debian)
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2222/tcp    # SSH
sudo ufw allow 80/tcp      # HTTP
sudo ufw allow 443/tcp     # HTTPS
sudo ufw enable

# RHEL 系列用 firewalld sudo firewall-cmd --permanent --add-service=ssh sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload

Linux 伺服器安全加固第四步:Fail2Ban 防暴力破解

Fail2Ban 會自動 ban 走重複登入失敗嘅 IP,係防 brute-force 攻擊嘅必備工具。

sudo apt install fail2ban -y

# 建立 local config sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

# 編輯 jail.local,啟用 sshd jail [sshd] enabled = true port = 2222 maxretry = 3 bantime = 3600 findtime = 600

sudo systemctl enable --now fail2ban

# Check status sudo fail2ban-client status sshd

Linux 伺服器安全加固第五步:強制密碼政策同權限管理

就算用 key-based SSH,系統入面嘅 user account 都要有嚴格密碼政策。

# 設定密碼過期政策
sudo chage -M 90 -m 7 -W 7 username

# 修改 /etc/security/pwquality.conf minlen = 12 minclass = 3 maxrepeat = 3

# 檢查 sudo 權限 sudo visudo # 確保只有必要 user 有 sudo 權限

Linux 伺服器安全加固第六步:檔案系統安全同審計

用 auditd 記錄關鍵檔案嘅改動,同埋設定適當嘅 file permission。

# 安裝 auditd
sudo apt install auditd -y

# 監控 /etc/passwd 同 /etc/shadow 改動 sudo auditctl -w /etc/passwd -p wa -k passwd_changes sudo auditctl -w /etc/shadow -p wa -k shadow_changes sudo auditctl -w /etc/ssh/sshd_config -p wa -k sshd_config

# 檢查 world-writable files sudo find / -type f -perm -o+w 2>/dev/null | grep -v proc

# 檢查 SUID/SGID files sudo find / -perm -4000 -o -perm -2000 2>/dev/null

Linux 伺服器安全加固第七步:核心參數安全調校(sysctl)

Linux kernel 有好多 network security 相關參數可以調校,防止常見嘅 network attack。

sudo vim /etc/sysctl.d/99-security.conf

# 防止 IP spoofing net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1

# 忽略 ICMP redirects net.ipv4.conf.all.accept_redirects = 0 net.ipv6.conf.all.accept_redirects = 0

# 防止 SYN flood net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_syn_backlog = 2048

# 記錄 martian packets net.ipv4.conf.all.log_martians = 1

sudo sysctl --system

Linux 伺服器安全加固第八步:Log 管理同集中監控

冇 log 就等於盲咗。確保 log 有記錄、有 rotation、有 backup。

# 檢查 rsyslog 行緊
sudo systemctl status rsyslog

# 設定 logrotate sudo vim /etc/logrotate.d/custom /var/log/custom/*.log { daily rotate 30 compress delaycompress missingok notifempty }

# 用 journald 持久化儲存 sudo mkdir -p /var/log/journal sudo systemd-tmpfiles --create --prefix /var/log/journal

Linux 伺服器安全加固第九步:AppArmor / SELinux 強制存取控制

MAC(Mandatory Access Control)係最後一道防線。就算 application 俾人 hack,MAC 都可以限制 damage。

# Ubuntu/Debian 用 AppArmor
sudo aa-status
sudo apt install apparmor-profiles apparmor-utils -y
sudo aa-enforce /etc/apparmor.d/*

# RHEL/CentOS 用 SELinux sestatus sudo setenforce 1 # 永久啟用:edit /etc/selinux/config → SELINUX=enforcing

Linux 伺服器安全加固第十步:定期漏洞掃描同備份

最後一步係建立持續性嘅安全流程。定期 scan、定期 backup、定期 review。

# Lynis 安全審計工具
sudo apt install lynis -y
sudo lynis audit system

# 自動備份 script(cron job) 0 2 * * * /usr/local/bin/backup.sh

# backup.sh 內容 #!/bin/bash tar -czf /backup/etc-$(date +%Y%m%d).tar.gz /etc mysqldump --all-databases | gzip > /backup/db-$(date +%Y%m%d).sql.gz # rsync 去 remote server rsync -avz /backup/ backup@remote:/backup/

總結:Linux 伺服器安全加固係持續過程

以上 10 個 Linux 伺服器安全加固步驟,由基本嘅系統更新、SSH 配置、防火牆,到進階嘅 auditd、MAC、漏洞掃描,涵蓋咗伺服器安全嘅各個層面。記住,安全唔係一次性嘅 project,而係持續嘅 process。建議每個月做一次 Lynis audit,每季 review 一次 firewall rules,咁先可以保持伺服器長期安全。

如果你覺得呢篇文有用,記得 bookmark 同 share 俾同事!有咩問題歡迎留言討論。

#Linux #技術分享 #監控 #IT教學 #安全