How to unban an IP with fail2ban (fail2ban-client and iptables)

Fail2Ban is great but sometimes you ban wrongly.
Here's how to unban an IP banned by Fail2Ban.

TL;DR, as root on the server:

fail2ban-client status <jail>
fail2ban-client set <jail> unbanip <ip>

The first command lists the IPs banned by a jail, the second one unbans the IP. The iptables method, still useful on very old versions or when the rule is left behind in the firewall, is described further down.

Find the jail that banned the IP

Log in to the server as root and list the active jails:

fail2ban-client status

You'll get something like:

Status
|- Number of jail:      2
`- Jail list:   apache-botseeker-adf, sshd

Then show the details of a jail. The banned IPs are on the last line:

fail2ban-client status apache-botseeker-adf
Status for the jail: apache-botseeker-adf
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     42
|  `- File list:        /var/log/apache2/error.log
`- Actions
   |- Currently banned: 3
   |- Total banned:     18
   `- Banned IP list:   66.249.71.209 88.184.109.30 109.8.129.47

If you don't know which jail banned the IP, search the Fail2Ban log:

grep 66.249.71.209 /var/log/fail2ban.log

Unban the IP with fail2ban-client

fail2ban-client set apache-botseeker-adf unbanip 66.249.71.209

Of course, replace "apache-botseeker-adf" with the name of the jail, and the IP with the one you want to unban. The command returns 1 if the IP was unbanned, 0 if that jail had not banned it.

Since version 0.10, you can also unban an IP from all jails at once:

fail2ban-client unban 66.249.71.209

Run fail2ban-client status <jail> again to check that the IP is no longer in the list of banned addresses.

Using fail2ban-client rather than iptables has two benefits: Fail2Ban updates its own state (otherwise it still thinks the IP is banned), and the command works whatever firewall is behind it (iptables, nftables, firewalld…).

Keep the IP from being banned again

In my case, Fail2Ban had banned Google's crawler, no less. I don't know about you, but it's not a target I was planning to ban. If the IP is likely to trigger the rule again, add it to ignoreip in /etc/fail2ban/jail.local:

[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 66.249.71.209

Then reload the configuration:

fail2ban-client reload

Appendix: unban with iptables

This is the method I originally used. It is still useful on very old versions of Fail2Ban, or when fail2ban-client says the IP is not banned while it is still blocked in iptables (a rule left behind after a Fail2Ban restart, for example).

List the banned IPs with the command:

iptables -L

You'll get something like:

...
Chain fail2ban-apache-botseeker-adf (1 references)
target     prot opt source               destination
DROP       all  --  APoitiers-551-1-48-250.w92-146.abo.wanadoo.fr  anywhere
DROP       all  --  ip-83-134-25-126.dsl.scarlet.be  anywhere
DROP       all  --  ADijon-154-1-14-132.w90-33.abo.wanadoo.fr  anywhere
DROP       all  --  cnv94-6-88-185-61-237.fbx.proxad.net  anywhere
DROP       all  --  mau78-1-88-184-109-30.fbx.proxad.net  anywhere
DROP       all  --  APlessis-Bouchard-154-1-33-207.w90-3.abo.wanadoo.fr  anywhere
DROP       all  --  crawl-66-249-68-133.googlebot.com  anywhere
DROP       all  --  ABordeaux-257-1-85-192.w90-50.abo.wanadoo.fr  anywhere
DROP       all  --  47.129.8.109.rev.sfr.net  anywhere
DROP       all  --  ANancy-256-1-30-55.w90-26.abo.wanadoo.fr  anywhere
DROP       all  --  AAnnecy-158-1-62-25.w90-48.abo.wanadoo.fr  anywhere
DROP       all  --  ALagny-152-1-28-44.w83-112.abo.wanadoo.fr  anywhere
DROP       all  --  pub30-175.mobius.fr  anywhere
DROP       all  --  APuteaux-651-1-273-223.w90-44.abo.wanadoo.fr  anywhere
DROP       all  --  81-67-122-188.rev.numericable.fr  anywhere
DROP       all  --  209.151.95.79.rev.sfr.net  anywhere
DROP       all  --  190.195.22.93.rev.sfr.net  anywhere
DROP       all  --  crawl-66-249-71-209.googlebot.com  anywhere
RETURN     all  --  anywhere             anywhere
...

On the penultimate line, you can see Google's crawler.

You have to count the number of banned IP lines up to the one you want to unban. Here it is the 18th line. To avoid counting, add --line-numbers (and -n to show IPs without DNS resolution, which is much faster):

iptables -L fail2ban-apache-botseeker-adf -n --line-numbers

Then a small command line. Of course, replace "fail2ban-apache-botseeker-adf" with the name of the chain that banned the IP (f2b-<jail> on recent versions) and "18" with the corresponding line number.

iptables -D fail2ban-apache-botseeker-adf 18

Run the first command again to check that the IP is no longer in the list of banned addresses.

Be careful: with this method, Fail2Ban is not notified and still considers the IP banned. On recent distributions, the rules may also live in nftables (nft list ruleset) rather than iptables. In both cases, prefer fail2ban-client.

Keep an eye on what Fail2Ban bans

If like me, you have set up a ban strategy based on Apache error logs to keep robots away, consider monitoring Fail2Ban with a tool like Munin or Cacti and checking from time to time who you have banned.

Add a comment