centos7 安装
yum install -y fail2ban
cp /etc/fail2ban/jail.{conf,local}
cat << EOF > /tmp/sshd.local
[sshd]
enabled = true
findtime = 1h
maxretry = 5
bantime = 1y
ignoreip = 127.0.0.1/8 ::1 192.168.0.0/24
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
EOF
cp /tmp/sshd.local /etc/fail2ban/jail.d/sshd.local
systemctl enable --now fail2ban
systemctl restart fail2ban可接受的缩写标记:
years?, yea?, yy?
months?, mon?
weeks?, wee?, ww?
days?, da, dd?
hours?, hou?, hh?
minutes?, min?, mm?
seconds?, sec?, ss?
- ?代表可选标记,因此要以天为单位设置时间,例如- days/- day/- da/- dd/- d
- 可以组合多个标记,格式为空格分隔,不带分隔符,例如:1y6mo或1d12h30m
- 标记 m和mm表示分钟,月份使用缩写mo或mon
Fail2ban客户端
Fail2ban附带了一个名为 fail2ban-client 的命令行工具,可用于与Fail2ban服务进行交互。
要查看所有可用选项,请使用-h选项调用命令:
fail2ban-client -h此工具可用于禁止/取消禁止IP地址,更改设置,重新启动服务等等。 以下是一些示例:
检查Jails状况:
sudo fail2ban-client status sshd取消IP封禁:
sudo fail2ban-client set sshd unbanip 23.34.45.56禁止IP:
sudo fail2ban-client set sshd banip 23.34.45.56参考:https://denisrasulev.medium.com/server-protection-with-fail2ban-bfbe38a775b0