# 統計 SSH 登入失敗 IP 及次數
grep "Failed password" /var/log/secure | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr | head -20
# 輸出範例:
# 1245 192.168.1.100
# 320 203.0.113.45
# 若出現大量失敗記錄,說明正在遭受暴力破解
vim /etc/ssh/sshd_config
# 找到 #Port 22,改為
Port 2222
# 保留 Port 22 一行可暫時雙開,驗證無誤後註釋掉
systemctl restart sshd
# 之後連接:ssh -p 2222 user@host
/etc/security/pwquality.conf 設 minlen=14 dcredit=-1 ucredit=-1 ocredit=-1 lcredit=-1。PasswordAuthentication no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
ChallengeResponseAuthentication no
vim /etc/ssh/sshd_config
PermitRootLogin no
# 確保前無 # 號
systemctl restart sshd
前提:已建立普通用戶並加入 wheel 組,可 sudo 提權:
useradd -m -G wheel opsadmin
passwd opsadmin
# 訪問時:ssh -p 2222 opsadmin@host → sudo -i
yum -y install epel-release
yum -y install fail2ban
vim /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/secure
maxretry = 3
bantime = 3600
findtime = 600
action = iptables-multiport[name=sshd, port="2222", protocol=tcp]
systemctl enable --now fail2ban
fail2ban-client status sshd
| 措施 | 配置 |
|---|---|
| 僅允許特定 IP 登入 | AllowUsers opsadmin@192.168.1.* 或 Match Address 10.0.0.0/8 |
| 啟用 Google Authenticator 2FA | yum install google-authenticator + PAM 配置 |
| 端口敲門 | knockd 隱藏 SSH 端口,敲門序列後動態開放 |
| 審計所有登入 | LogLevel VERBOSE 記錄密鑰指紋、IP、時間 |