Security and hardening · Intermediate · 11 min
How to Block Brute-Force Logins with Fail2ban
By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · Fail2ban 1.1
You will install Fail2ban 1.1 on Ubuntu 26.04, set it up to ban any address that keeps failing SSH logins, test the rule against a sample log line, and learn how to see and undo bans.
On this page
Bots try thousands of passwords against every SSH server they can find. Key-only logins make those attempts fail, but the attempts still fill your logs and use CPU. Fail2ban reads your logs, counts failed logins per address, and tells the firewall to drop traffic from addresses that fail too often.
In this tutorial you will install Fail2ban, write a small settings file, test it, and learn the commands for checking and undoing bans. Fail2ban complements SSH hardening, it does not replace it: switch to key-only logins first, as in /tutorials/harden-ssh-on-ubuntu-26-04.
Step 1 · Install Fail2ban
sudo apt-get update
sudo apt-get install -y fail2banUbuntu 26.04 ships Fail2ban 1.1. The package registers the service to start at boot. Fail2ban uses nftables to block addresses, and apt also installs the nftables tools it needs. Check with systemctl is-active fail2ban if you want to be sure it is running.
Check it worked
fail2ban-client --version | head -n 1The output starts with Fail2Ban v1.1.
Step 2 · Learn which files to edit
Fail2ban ships a large jail.conf file. A jail is one rule set: which log to watch, which pattern means a failure, and what to do about it. Never edit jail.conf, because package updates replace it. Put your own settings in /etc/fail2ban/jail.local, which overrides it.
Ubuntu also ships a file that turns on the SSH jail. Look at it:
cat /etc/fail2ban/jail.d/defaults-debian.confThe output sets banaction = nftables in [DEFAULT] and enables the [sshd] jail, reading the journal for the ssh service. So SSH protection is already on with the stock settings. You will tune them next.
Step 3 · Write your settings
Settings in [DEFAULT] apply to every jail. The [sshd] section holds those that belong to the SSH jail. Replace 203.0.113.5 with your own public IP address. It goes in ignoreip, a list of addresses that are never banned, and it stops you from banning yourself while testing.
sudo tee /etc/fail2ban/jail.local > /dev/null <<'CONF'
[DEFAULT]
# Never ban these. Add your own public IP address.
ignoreip = 127.0.0.1/8 ::1 203.0.113.5
# Ban an address that fails maxretry times within findtime.
findtime = 10m
maxretry = 4
bantime = 1h
# Make repeat offenders wait longer each time, up to a week.
bantime.increment = true
bantime.maxtime = 1w
[sshd]
enabled = true
CONFWhat the numbers mean: an address that fails four times within ten minutes is banned for an hour. With bantime.increment, each repeat ban for the same address lasts longer than the one before, up to the bantime.maxtime of one week. The first ban is still one hour, and Fail2ban decides the growth with its default formula, which you can change with bantime.factor. Make maxretry smaller if you only use keys, because a real user with a key will never fail four times. Do not set it to 1: a mistyped command or a wrong key can hit it.
Check it worked
Fail2ban has a configuration test. It reads all the files and reports errors without changing anything. Run it every time you edit:
sudo fail2ban-client -tThe last line is OK: configuration test is successful. If it names a file and line, fix that line first.
Step 4 · Test the SSH filter against a log line
A filter is a set of patterns that mean a failed login. fail2ban-regex tests a log line against one. Make a line that looks like a real failure from the documentation address 198.51.100.7:
echo "Nov 15 10:00:01 host sshd[1234]: Failed password for invalid user admin from 198.51.100.7 port 4022 ssh2" > /tmp/ssh-sample.log
fail2ban-regex /tmp/ssh-sample.log sshdThe output ends with Lines: 1 lines, 0 ignored, 1 matched, 0 missed. The filter found the address in the line, which is what it will do with real log lines. If matched is 0, the filter does not recognise your log format, and no ban would ever happen.
To test the filter against the real journal on your server:
sudo fail2ban-regex systemd-journal sshdIt prints how many real failed logins from your history the filter matched. A server that has been online for a day usually matches some.
Step 5 · Apply the settings and check the jail
Restart Fail2ban so it reads the file:
sudo systemctl restart fail2ban
sudo systemctl status fail2banThe status shows active (running).
Check it worked
sudo fail2ban-client status
sudo fail2ban-client status sshdThe first command lists Number of jail: 1 and Jail list: sshd. The second shows the jail's counters: Currently failed and Total failed count failed logins seen in the log, and Currently banned and Total banned count the addresses blocked. On a server that is online, Total failed usually grows within hours. The Banned IP list line names the addresses.
Troubleshooting
ERROR NOK: ("Failed to access socket path"...orConnection refused: the service is not running. Start it withsudo systemctl start fail2banand readsudo journalctl -u fail2ban -n 50.Total failedstays 0 though you know logins failed: the filter is not reading the right place. Runsudo fail2ban-regex systemd-journal sshdand check that it matches lines.ERROR Failed during configuration: ... Bad value substitution: a typo injail.local.sudo fail2ban-client -tnames the section.- You banned yourself: a new SSH connection will not work. Use your provider's web console (see
/tutorials/harden-ssh-on-ubuntu-26-04for the console password you need) or an SSH session that was already open, and runsudo fail2ban-client set sshd unbanip <your address>. Add your address toignoreipso it does not happen again.
Step 6 · Ban and unban by hand
You can test the chain, from a ban to the firewall rule, without waiting for an attacker. Use a documentation address that is not yours, and never ban the address you are connected from: a banned address cannot reach SSH at all, so you could not log in to lift the ban. If you do ban yourself, you need an SSH session that was already open, or your provider's web console, to run the unban command. Ban the documentation address 203.0.113.50 in the sshd jail, look at the jail, and lift the ban:
sudo fail2ban-client set sshd banip 203.0.113.50
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip 203.0.113.50The first command replies with 1, and the status lists 203.0.113.50 under Banned IP list. The last command replies with 1 and removes the ban. The same command lifts a ban on a real address. If it is yours, run it from a session that was already open or from the provider's console, because a banned address cannot open a new SSH connection.
While an address is banned, the firewall drops its packets. List what Fail2ban has put in the firewall with:
sudo nft list rulesetThe output includes a table named f2b-table with a set of banned addresses, one set per jail. If you also use UFW, the two work side by side, because Fail2ban adds its own table.
To see what Fail2ban has done, read its log:
sudo tail -n 5 /var/log/fail2ban.logLines such as [sshd] Ban 203.0.113.50 and [sshd] Unban 203.0.113.50 tell you what happened and when. Your manual test appears here.
Step 7 · Add a jail for another service
The same method protects other services. A jail for a service needs a filter that matches its failure line and a log to read. Fail2ban ships filters for many, listed in /etc/fail2ban/filter.d/:
ls /etc/fail2ban/filter.d | head -n 20To enable one, add a section to jail.local, for example [nginx-http-auth] with enabled = true for HTTP basic-auth failures. Run sudo fail2ban-client -t, then restart the service, then check sudo fail2ban-client status for the new jail. Test any new filter with fail2ban-regex on a real log file before you trust it.
Next steps
- Close every port you do not use:
/tutorials/set-up-ufw-firewall-ubuntu-26-04. - Keep the server patched without logging in:
/tutorials/automatic-security-updates-ubuntu. - See what else could be exposed:
/tutorials/audit-a-server-with-lynis. - The reference is
man jail.confand the project's documentation at fail2ban.readthedocs.io.
Related tutorials
- How to Do the Initial Setup of a New Ubuntu 26.04 ServerYou will take a fresh Ubuntu 26.04 server from a root-only login to an updated machine with a daily-use account, your SSH key on it, a firewall and a known clock.
- How to Log In to a Linux Server with SSH KeysYou will create an Ed25519 SSH key, install its public half on a server, log in without a password, and save the connection as a short name.
- How to Create a Sudo User on Ubuntu 26.04You will create a user that can run administrative commands with sudo, test it, limit it to specific commands, and take the access away again.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials