# SSH 加固:5 步鎖死你部 Linux Server,由 Port 22 到 Fail2Ban 全方位防禦
成日聽人講「俾人爆咗 Server」,其實十次有九次都係 SSH 俾人 brute force。尤其係放咗上 Cloud 嗰啲 VM,一開 port 22 就成日有人撞 password。今日同大家分享 SSH 加固嘅 5 個必做步驟,由基本到進階,一步步鎖死你部 Linux Server。
👉 延伸閱讀:Linux Server 安全加固 6 步搞掂 — Defense in Depth 實戰指南
## SSH 加固第一步:棄用 Password,只用 SSH Key
SSH 加固最基本亦係最重要嘅一步,就係**停用密碼登入**。Password 無論幾複雜都有機會俾人 brute force 撞中,但 SSH key pair(RSA 4096-bit 或 Ed25519)基本上冇可能俾人 crack。
首先喺你部 Local 機 generate SSH key:
ssh-keygen -t ed25519 -C "your_email@example.com"
然後將 public key copy 去 Server:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@your-server-ip
入到 Server 之後,編輯 `/etc/ssh/sshd_config`:
sudo nano /etc/ssh/sshd_config
修改以下設定:
PasswordAuthentication no
PubkeyAuthentication yes
ChallengeResponseAuthentication no
Save 完 restart SSH service:
sudo systemctl restart sshd
**⚠️ 重要:** restart 之前一定要 confirm 你已經成功用 key login 到!開多個 terminal session 試清楚先,否則 lock 死自己就喊都無謂。
## SSH 加固第二步:換走 Default Port 22
SSH 加固嘅第二步係換 port。雖然 security by obscurity 唔係終極方案,但換走 port 22 可以擋走 90% 以上嘅 automated scanning bots。Bot 只會 scan default port,你換咗 port 佢哋就搵你唔到。
編輯 `/etc/ssh/sshd_config`:
Port 2222
同時記住 update firewall:
sudo ufw allow 2222/tcp
sudo ufw delete allow 22/tcp
如果你用 AWS/Azure/GCP,記住都要 update Security Group / NSG rule。唔好換完 port 之後自己都入唔到!
## SSH 加固第三步:禁用 Root Login
直接用 root login 係極之危險嘅做法。SSH 加固要徹底,root login 一定要 disable。所有 admin 操作都應該經 sudo user 做。
`/etc/ssh/sshd_config`:
PermitRootLogin no
建立一個 regular user 並加入 sudo group:
sudo adduser adminuser
sudo usermod -aG sudo adminuser
咁樣就算有人知道你 root password,佢都冇辦法直接 SSH 入嚟做野。
## SSH 加固第四步:Fail2Ban 自動封鎖攻擊者
Fail2Ban 係 SSH 加固嘅自動化防線。佢會 mon 住 log file,一旦 detect 到有 IP 不斷 fail login,就會自動用 iptables ban 咗佢一段時間。
安裝 Fail2Ban:
sudo apt update && sudo apt install fail2ban -y
建立 local config:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
編輯 `/etc/fail2ban/jail.local`,重點設定:
[sshd]
enabled = true
port = 2222
maxretry = 3
bantime = 3600
findtime = 600
以上設定意思係:10 分鐘內 fail 3 次就 ban 1 個鐘。你可以根據需要調整 bantime,我見過有人 set 86400(24 小時)甚至 -1(永久 ban)。
Start 同 enable Fail2Ban:
sudo systemctl enable fail2ban --now
Check status:
sudo fail2ban-client status sshd
你會見到 banned IP list,通常一開冇耐就會有人撞,證明呢個世界幾咁危險。
## SSH 加固第五步:Hardening SSH Config(最終防線)
最後一步係 fine-tune SSH config,做埋 SSH 加固嘅終極設定。以下係我推薦嘅 production-grade settings:
# 只用 SSH Protocol 2(Protocol 1 有已知漏洞)
Protocol 2
# Maximum authentication attempts
MaxAuthTries 3
# Disable empty passwords
PermitEmptyPasswords no
# Disable X11 forwarding(除非你真係需要用)
X11Forwarding no
# Login grace time — 太耐唔 authenticate 就 disconnect
LoginGraceTime 30
# Maximum concurrent unauthenticated connections
MaxStartups 3:50:10
# Allow only specific users(optional,限制邊個可以 SSH)
AllowUsers adminuser devops
Apply 設定:
sudo sshd -t # test config 有冇 syntax error
sudo systemctl restart sshd
## SSH 加固 Bonus:用 SSH Config File 管理連線
成日打 IP 打到傻?SSH 加固唔止係 security,方便管理都係一部分。用 `~/.ssh/config`:
Host prod-web
HostName 203.0.113.10
Port 2222
User adminuser
IdentityFile ~/.ssh/id_ed25519
Host dev-db
HostName 10.0.1.50
Port 2222
User devops
IdentityFile ~/.ssh/id_ed25519
之後只需要打:
ssh prod-web
搞掂!又快又安全。
## SSH 加固總結
SSH 加固係每個 system admin 嘅基本功。以上 5 步由淺入深,由換 key 到 Fail2Ban 再到 hardening config,做到晒基本上 99% 嘅 attack 都同你冇關係。記住:security 唔係做一次就完,係要持續 audit 同 update。
**TL;DR SSH 加固 Checklist:**
– ✅ 停 password login,只用 SSH key (Ed25519)
– ✅ 換走 default port 22
– ✅ Disable root login
– ✅ 裝 Fail2Ban 自動封鎖 brute force
– ✅ Hardening sshd_config 各項參數
– ✅ 定期 check auth log:`sudo tail -f /var/log/auth.log`
如果你仲係用緊 password + port 22 + root login,拿拿聲跟住以上 SSH 加固步驟做一次。唔係講笑,而家 internet 上面 scanning bot 嘅密度比你想象中高好多,你部機一上線幾分鐘就會有人撞你。
🔗 參考資料:NVD NIST 漏洞資料庫
> 📌 延伸閱讀:想學更多 Linux security?睇埋 [Docker 容器安全掃描實戰:用 Trivy 幫你啲 Container 做體檢](https://molious.com/docker-container-security-trivy/) ,由 container 層面幫你鎖死成個 stack。
> 📌 延伸閱讀:想學更多 Linux security?睇埋我哋之前寫嘅 Docker 容器安全掃描實戰,由 container 層面幫你鎖死成個 stack。
#SSH #Linux安全 #伺服器管理 #Fail2Ban #資安加固



