Fail2ban 經過掃描錯誤日誌來禁止某些 IP 訪問服務,它會直接修改防火牆規則來阻止來自這些 IP的請求。vim
我在使用 journalctl -xe --full
查日誌時,發現大量的相似下面的日誌:服務器
sshd[13352]: pam_unix(sshd:auth): check pass; user unknown sshd[13352]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=182.61.161.121 sshd[13352]: Failed password for invalid user chase from 182.61.161.121 port 58172 ssh2 sshd[13352]: Received disconnect from 182.61.161.121 port 58172:11: Bye Bye [preauth] sshd[13352]: Disconnected from 182.61.161.121 port 58172 [preauth] sshd[13354]: reverse mapping checking getaddrinfo for dynamic-ip-1868417225.cable.net.co [186.84.172.25] failed - POSSIBLE BREAK-IN ATTEM sshd[13354]: Invalid user survey from 186.84.172.25 port 38058 sshd[13354]: input_userauth_request: invalid user survey [preauth]
能夠發現是有陌生 IP 的用戶在嘗試破解 SSH 登陸。app
經過這條命令能夠看到的確有大量失敗登陸的記錄:ssh
grep -o "Failed password" /var/log/secure|uniq -c
73426 Failed password
。tcp
所以我採起了一些措施,首先修改了 ssh 的默認端口:ide
vim /etc/ssh/sshd_config
改完後須要重啓 sshd:ui
systemctl restart sshd
而後就是安裝了 Fail2ban。.net
在個人 CentOS 服務器上用 yum 安裝:unix
yum -y install fail2ban
而後進行配置:rest
vim /etc/fail2ban/jail.d/jail.local
其中 22 要改成實際的 ssh 端口:
[sshd] enabled = true filter = sshd action = iptables[name=ssh,port=22,protocol=tcp] logpath = /var/log/secure bantime = 600 maxretry = 3
保存後重啓服務
systemctl restart fail2ban
查看狀態
fail2ban-client status sshd