安装并配置fail2ban防止ssh爆破攻击
通过下面命令安装fail2ban
yum -y install fail2ban
启动
systemctl start fail2ban
查看状态
systemctl status fail2ban
设置开机启动
systemctl enable fail2ban
检查版本
fail2ban-client -V
fail2ban-sever -V
检查配置文件是否有误
fail2ban-client -d
配置ssh防爆破策略文件
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
vi /etc/fail2ban/jail.local
添加下面内容,点击查看
[SSH-iptables]
enabled = true
filter = sshd
action = iptables[name=SSH, port=22, protocol=tcp]
logpath = /var/log/secure
maxretry = 3
bantime = 300
maxretry是3次,bantime是300秒5分钟
配置后需要重启服务
systemctl restart fail2ban
通过ssh登录错误尝试看看
查看日志
cat /var/log/fail2ban.log
三次后ip被ban了,直接给自己踢下线了
换地址登录后查看日志
十分钟后解锁了
PS:SSH-iptables里写错了bintime,导致5分钟的没生效是10分钟
验证fail2ban是否正常运行
fail2ban-client ping
正常情况回复pong
查看被禁的ip
fail2ban-client status SSH-iptables
在日志里也看到重启服务后的新策略
通过系统iptables加黑名单
iptables -vnL
感觉这个比denyhosts好用,那个只能用来防止ssh爆破攻击,而且配置不难