$ fail2ban-client status Status |- Number of jail: 1 `- Jail list: sshd
可以看到当前只启动了 sshd 这一个 jail
执行以下命令查看 sshd 的防护情况,可以看到目前有哪些 ip 被封锁,以及过去总共多少 ip 被封锁
$ fail2ban-client status sshd Status for the jail: sshd |- Filter | |- Currently failed: 0 | |- Total failed: 0 | `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd `- Actions |- Currently banned: 1 |- Total banned: 1 `- Banned IP list: 54.193.188.141
$ fail2ban-client status Status |- Number of jail: 2 `- Jail list: sshd, vnc
如果输出显示 : ERROR Failed to access socket path: /var/run/fail2ban/fail2ban.sock. Is fail2ban running? ,说明 fail2ban 启动失败,可能是配置文件存在问题,比如 fail2ban 服务状态如下:
$ systemctl status fail2ban -l fail2ban.service - Fail2Ban Service Loaded: loaded (/usr/lib/systemd/system/fail2ban.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Sat 2022-08-20 13:33:48 CST; 24s ago Docs: man:fail2ban(1) Process: 2520 ExecStop=/usr/bin/fail2ban-client stop (code=exited, status=0/SUCCESS) Process: 2536 ExecStart=/usr/bin/fail2ban-server -xf start (code=exited, status=255) Process: 2533 ExecStartPre=/bin/mkdir -p /run/fail2ban (code=exited, status=0/SUCCESS) Main PID: 2536 (code=exited, status=255)
Aug 20 13:33:47 myserver-cn systemd[1]: Starting Fail2Ban Service... Aug 20 13:33:47 myserver-cn systemd[1]: Started Fail2Ban Service. Aug 20 13:33:48 myserver-cn fail2ban-server[2536]: 2022-08-20 13:33:48,058 fail2ban [2536]: ERROR Failed during configuration: Have not found any log file for vnc jail Aug 20 13:33:48 myserver-cn fail2ban-server[2536]: 2022-08-20 13:33:48,060 fail2ban [2536]: ERROR Async configuration of server failed Aug 20 13:33:48 myserver-cn systemd[1]: fail2ban.service: main process exited, code=exited, status=255/n/a Aug 20 13:33:48 myserver-cn systemd[1]: Unit fail2ban.service entered failed state. Aug 20 13:33:48 myserver-cn systemd[1]: fail2ban.service failed.
输出中显示 Failed during configuration: Have not found any log file for vnc jail,说明 jail.d/vnc.local 中配置的日志路径或者是权限有问题,需要检查此处配置。
此处报错是因为 jail.d/vnc.local 中配置的日志路径如下:
jail.d/vnc.local
logpath = /home/cosmos/.vnc/cosmos\:1.log
配置文件中不需要对 : 进行转义。
配置无误,重启服务生效后,可以检查 vnc 对应的 ban 的状态:
$ fail2ban-client status vnc Status for the jail: vnc |- Filter | |- Currently failed: 22 | |- Total failed: 52632 | `- File list: /home/cosmos/.vnc/cosmos:1.log.bak `- Actions |- Currently banned: 18 |- Total banned: 18 `- Banned IP list: 212.80.219.226 212.80.219.175 175.211.155.52 ...
查看防火墙状态,可以看到被封的 ip :
iptables -L -v -n
如果被封禁的 ip 中有被误伤的,需要解禁,使用以下命令:
fail2ban-client set vnc unbanip 45.33.65.249
命令中的 vnc 指 ban 配置文件 /etc/fail2ban/jail.d/vnc.local 中的 [vnc]名称