安装可以使用源码编译 官网 或者yum安装
安装
(base) [root@www fail2ban]# yum -y install fail2ban
配置
#配置
(base) [root@www fail2ban]# vim jail.conf
加入以下内容
42 [ssh-iptables]
43 enabled = true
44 filter = sshd
45 action = iptables[name=SSH, port=ssh, protocol=tcp]
46 sendmail-whois[name=SSH, [email protected], [email protected]]
47 logpath = /var/log/secure
48 maxretry = 3
49 bantime = 3600
50 findtime = 300
重启fail2ban
(base) [root@www fail2ban]# systemctl restart fail2ban
测试效果
[root@NFS ~]# ssh 192.168.100.119
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@NFS ~]# ssh 192.168.100.119
ssh: connect to host 192.168.100.119 port 22: Connection refused
可以在/var/log/secure 查看日志信息
查看iptables规则
(base) [root@www fail2ban]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
f2b-SSH tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain f2b-SSH (1 references)
target prot opt source destination
REJECT all -- 192.168.100.155 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 192.168.100.157 0.0.0.0/0 reject-with icmp-port-unreachable
RETURN all -- 0.0.0.0/0 0.0.0.0/0
(base) [root@www fail2ban]#
使用fail2ban-client 查看状态
(base) [root@www fail2ban]# fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- Filter
| |- Currently failed: 0
| |- Total failed: 6
| `- File list: /var/log/secure
`- Actions
|- Currently banned: 2
|- Total banned: 2
`- Banned IP list: 192.168.100.155 192.168.100.157
解锁特定的ip
(base) [root@www fail2ban]# fail2ban-client set ssh-iptables unbanip 192.168.100.155
1
(base) [root@www fail2ban]# fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- Filter
| |- Currently failed: 0
| |- Total failed: 6
| `- File list: /var/log/secure
`- Actions
|- Currently banned: 1
|- Total banned: 2
`- Banned IP list: 192.168.100.157