If you run a public-facing Ubuntu server, someone is already trying to log in to it. Check the SSH auth log on any server online for a week and you will find hundreds of failed attempts from strangers, mostly automated scripts guessing common usernames and passwords. To install Fail2Ban on Ubuntu Server is to put an automatic guard on that door: the tool watches your logs, spots repeated failures from one address, and blocks that address at the firewall.
This guide covers the full setup on Ubuntu 22.04 and 24.04: installation, the SSH jail, protection for Nginx, sensible ban settings, and safe testing. You need a server you can reach over SSH and a user account with sudo. The whole job takes fifteen minutes.
What Fail2Ban Does and When It Helps
Fail2Ban is a log-watching daemon. It reads service logs, matches failure patterns, and when one IP crosses a threshold, it adds a firewall rule that drops that IP’s traffic for a set time. When the ban expires, the rule is removed automatically. It does not replace a firewall or key-based login. It adds a layer that makes brute-force attacks slow and expensive for the attacker.
Fail2Ban helps most against password guessing on SSH, failed logins on web panels, and bots hammering WordPress login pages. It does not stop a single well-crafted attack, a stolen password, or an unpatched vulnerability. Think of it as a bouncer that throws out anyone who keeps rattling the door handle, not a replacement for the lock. If you have not hardened SSH yet, read our guide on how to harden SSH on Ubuntu 24.04 with key-only login and Fail2Ban alongside this one, because disabling password login removes most of the risk Fail2Ban guards against.
Before You Start: A Safety Net First
Fail2Ban bans by IP, and a misconfigured rule can ban your own address. Before you touch it, make sure you have a way back in: a second route such as your provider’s web console or a VPN exit, a spare SSH session left open while you test (an existing connection is not cut off when your IP is banned), and your own IP whitelisted in Fail2Ban’s ignoreip setting before you enable any jail.
Fail2Ban also works best next to a proper firewall. If UFW is not set up yet, our walkthrough on how to configure UFW firewall rules for a web server step by step covers what a web server needs, and Fail2Ban uses UFW as its banning backend by default on Ubuntu.
Install Fail2Ban on Ubuntu Server Step by Step
Fail2Ban ships in Ubuntu’s default repositories, so installation is one command. Update the package index first, then install the package:
sudo apt update
sudo apt install fail2ban -y
Then enable the service so it starts on boot, and start it now:
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
Check it with sudo systemctl status fail2ban. You should see active (running). Out of the box, only the SSH jail is enabled. That is a usable starting point, but the defaults deserve a look before you trust them.
One habit saves trouble later: never edit /etc/fail2ban/jail.conf directly, because package updates overwrite it. Create /etc/fail2ban/jail.local and put only your overrides there. Anything in the local file wins.
Configure the SSH Jail
A jail is a bundle of three things: a log file to watch, a filter that defines what a failure looks like, and an action to take when the threshold is crossed. The fail2ban sshd jail watches /var/log/auth.log, matches failed password attempts, and bans the source IP. Three settings control every jail: maxretry (failures that trigger a ban, default 5), findtime (the window those failures must fall inside, default 10 minutes), and bantime (how long the ban lasts, default 10 minutes).
Create /etc/fail2ban/jail.local with your SSH settings. This example enables the jail, whitelists your own IP, and sets a firmer ban than the default:
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 203.0.113.25
bantime = 1h
findtime = 10m
maxretry = 5
[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
Replace 203.0.113.25 with your real public IP. After saving, reload with sudo fail2ban-client reload and confirm the jail is active with sudo fail2ban-client status sshd, which lists banned IPs and the total failure count. If you changed the SSH port, update the port line to match, or Fail2Ban will ban the right IP on the wrong port.
Protect Nginx and Other Services
SSH is the usual target, but web services get hammered too. Fail2Ban ships with filters for Nginx, Apache, and common applications, so adding a jail is mostly a matter of naming the right filter and log file. To guard Nginx against bots probing for login pages and scanning for vulnerabilities, add this to jail.local:
[nginx-http-auth]
enabled = true
port = http,https
logpath = %(nginx_error_log)s
[nginx-botsearch]
enabled = true
port = http,https
logpath = %(nginx_access_log)s
maxretry = 2
The nginx-botsearch jail catches requests for paths that should not exist, which is the signature of vulnerability scanners. A maxretry of 2 is reasonable here because a real visitor almost never requests two nonexistent scanner paths in a row. For WordPress sites, the same approach works with a jail watching wp-login.php failures, and our earlier guide on how to install and configure Fail2Ban for SSH and Nginx on Ubuntu walks through a combined SSH and Nginx setup in more detail. On a small VPS, five to eight jails is comfortable. Enabling every shipped filter at once is not, since most watch services you do not run.
Tune Ban Time, Find Time, and Max Retry
Defaults are safe, but they are not tuned to your server. A few rules of thumb help you set values that block attackers without punishing real users:
- Ban time: one hour is a sensible starting point for SSH. Repeat offenders can be banned longer with
bantime.increment = true, which grows the ban each time the same IP reoffends, so a first ban of one hour can become a day after several repeats. - Find time: keep it short, around ten minutes. A longer window catches slow attacks but also catches a user who mistypes a password, walks away, and tries again later.
- Max retry: five is fair for SSH with password login. If you use key-only login, drop it to three, because a legitimate key user should never produce a failed password attempt at all.
Short bans with escalating repeat bans beat one long ban for most small servers. A bot that returns after an hour gets banned again, longer each time, while a real user who mistypes a password is only out for an hour at most. This fail2ban jail configuration keeps protection strict without turning a typo into a day-long lockout, and the ubuntu server brute force protection it provides costs far less than the CPU wasted answering thousands of login attempts.
Test Your Setup Without Locking Yourself Out
Testing matters, and testing from your own IP is how people lock themselves out. Use a second machine, a phone on mobile data, or a cheap cloud instance as the test source, and keep your main SSH session open throughout.
From the test machine, deliberately fail five SSH logins with a wrong password. Then, on the server, run sudo fail2ban-client status sshd. You should see the test IP listed under banned IPs. Try to connect from the test machine again: the connection should time out or be refused immediately, not ask for a password. To lift the ban early, run sudo fail2ban-client set sshd unbanip <test-ip>, and check your own IP is not banned before you close the spare session. Fail2Ban logs every ban to /var/log/fail2ban.log, so if something behaves oddly later, sudo tail -n 50 /var/log/fail2ban.log is the first place to look.
What Fail2Ban Cannot Do
Fail2Ban is one layer, and it has hard limits worth knowing before you rely on it. It only reacts to failures written to a log, so an attack that never fails a login, like a distributed slow scan or a zero-day exploit, passes straight through. It cannot protect a service whose logs it is not watching, and it cannot fix weak passwords. An attacker who guesses the right password on the first try is never banned.
Frequently Asked Questions
How do I whitelist my own IP permanently?
Add it to the ignoreip line in the [DEFAULT] section of jail.local, then reload Fail2Ban. You can list several IPs separated by spaces, and whole subnets in CIDR form such as 203.0.113.0/24. Whitelisted IPs are never banned by any jail.
Can Fail2Ban protect WordPress logins?
Yes, with a jail that watches your web server access log for repeated wp-login.php POST requests. This is one of the most useful jails on a WordPress VPS, because login bots are relentless and a plugin-only limit resets when the attacker switches IPs. Pair it with strong passwords and, where possible, two-factor authentication.