/etc/ssh/sshd_config # SSH 服務端配置
/etc/ssh/ssh_config # SSH 客戶端配置
每次修改後需重啟:systemctl restart sshd 或 /etc/init.d/ssh restart
# sshd_config
Port 2222 # 或任意 1024-65535 未用埠
# 效果:避開自動化掃描腳本(多數僅掃描 22 埠)
Protocol 2
# 禁用有中間人攻擊風險的 SSH-1
# 禁用 Root 直接登入
PermitRootLogin no
# 僅允許特定用戶
AllowUsers anze dasa kimy
# 或群組
AllowGroups sshusers
# 拒絕特定用戶
DenyUsers guest test
DenyGroups nogroup
# 建立橫幅檔
cat > /etc/ssh-banner.txt << 'EOF'
********************************************
* This is a private SSH service. *
* Unauthorized access is strictly prohibited.*
* All activities are monitored and logged. *
********************************************
EOF
# sshd_config
Banner /etc/ssh-banner.txt
# 1. 客戶端生成金鑰對(推薦 Ed25519)
ssh-keygen -t ed25519 -C "user@host"
# 或傳統 DSA
ssh-keygen -t dsa
# 2. 部署公鑰到伺服器
ssh-copy-id -p 2222 user@server
# 或手動:
cat ~/.ssh/id_ed25519.pub | ssh -p 2222 user@server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
# 3. 伺服器強制金鑰認證
PubkeyAuthentication yes
PasswordAuthentication no
AuthenticationMethods publickey
# 4. 關鍵權限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
# /etc/hosts.deny - 預設拒絕
sshd: ALL
# /etc/hosts.allow - 例外允許
sshd: 192.168.1.0/255.255.255.0 193.180.177.13
sshd: .trusted.domain.com
# 效果:不匹配 hosts.allow 的連線在認證前即被切斷
# 僅允許特定管理 IP
iptables -A INPUT -p tcp -s 193.180.177.13 --dport 2222 -j ACCEPT
iptables -A INPUT -p tcp --dport 2222 -j DROP
# 連線頻率限制(防暴力破解)
# 每分鐘最多 1 次新連線
iptables -A INPUT -p tcp --dport 2222 -m state --state NEW -m limit --limit 1/minute --limit-burst 1 -j ACCEPT
iptables -A INPUT -p tcp --dport 2222 -m state --state NEW -j DROP
# 特定 IP 寬鬆限制
iptables -A INPUT -p tcp -s 193.180.177.13 --dport 2222 -m limit --limit 5/minute --limit-burst 3 -j ACCEPT
# 僅允許工作時間 09:00-18:00 連線
iptables -A INPUT -p tcp --dport 2222 -m time --timestart 09:00 --timestop 18:00 --days Mon,Tue,Wed,Thu,Fri -j ACCEPT
iptables -A INPUT -p tcp --dport 2222 -j DROP
# 需 kernel 支援 xt_time 模組
# 安裝
yum install fail2ban / apt install fail2ban
# 配置 /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
findtime = 600
action = iptables-multiport[name=sshd, port="2222", protocol=tcp]
# 啟用
systemctl enable --now fail2ban
# 查看封鎖狀態
fail2ban-client status sshd
網路層: 邊界防火牆 → 雲端 WAF/DDoS 防護
主機層: iptables/nftables 埠口/頻率/時間控制
服務層: TCP Wrappers 主機白名單
SSH 層: 非標準埠 + 協議 v2 + 禁用 Root + 僅金鑰認證 + 允許用戶白名單
應用層: Fail2ban 行為分析自動封鎖 + 登入橫幅法律告知
審計層: auditd 關鍵系統呼叫 + 日誌集中化 (ELK/SIEM) + 即時告警
#!/bin/bash
# ssh_audit.sh
echo "=== SSH Security Audit ==="
sshd -T | grep -E '^port|^protocol|^permitrootlogin|^passwordauthentication|^pubkeyauthentication|^maxauthtries|^clientaliveinterval|^banner|^allowusers|^denyusers'
echo ""
echo "=== Listening Ports ==="
ss -tlnp | grep sshd
echo ""
echo "=== Fail2ban Status ==="
fail2ban-client status sshd 2>/dev/null || echo "Fail2ban not running"
echo ""
echo "=== Recent Failed Logins ==="
grep "Failed password" /var/log/auth.log | tail -10
這九大策略層層疊加,從網路層到應用層構建纖深防禦。單一措施皆可被繞過,組合使用才能大幅提高攻擊成本。記住:安全是過程,而非產品。