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.
- Updated Nov 27, 2026
- Markdown
Configure fail2ban jails with sensible ban times, unban addresses, enable the recidive jail and test filters before relying on them.
- 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:
sudo nano /etc/fail2ban/jail.d/sshd.local[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 192.0.2.25
bantime = 1h
findtime = 10m
maxretry = 4
[sshd]
enabled = true
backend = systemdReplace 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#
sudo systemctl restart fail2ban
sudo fail2ban-client status
sudo fail2ban-client status sshdThe 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:
sudo fail2ban-client set sshd banip 198.51.100.7
sudo fail2ban-client set sshd unbanip 198.51.100.7unbanip 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:
[recidive]
enabled = true
bantime = 1w
findtime = 1d
maxretry = 3This 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:
sudo fail2ban-regex systemd-journal sshd
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-botsearch.confThe 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#
sudo fail2ban-client status sshd
sudo fail2ban-client get sshd bantime
sudo fail2ban-client get sshd ignoreip
sudo tail -n 20 /var/log/fail2ban.logget 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,1dand1w. 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
unbanipto remove one.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
sshd jail not listed | Syntax error in a .local file | sudo journalctl -u fail2ban -n 30, fix the file, restart |
| No bans despite attacks | Jail reads the wrong log | Set backend = systemd for sshd |
fail2ban-regex reports 0 matched | Wrong filter or log path | Check the filter name and log source |
Failed to access socket path: /var/run/fail2ban/fail2ban.sock | fail2ban is not running | sudo systemctl start fail2ban |
| Locked out of SSH | Your address was banned | Use the console, then unbanip and add ignoreip |
| Changes ignored | Edited jail.conf or forgot restart | Use jail.d and restart the service |
| Banned IP still connects | Another route bypasses the firewall rule | Test the rule, check Docker published ports |
Quick reference#
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 fail2banRelated lessons#
- Protect SSH with fail2ban: installing and the first jail.
- Harden SSH server configuration: reduce what attackers can try at all.
- Advanced ufw firewall rules:
ufw limitas a lightweight companion. - Read logs with journalctl: see the failures fail2ban reads.
- Audit a Linux server: review failed logins with lastb and auth logs.
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.
Want a real machine? Free browser labs: Killercoda
Check what you learned
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
Keep going: guides and tutorials
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.