Put a fresh VPS online with SSH on port 22 and password login enabled, and automated login bots will find it fast, often within the hour. They hammer the port with thousands of username and password guesses around the clock. Fail2Ban is the standard, free answer to that noise: it watches your SSH logs, spots repeated failed logins, and blocks the attacking IP at the firewall for a set period. This guide walks you through installing Fail2Ban on Ubuntu, writing a correct jail.local for SSH, testing that bans actually work, and tuning the values for a real server.
How Fail2Ban Works in Plain Terms
Fail2Ban runs as a background service with three moving parts. A filter is a set of regular expressions that match failed-login lines in your logs (for SSH, the sshd filter). A jail ties a filter to a log file and decides what happens when the pattern repeats: how many failures, over what time window, and how long the ban lasts. An action applies the ban, usually by adding an iptables or nftables rule that drops the attacker’s traffic.
The defaults are sane, but three numbers do the heavy lifting: maxretry (failed attempts before a ban), findtime (the window those failures must fall inside), and bantime (how long the ban lasts). Set them sensibly and bots get locked out while legitimate users barely notice.
Prerequisites
You need a VPS running Ubuntu 20.04 or newer (the same steps work on Debian), a user with sudo access, and SSH running and reachable. Before touching Fail2Ban, set up SSH key authentication and confirm you can log in with your key. If you accidentally lock yourself out during testing, you need a working key login, or console access from your provider, to fix it.
Step 1: Install Fail2Ban
Fail2Ban ships in Ubuntu’s default repositories, so installation is one command:
sudo apt update && sudo apt install fail2ban -y
Verify it installed and check the version:
fail2ban-client --version
The service starts automatically after installation. Leave the default /etc/fail2ban/jail.conf file alone: package upgrades can overwrite it. Every customization goes in /etc/fail2ban/jail.local, which the service reads as an override.
Step 2: Create a jail.local for SSH
Create the file and add an SSH jail. The key lines are filter = sshd (the built-in filter that matches OpenSSH failure lines), port = ssh, and the three timing values:
sudo nano /etc/fail2ban/jail.local
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 5
findtime = 10m
bantime = 1h
What this does: an IP gets banned for one hour after 5 failed SSH logins within any 10-minute window. Fail2Ban writes the ban to your firewall automatically and removes it when the hour expires.
Two settings worth adding in the [DEFAULT] section of the same file. First, ignoreip whitelists addresses that must never be banned: always include your own public IP (and 127.0.0.1/8):
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 203.0.113.45
Replace 203.0.113.45 with your real public IP, which you can get with curl -4 ifconfig.me. Second, backend = systemd is worth setting on systems where the auth log is unreliable; the default auto detection usually picks the right one, so leave it unless bans are not triggering.
Step 3: Restart and Enable the Service
Restart Fail2Ban so it picks up jail.local, and make sure it starts on boot:
sudo systemctl restart fail2ban
sudo systemctl enable fail2ban
sudo fail2ban-client status sshd
You should see the jail listed with Currently failed: 0 and Currently banned: 0, plus a filter summary. If the jail is missing from the status output, the config did not load: check for typos with sudo fail2ban-client -d, which dumps the effective configuration.
Step 4: Test That Bans Actually Work
Do not skip this. From a second machine or phone hotspot (never from your whitelisted IP) and deliberately fail SSH logins against your server more times than maxretry. Then, on the server, confirm the IP is banned:
sudo fail2ban-client status sshd
The banned IP should appear under Banned IP list. You can also inspect the firewall rule directly:
sudo iptables -L f2b-sshd -n
To release a test ban early:
sudo fail2ban-client set sshd unbanip 198.51.100.23
If bans never trigger, the usual culprits are: a wrong logpath (check that /var/log/auth.log exists and contains sshd lines), an aggressive ignoreip covering the test IP, or a typo in the jail name: it must be [sshd], not [ssh].
Tuning bantime, findtime, and maxretry
The defaults above are a good starting point, but here is how to think about each value:
- maxretry = 5. Low enough to stop bots, high enough that a user mistyping a password twice is not punished. Going below 3 risks locking out legitimate users; above 8 gives bots too much room.
- findtime = 10m. A 10-minute window catches burst attacks. Slow, distributed attempts spread over hours will slip past a short window — that is a trade-off, not a bug.
- bantime = 1h. One hour deters most bots without permanently locking anyone out. If your logs show the same IPs returning, raise this to
12hor24h. Fail2Ban also supportsbantime.increment = truefor repeat offenders, which multiplies the ban each time the same IP is caught again.
After changing any value, restart Fail2Ban and re-run the status check.
Common Mistakes to Avoid
Editing jail.conf instead of jail.local. Your changes get wiped on the next package update. Always use jail.local.
Testing from your own IP while it is whitelisted. Whitelisted IPs can never be banned, so a test from there “proves” nothing. Use a different network for the test.
No SSH key access before enabling bans. Password-only login plus a strict jail is how admins lock themselves out of their own servers. Set up keys first.
Forgetting fail2ban is log-driven. If your server logs SSH failures to the systemd journal instead of /var/log/auth.log (common on newer Ubuntu installs), the default logpath sees nothing. Check your logs, then set backend = systemd if needed.
What to Protect Next
Once the SSH jail is working, the same pattern covers other services: Fail2Ban ships with ready-made filters for Nginx, Apache, Postfix, and Dovecot. And remember what Fail2Ban is not. It is one layer, not a strategy. Keep SSH key-only, disable password authentication entirely once keys work, and consider moving SSH off port 22 to cut the noise further. A banned IP list and key-only SSH together take brute-force attacks off your worry list for good. Pair it with automatic security updates on Ubuntu and Debian so patched packages land without manual work.