Fail2ban jails explained in plain terms: a jail is a rule set that watches one log file for repeated failures and bans the source IP for a set time. Fail2ban itself does not block traffic. It reads logs, matches failure patterns, and tells your firewall to drop packets from an offending address. That split matters, because a jail only works when three parts line up: the right log file, a filter that matches real failures, and a ban action your firewall actually applies.
Most VPS owners meet Fail2ban through SSH, where bots try common usernames and passwords all day. The same idea covers WordPress logins and mail services, but each service writes different logs and fails in different ways. This guide covers what a jail is, how the SSH, WordPress, and mail jails differ, and where Fail2ban falls short.
Fail2ban Jails Explained: How a Jail Works
Every jail has four working parts. The filter holds regular expressions that match failure lines in a log. The log path tells Fail2ban which file to read. The findtime and maxretry settings define the trigger, such as five failures within ten minutes. The bantime sets how long the ban lasts, and the banaction defines how the ban is applied, usually by adding a firewall rule.
When a match count crosses the threshold inside the window, Fail2ban runs the ban action for that IP. When bantime expires, it removes the rule. Nothing about the ban is permanent by default, and a banned IP can return once the timer runs out. That design keeps a jail from filling your firewall with stale rules, but it also means a patient attacker can simply wait and try again.
Fail2ban ships with many built-in jails, but only a few are active by default. Add your own settings in jail.local, never by editing jail.conf, since package updates overwrite jail.conf and wipe your changes. Our Fail2ban setup guide for SSH brute-force protection covers the first install and the SSH jail in more detail.
The SSH Jail: Your First Line of Defence
The built-in sshd jail watches the authentication log for failed SSH logins. On Ubuntu 24.04 that log is /var/log/auth.log. The jail matches lines for failed passwords, invalid users, and refused keys, then bans the source after the set number of failures. A basic fail2ban jail configuration for SSH in /etc/fail2ban/jail.local looks like this:
[sshd] enabled = true port = ssh logpath = /var/log/auth.log maxretry = 5 findtime = 10m bantime = 1h
Those numbers are a starting point. A maxretry of 5 with a findtime of 10 minutes tolerates a few typos, while a bantime of 1 hour stops a bot script without locking a real user out for a day. If you use a non-standard SSH port, set the port line to that number, or the ban action blocks the wrong port.
After editing jail.local, restart the service and check the jail:
sudo systemctl restart fail2ban sudo fail2ban-client status sshd
The status output shows currently banned IPs and total failures seen. Test from an address you control by failing five logins on purpose, then confirm the ban appears. Keep your working SSH session open while you test, so a mistake does not lock you out. A fail2ban ssh jail works best next to key-only login, since keys end password guessing and leave the jail to catch the remaining noise.
The WordPress Jail: Blocking Login Attacks at the App Layer
WordPress has no built-in jail, because it records login failures only when a plugin logs them, or in the web server access log as repeated POST requests to wp-login.php. One common approach filters the Nginx or Apache access log for repeated POST requests to wp-login.php and xmlrpc.php from one IP, then bans that IP at the firewall.
A minimal custom filter in /etc/fail2ban/filter.d/wordpress.conf looks like this:
[Definition]
failregex = ^<HOST> - .* "POST /wp-login.php
^<HOST> - .* "POST /xmlrpc.php
ignoreregex =
Pair it with a jail in jail.local:
[wordpress] enabled = true port = http,https filter = wordpress logpath = /var/log/nginx/access.log maxretry = 10 findtime = 5m bantime = 1h
Set maxretry higher here than for SSH, because a shared office or campus IP can trip a low threshold fast. A wordpress fail2ban filter is a second layer, not a replacement for WordPress hardening: strong passwords, two-factor login, and limited login attempts inside WordPress still do most of the work. For a broader checklist, see the WordPress hardening basics for VPS owners, and consider disabling XML-RPC if your site does not need it, since xmlrpc.php draws a large share of automated login attempts.
Mail Jails: Postfix and Dovecot
Mail servers attract the same brute-force traffic, aimed at SMTP authentication and IMAP logins. Fail2ban ships filters for both. The postfix jail watches the mail log for failed SMTP authentication, and the dovecot jail watches for failed IMAP and POP3 logins. A combined mail setup in jail.local looks like this:
[postfix] enabled = true port = smtp,ssmtp,submission logpath = /var/log/mail.log maxretry = 5 findtime = 10m bantime = 1h [dovecot] enabled = true port = pop3,pop3s,imap,imaps logpath = /var/log/mail.log maxretry = 5 findtime = 10m bantime = 1h
Mail jails need care around shared IPs. One user with a wrong saved password can get a whole office banned from mail when many users share one address. Keep maxretry at 5 or higher for mail, watch the mail log after enabling the jail, and whitelist your own office IP with ignoreip if your server serves a fixed office address.
Tuning bantime, findtime, and the Recidive Jail
Short bans stop scripts; long bans stop repeat offenders. The recidive jail bridges the two by watching the Fail2ban log itself and banning IPs that get banned repeatedly, for a much longer period. Enable it in jail.local:
[recidive] enabled = true logpath = /var/log/fail2ban.log bantime = 1w findtime = 1d maxretry = 5
An IP banned five times in one day then sits out for a week. Ordinary users, who rarely trip a jail even once, never touch that threshold. Check it with fail2ban-client status recidive.
When tuning, change one value at a time and watch the logs for a day. A bantime that is too long turns a typo into a support ticket, and a findtime that is too short lets a slow attacker slip under the threshold by spacing attempts apart. The right values depend on how many real users share IPs on your services.
Checking, Unbanning, and Managing Jails
Day-to-day Fail2ban work runs through fail2ban-client. The commands you will use most are:
sudo fail2ban-client statuslists all active jails.sudo fail2ban-client status sshdshows bans and failure counts for one jail.sudo fail2ban-client set sshd unbanip 203.0.113.45removes one ban at once.sudo fail2ban-client reloadreloads config after a jail.local edit without a full restart.
Unban your own IP the moment you trip a jail by accident, and add it to ignoreip if it happens twice. Also check that the firewall backend matches your system: Fail2ban defaults to nftables on modern Ubuntu, and a mismatch there is the most common reason bans appear in status but traffic keeps flowing. Pair the jail setup with a correct firewall base, as covered in our UFW firewall setup guide, and test a real ban end to end rather than trusting status output alone.
Limitations and Downsides of Fail2ban
Fail2ban has real limits. First, it only reacts to failures it can see in a log: an attack that succeeds on the first guess, or that uses stolen credentials, never trips a jail. Second, it struggles against distributed attacks, since a botnet trying one password from thousands of IPs never hits maxretry on any single address. Third, it depends on log formats, and a service update that changes a log line can silently break a filter.
Fourth, bans can hit real users: shared IPs, mistyped passwords, and mail clients that retry aggressively all cause false bans. Fifth, Fail2ban adds load and complexity, since every jail tails a log, every ban adds a firewall rule, and a misconfigured jail can block your own admin access. None of this makes Fail2ban useless. It makes it one layer: keys for SSH, strong passwords and two-factor login for WordPress, and a firewall underneath, with Fail2ban cutting the automated noise on top. Set it up, test a real ban, tune the numbers to your traffic, and re-check the filters after service updates.