Chapter 10 · Lesson 2 of 10
Server security

Tune fail2ban jails and bans (bantime, findtime, recidive)

Tuning fail2ban means setting ban rules in jail.local or jail.d files, so repeat offenders are blocked for longer while your own addresses are never banned.

By the end you will

Configure fail2ban jails with sensible ban times, unban addresses, enable the recidive jail and test filters before relying on them.

Time
20 min
Level
Intermediate
Check
4 questions
You need
  • Ubuntu or Debian server with fail2ban installed
  • A second SSH session open

fail2ban watches logs for repeated failures and bans the source address for a while. The defaults work, but you should tune them: set a ban time that actually discourages repeat attempts, make sure your own addresses can never be banned, read the right log on Ubuntu, and add the recidive jail so persistent offenders get a long ban. This lesson assumes the basics from protect SSH with fail2ban.

How it works#

A jail ties together a filter (a regular expression that spots a failure in a log), an action (usually a firewall rule that blocks the address) and timing settings. fail2ban reads configuration in layers: /etc/fail2ban/jail.conf holds packaged defaults, then jail.local and files in /etc/fail2ban/jail.d/ override it. Later files win. Because package upgrades may replace jail.conf, never edit it.

Four settings control behavior:

  • maxretry: failures allowed before a ban.
  • findtime: the time window in which those failures are counted.
  • bantime: how long the ban lasts. A negative value means permanent in recent versions.
  • ignoreip: addresses and ranges that are never banned.

So maxretry = 4 and findtime = 10m means four failures from one address within ten minutes causes a ban of bantime.

Step 1: Put your settings in jail.d#

Create an override file rather than editing the defaults:

bash
sudo nano /etc/fail2ban/jail.d/sshd.local
ini
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 192.0.2.25
bantime  = 1h
findtime = 10m
maxretry = 4

[sshd]
enabled = true
backend = systemd

Replace 192.0.2.25 with the public address you connect from. If your home address changes often, rely on key-only login from hardened SSH so a ban matters less. Do not add large ranges to ignoreip that you do not control, because attackers inside them are then never banned.

backend = systemd tells the jail to read the systemd journal. Recent Ubuntu releases may not write SSH logs to /var/log/auth.log the way older ones did, so a jail that reads a file can sit there seeing nothing. The journal is also readable with journalctl -u ssh, as shown in read logs with journalctl.

Step 2: Restart and check status#

bash
sudo systemctl restart fail2ban
sudo fail2ban-client status
sudo fail2ban-client status sshd

The first command lists active jails. The second shows currently failed and banned counts and the banned IP list for sshd. If sshd is missing from the list, the config has an error. Check sudo journalctl -u fail2ban -n 30 for the message.

Step 3: Ban and unban by hand#

You will sometimes lock out a colleague, or want to block an address you saw in the logs:

bash
sudo fail2ban-client set sshd banip 198.51.100.7
sudo fail2ban-client set sshd unbanip 198.51.100.7

unbanip removes the ban at once. If you banned your own address by mistake and cannot log in, use the provider console and run the unban from there. Bans created this way last as long as the jail’s bantime.

Step 4: Enable the recidive jail#

Ordinary bans expire. An attacker who comes back every hour gets a fresh set of attempts each time. The recidive jail reads fail2ban’s own log and bans addresses that were banned repeatedly:

ini
[recidive]
enabled  = true
bantime  = 1w
findtime = 1d
maxretry = 3

This reads: three bans within a day earns a week-long block. Choose values that fit your own tolerance. A long ban is only a good idea if you have an ignoreip entry for yourself and a console fallback. Restart fail2ban after adding it.

Step 5: Test a filter before trusting it#

When you write or change a filter, check it against real data:

bash
sudo fail2ban-regex systemd-journal sshd
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-botsearch.conf

The first argument is the log source, the second is the filter name or file. The output shows how many lines matched and how many were missed. A filter that matches zero lines on a log full of attempts is not protecting you. Use it before enabling any custom jail, and see rate limiting and security headers for limiting at the web server level.

Check it worked#

bash
sudo fail2ban-client status sshd
sudo fail2ban-client get sshd bantime
sudo fail2ban-client get sshd ignoreip
sudo tail -n 20 /var/log/fail2ban.log

get prints the live values, so you can confirm your file was applied. The log shows Ban and Unban lines with the jail name and address.

Edge cases and gotchas#

  • Your own address. Without ignoreip, a few typos can ban you. Keep a second session open and know your provider console.
  • Docker traffic. Bans are firewall rules on the host. Container ports published by Docker may bypass them, so check with a real test. See advanced ufw firewall rules.
  • Time units. Values accept suffixes such as 10m, 1h, 1d and 1w. A bare number means seconds.
  • IPv6. Make sure the action covers IPv6 if your server has an IPv6 address, or attackers just switch family.
  • Long permanent bans. A shared address, such as an office or mobile carrier, can be banned for a long time because of one person. Prefer long finite bans.
  • Restart clears nothing. A restart keeps the ban database, so bans usually persist across restarts. Use unbanip to remove one.

Troubleshooting#

SymptomLikely causeFix
sshd jail not listedSyntax error in a .local filesudo journalctl -u fail2ban -n 30, fix the file, restart
No bans despite attacksJail reads the wrong logSet backend = systemd for sshd
fail2ban-regex reports 0 matchedWrong filter or log pathCheck the filter name and log source
Failed to access socket path: /var/run/fail2ban/fail2ban.sockfail2ban is not runningsudo systemctl start fail2ban
Locked out of SSHYour address was bannedUse the console, then unbanip and add ignoreip
Changes ignoredEdited jail.conf or forgot restartUse jail.d and restart the service
Banned IP still connectsAnother route bypasses the firewall ruleTest the rule, check Docker published ports

Quick reference#

bash
sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd banip 198.51.100.7
sudo fail2ban-client set sshd unbanip 198.51.100.7
sudo fail2ban-regex systemd-journal sshd
sudo systemctl restart fail2ban

Next#

Continue with Advanced ufw firewall rules.

Try it: Tune the sshd jail

Write a local override, reload fail2ban and inspect and unban an address.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Where should your own fail2ban changes go?
2maxretry = 5, findtime = 10m. When does a ban happen?
3What does the recidive jail do?
4Which tool checks whether a filter matches lines of a log?

Frequently asked questions

Should I edit jail.conf or jail.local?

Never edit jail.conf, because package upgrades can replace it. Put your changes in jail.local or in files under /etc/fail2ban/jail.d/, which override the defaults.

How do I unban an IP in fail2ban?

Run sudo fail2ban-client set sshd unbanip 203.0.113.50, using the jail name and the address. Check the result with sudo fail2ban-client status sshd.

What is the difference between findtime and bantime?

findtime is the window in which failures are counted. maxretry failures inside that window trigger a ban, and bantime is how long the ban lasts.

Why does fail2ban not ban anyone on Ubuntu?

Often the jail reads the wrong log. Recent Ubuntu releases log SSH to the systemd journal, so set backend = systemd for the sshd jail, then restart fail2ban and check its status.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios