Fail2ban阻止服务器暴力破解登录
12/03
本文最后更新于
2024年12月03日,已超过
88天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
今天通过SSH登录到云服务器上,看到离上次成功登录已经有快三万多次的非法尝试登录。可怜我的小服务器啥都没有,还要来暴力破解...
There were 27607 failed login attempts since the last successful login.
为了增强服务器安全,在网上找到一种可以阻止暴力破解的方法,就是使用fail2ban
工具锁定多次登录失败的IP。
首先安装fail2ban
工具(服务器是centos系统)
> dnf install epel-release -y
> dnf install fail2ban -y
> vi /etc/fail2ban/jail.local
安装好后进行设置,新增文件/etc/fail2ban/jail.local
并添加以下内容
[sshd]
enabled = true
port = ssh
filter = sshd
logpath =/var/log/secure
maxretry = 3
bantime = 86400
其中maxretry
参数指密码登录最大尝试次数,bantime
指锁定IP时间,单位是秒,可以设长一点。
然后开启fail2ban服务
> systemctl enable --now fail2ban
过一会儿就可以检查效果啦,使用fail2ban-client
命令查看是否成功阻止非法IP访问。
> fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: sshd
进一步查看阻止的IP:
> fail2ban-client status sshd
此外,为了服务器安全,可以将SSH端口改成其它不常用的端口。还可以禁止SSH密码登录,只允许通过密钥登录。
参考资料

