Learning how to set up Fail2ban is the fastest way to stop the background noise every public VPS attracts. Within minutes of opening SSH to the internet, bots start trying common usernames and passwords, and if you run a website, the same bots hammer your login pages too. Fail2ban watches your logs, counts repeated failures, and tells your firewall to block the offending IP for a while. It is not a replacement for key-only SSH or strong passwords, but it turns a constant trickle of attacks into a quiet log file.
This guide sets up Fail2ban on Ubuntu with safe defaults, then adds three practical jails: SSH, Nginx basic-auth, and WordPress logins. You will also add a recidive jail for repeat offenders, learn how to test a ban without locking yourself out, and see the mistakes that cause most Fail2ban headaches. Everything here was verified against current Fail2ban and community guides in October 2026.
What Fail2ban actually does
Fail2ban has three moving parts, and keeping them straight makes the rest of the guide easy. A filter is a regular expression that recognises a failed login in a log file. A jail ties a filter to a log file, a failure threshold, and a ban action. An action is how the ban is enforced, usually by adding a firewall rule with nftables, iptables, or UFW.
Three settings control almost every jail. maxretry is how many failures are allowed before a ban. findtime is the window those failures must fall inside. bantime is how long the ban lasts. So maxretry = 5, findtime = 10m, and bantime = 1h means: five failures within ten minutes earns a one-hour ban.
What Fail2ban does not do matters just as much. It does not patch your software, it does not stop a distributed attack coming from thousands of different IPs, and it does not make password authentication safe on its own. If SSH still accepts passwords, a patient attacker with a good password list can still get lucky. Use Fail2ban alongside key-only SSH, which this blog covered in its SSH hardening guide, not instead of it.
Install Fail2ban on Ubuntu
Fail2ban is in Ubuntu’s default repositories, so installation is one command. Run it over your existing SSH session, and keep that session open for the whole setup — it is your escape route if something goes wrong.
sudo apt update
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban --no-pager
On Ubuntu and Debian, the package ships a jail.d/defaults-debian.conf file that already enables the sshd jail and reads SSH events from the systemd journal. So SSH protection may already be running the moment you install. Check before you assume anything:
sudo fail2ban-client status
sudo fail2ban-client status sshd
If sshd appears in the jail list, it is active. The rest of this guide still applies: the shipped defaults are lenient, and you still need to set your own trusted IPs, tune the thresholds, and add the web jails.
The one rule: never edit jail.conf
Never edit /etc/fail2ban/jail.conf. Package updates can overwrite it, silently wiping your configuration. Put every override in /etc/fail2ban/jail.local or in a separate file under /etc/fail2ban/jail.d/ ending in .local. Fail2ban merges these files, and your local settings win over the shipped defaults.
This guide uses one file, /etc/fail2ban/jail.local, so the whole setup is easy to review in one place. If you later manage many servers, splitting one jail per file under jail.d/ is the tidier option, but the settings themselves are identical.
Step 1: Lockout-proof defaults
Before enabling anything else, set global defaults and whitelist your own IPs. This is the step people skip, and it is the step that causes lockouts. Add your home or office IP and your VPN range to ignoreip. If your IP changes often, use your VPN’s static range instead of a single address, and never whitelist 0.0.0.0/0 — that disables the protection entirely.
sudo nano /etc/fail2ban/jail.local
[DEFAULT]
# An IP that fails 5 times within 10 minutes is banned for 1 hour.
bantime = 1h
findtime = 10m
maxretry = 5
# Repeat offenders get progressively longer bans.
bantime.increment = true
# Never ban these addresses. Replace 203.0.113.10 with your own static IP.
ignoreip = 127.0.0.1/8 ::1 203.0.113.10
# Read logs from the systemd journal where available.
backend = systemd
The bantime.increment = true line is the single most useful tuning change for a busy server. The first ban lasts an hour, but each repeat ban grows longer, so a bot that keeps coming back spends most of its time banned instead of probing. The exact growth curve depends on your Fail2ban version, so treat it as “longer each time” rather than memorising a multiplier.
One more safety habit: whenever you change SSH or firewall settings, open a second SSH session and confirm you can still log in before closing the first one. Fail2ban bans are temporary, but a bad firewall rule is not, and a second session is free insurance.
Step 2: The SSH jail
Add the SSH jail to the same jail.local file. If SSH listens on a custom port, set port to that number; otherwise ssh resolves to port 22 through the system service name.
[sshd]
enabled = true
port = ssh
filter = sshd
backend = systemd
logpath = %(sshd_log)s
maxretry = 5
findtime = 10m
bantime = 1h
On Ubuntu, %(sshd_log)s resolves to the systemd journal path handling, and backend = systemd tells Fail2ban to read journal entries rather than a flat log file. If you run a distro where SSH still logs to /var/log/auth.log or /var/log/secure, point logpath at that file instead and drop the journal backend. The wrong log source is the most common reason a jail “runs” but never bans anything.
Test the configuration before restarting the service. A syntax error here will stop Fail2ban from starting at all:
sudo fail2ban-client -t
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
Step 3: The Nginx basic-auth jail
SSH is only half the story on a web server. If any part of your site sits behind Nginx basic authentication — a staging site, an admin area, a private API — that password prompt gets brute-forced too. Fail2ban ships a ready-made jail for it, nginx-http-auth, which watches /var/log/nginx/error.log for repeated authentication failures.
[nginx-http-auth]
enabled = true
filter = nginx-http-auth
port = http,https
logpath = /var/log/nginx/error.log
maxretry = 5
findtime = 10m
bantime = 1h
Note the log path: basic-auth failures land in Nginx’s error log, not the access log. Pointing this jail at access.log is a quiet misconfiguration that bans nobody. If you use Apache instead, the equivalent shipped jail is apache-auth, with /var/log/apache2/error.log on Ubuntu.
Restart Fail2ban and confirm the jail loaded:
sudo systemctl restart fail2ban
sudo fail2ban-client status nginx-http-auth
Step 4: WordPress logins, done the right way
WordPress is the trap in this guide, because the obvious approach is wrong. WordPress returns HTTP 200 even when a login fails, so a filter that bans every POST to wp-login.php cannot tell a failed login from a successful one. Worse, it counts your own legitimate logins against you. Guides that recommend that bare POST filter will eventually ban a real user who logs in a few times in a row.
The fix is small: make failed WordPress logins return a status code Fail2ban can see. Add this mu-plugin, which fires on the wp_login_failed hook and changes only the response status:
# wp-content/mu-plugins/fail2ban-403.php
<?php
add_action('wp_login_failed', function() {
status_header(403);
});
Now a failed login leaves a 403 in the Nginx access log, and a filter can match exactly that. Create the filter:
sudo nano /etc/fail2ban/filter.d/wordpress-login.conf
[Definition]
failregex = ^<HOST> - .* "POST /wp-login\.php.*" 403
^<HOST> - .* "POST /xmlrpc\.php.*" 403
ignoreregex =
Then add the jail to jail.local:
[wordpress-login]
enabled = true
filter = wordpress-login
port = http,https
logpath = /var/log/nginx/access.log
maxretry = 5
findtime = 10m
bantime = 1h
Before enabling any custom filter, test it against your real logs. This command shows what the filter would match, without banning anyone:
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/wordpress-login.conf
If the test matches zero lines even though you know failed logins happened, your log format differs from the one assumed here — check the actual access-log line first, adjust the regex, and test again. Never enable an untested custom filter in production. Also note the xmlrpc line: if you do not use XML-RPC at all, blocking it outright in your Nginx config is simpler and safer than banning IPs for hitting it.
Step 5: Catch repeat offenders with recidive
Individual jails forget a banned IP once the ban expires. The recidive jail fixes that by watching Fail2ban’s own log: an IP that gets banned repeatedly across jails earns a much longer ban of its own.
[recidive]
enabled = true
filter = recidive
logpath = /var/log/fail2ban.log
bantime = 1d
findtime = 1d
maxretry = 5
Recidive is optional on a quiet server, but on a server that gets probed daily it is what turns Fail2ban from a speed bump into a real deterrent. A bot that burns five one-hour bans in a day wakes up to a full-day ban instead of a fresh chance.
Verify, test, and unban
After every change, run the same three checks: config test, service restart, jail status. Then watch the log while you deliberately trigger a failure from a test IP — never from your own workstation IP unless it is whitelisted — and confirm the ban appears:
sudo fail2ban-client -t
sudo systemctl restart fail2ban
sudo fail2ban-client status
sudo tail -f /var/log/fail2ban.log
When a legitimate user gets banned, or you ban yourself during testing, unban them immediately. The command names the jail, so an IP banned by the SSH jail is unbanned from that jail:
sudo fail2ban-client set sshd unbanip 203.0.113.10
sudo fail2ban-client set wordpress-login unbanip 203.0.113.10
To ban an IP manually — for example, one you spotted hammering your logs before it hit the threshold — use the matching banip command. Manual bans follow the same bantime as automatic ones unless you override them, so use them sparingly and let the jails do the routine work.
Troubleshooting: why is nothing being banned?
Most Fail2ban problems fall into four buckets. Work through them in order before rewriting filters.
Wrong log source. Confirm the file in logpath exists and actually contains failure lines. Run sudo tail -f on it while triggering a failure. If the failures go to the journal instead, switch the jail to backend = systemd.
Wrong firewall backend. Fail2ban must ban through the firewall you actually run. Mixing UFW rules with a firewalld action, or an iptables action on a system that moved to nftables, produces bans that never take effect. Pick one firewall tool, and leave banaction at its distro default unless you have a specific reason to change it.
Behind a proxy. If Nginx sits behind Cloudflare or a load balancer, the access log shows the proxy’s IP, not the attacker’s. Banning that IP bans everyone behind the proxy. Fix real-IP forwarding first, or skip the web jails until you have. This is a configuration problem, not a Fail2ban bug, and no filter tweak fixes it.
Filter does not match. Log formats vary by distro and by custom Nginx log_format directives. Always run fail2ban-regex against a real log sample before enabling a custom filter, and re-test after any Nginx log-format change.
What to do after setup
Fail2ban is set-and-forget only if you check it occasionally. Once a week, glance at fail2ban-client status and the tail of /var/log/fail2ban.log: a jail with zero bans on a public server usually means its log source broke, not that attacks stopped. After any Nginx or SSH configuration change, re-run fail2ban-client -t and confirm every jail still loads. And keep the rest of the stack in order — key-only SSH, automatic security updates, and the backups this blog covered earlier — because Fail2ban slows attackers down, but it is the locked doors and the backups that actually save you when one gets through.
Further Reading & References
- How to Configure Fail2ban on Linux Server — YouStable — full jail, recidive, and WordPress sections used to cross-check this guide’s defaults.
- SSH Lockdown Tutorial (2026) — HostMyCode — SSH jail settings and the ignoreip seatbelt.
- How to Protect Nginx with Fail2ban — SlashSkill — the mu-plugin 403 approach for WordPress logins, and why WordPress’s 200-on-failure breaks naive filters.
- Solved: How I Stopped Brutal WordPress Attacks Using Fail2Ban on Ubuntu VPS — DEV — WordPress custom-filter walkthrough.
- Securing a Linux Server: Part 2 — Fail2ban Against Password Brute-Force Attacks — BigMike.help — filter/jail/action model and Debian’s pre-enabled sshd jail.
- Install and Configure Fail2Ban on Ubuntu Server — SmartVDS — config test, restart, and second-session safety check.
- SSH Hardening: How to Lock Down Your VPS in 7 Steps (2026) — HowToShipIt — the key-only SSH setup to pair with Fail2ban.
- How to Set Up an Nginx Reverse Proxy for Node.js Apps — HowToShipIt — Nginx setup whose logs the web jails in this guide read.



