1、安装(此处以Rocky Linux 9为例)

dnf install fail2ban -y

2、编写配置文件(一分钟内连续错误三次,封禁十分钟)

# cat > /etc/fail2ban/jail.local << EOF
[DEFAULT]
ignoreip = 127.0.0.1/8
bantime  = 600  #封禁600秒,10分钟
findtime = 60  #检查时间范围60秒,1分钟
maxretry = 3  #错误次数
backend = auto
banaction = firewallcmd-ipset
action = %(action_mwl)s

[sshd]
enabled = true
filter  = sshd
port    = 22
action = %(action_mwl)s
logpath = /var/log/secure
bantime  = 86400
findtime = 600
maxretry = 5
EOF

3、启动服务

systemctl enable --now fail2ban

4、查看状态

# 查看sshd封禁情况
fail2ban-client status sshd

# 查看防火墙封禁情况
firewall-cmd --direct --get-all-rules
或者
iptables-save

5、解除IP封禁

fail2ban-client unban 11.11.11.11

参考:

https://github.com/fail2ban/fail2ban
https://github.com/oneinstack/oneinstack/blob/main/include/fail2ban.sh

  

posted on 2024-02-29 11:14  a120608yby  阅读(42)  评论(0)    收藏  举报