With Fail2Ban before v0.8.8:
fail2ban-client get YOURJAILNAMEHERE actionunban IPADDRESSHERE
With Fail2Ban v0.8.8 and later:
fail2ban-client set YOURJAILNAMEHERE unbanip IPADDRESSHERE
The hard part is finding the right jail:
- Use
iptables -L -n
to find the rule name...
- ...then use
fail2ban-client status | grep "Jail list" | sed -E 's/^[^:]+:[ \t]+//' | sed 's/,//g'
to get the actual jail names. The rule name and jail name may not be the same but it should be clear which one is related to which.
The recidive jail recommended in the other answer here did not fix the issue for me. I eventually fixed this, however, so here's my method in case it helps others.
Fail2ban only blocks over TCP by default. At least with my setup, I noticed the "already banned" message was appearing when bots came back to try the blocked port over UDP instead.
To fix this issue, tell Fail2ban to block the port over all protocols instead of just TCP. You will need to make this change in /etc/fail2ban/jail.conf and in the [Init] section of every action you are using at /etc/fail2ban/action.d/.
Change this:
# Default protocol
protocol = tcp
To:
# Default protocol
protocol = all
Next, I disabled ICMP echo requests so blocked IPs had no way of hitting the server:
- nano /etc/sysctl.conf
Add these two lines:
net.ipv4.icmp_echo_ignore_all = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
- Exit and save the file.
- Run sysctl -p for the change to take effect.
After that, run fail2ban-client reload and you should not see these "already banned" messages any more unless you are spammed by an IP who gets a couple of access attempts before the block takes effect.
Also, it's important to block all ports for every offender rather than the port they were trying to access by using the action iptables-allports in each of the Jails. Otherwise, they may trigger another Jail and end up as "already banned" in the logs.
Best Answer
I once saw a system for centralizing fail2ban data on this site, and created a modified version. The database is the same, bu I changed and created some scripts.
My system have 4 components:
fail2ban database
It's a MySQL database containing only one table:
erp_core_fail2ban
:fail2ban.php
Every time a host is banned, it will populate the database:
cron2ban
You put this to run on crontab, every minute. It will retrieve the last added hosts, and ban them.
phpconfig
This file goes to /etc/fail2ban and have database configuration and jail selection.
Create those files and change the configuration from fail2ban:
After the line with
actionban = .....
a new row inserted to invoke the PHP script:/root/fail2ban.php <name> <protocol> <port> <ip>
Using this structure on all your servers will assure that every time one host gets banned on one server, all the other servers will ban it too.