Blog

Install Fail2Ban on Ubuntu to Protect SSH

Install Fail2Ban on Ubuntu 24.04, check that the SSH jail sees failed logins, change the ban time, and remove a ban when you block the wrong address.

Max Diamond 5 min read

Put a server on a public IP and its SSH log soon fills with failed logins for users like root, admin and ubuntu, from addresses you have never seen. Bots try common passwords on every address they can reach. SSH keys stop them getting in, but the attempts continue and bury the failures that matter in the log.

Fail2Ban reads the SSH log and blocks an address after repeated failures. This guide installs it, checks that the SSH jail sees failed logins, changes the ban time, and shows how to remove a ban when you block the wrong address. We tested every command on Ubuntu 24.04 with Fail2Ban 1.0.2.

Installing Fail2Ban from Ubuntu’s package repository

Refresh the package index and install Fail2Ban:

Terminal window
sudo apt update
sudo apt install fail2ban

On Ubuntu 24.04 the package starts Fail2Ban, enables it at boot, and turns on the SSH jail. The SSH jail is enabled by /etc/fail2ban/jail.d/defaults-debian.conf, which the package installs:

Terminal window
cat /etc/fail2ban/jail.d/defaults-debian.conf
[DEFAULT]
banaction = nftables
banaction_allports = nftables[type=allports]
backend = systemd
[sshd]
enabled = true

backend = systemd makes Fail2Ban read failed logins from the systemd journal instead of a log file. banaction = nftables makes it block an address with an nftables rule.

Checking that the SSH jail is running

Check the service and list the jails:

Terminal window
sudo systemctl is-active fail2ban
sudo fail2ban-client status
active
Status
|- Number of jail: 1
`- Jail list: sshd

Then look at the SSH jail:

Terminal window
sudo fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 0
|- Total banned: 0
`- Banned IP list:

Total failed counts the failed logins the jail has seen. A new installation shows zero.

The journal match names sshd.service, but the SSH service on Ubuntu 24.04 is ssh.service. The jail still sees the failures, because the + means “or”, and _COMM=sshd matches the SSH process by name.

Understanding when an address gets banned

Three settings control a ban. maxretry is the number of failures that triggers it, findtime is the window those failures must fall in, and bantime is how long the ban lasts. Read the current values:

Terminal window
sudo fail2ban-client get sshd maxretry
sudo fail2ban-client get sshd findtime
sudo fail2ban-client get sshd bantime
5
600
600

With the defaults, an address that fails 5 times in 600 seconds (10 minutes) is blocked from port 22 for 10 minutes.

Changing the ban time

Keep your changes in a .local file. A package upgrade can replace the .conf files, but it does not touch .local files. This example bans an address for one hour:

Terminal window
sudo tee /etc/fail2ban/jail.d/sshd.local >/dev/null <<'EOF'
[sshd]
bantime = 1h
EOF

Test the configuration, then restart Fail2Ban to load it:

Terminal window
sudo fail2ban-client -t
sudo systemctl restart fail2ban
sudo fail2ban-client get sshd bantime

The test prints OK: configuration test is successful, and bantime is now 3600. The test can also print a warning that allowipv6 is not defined. That warning is harmless: Fail2Ban uses its default, auto.

Before you make bans stricter, keep a second SSH session open and make sure you have console access to the server. A strict jail can ban you, or a team that shares one office IP.

Removing a ban from the wrong address

Find the address in the jail’s Banned IP list, then unban only that address:

Terminal window
sudo fail2ban-client set sshd unbanip 203.0.113.10

Replace 203.0.113.10 with the address you found. The command prints 1 when it removes a ban. Then find out why the address failed, for example an old password in a deploy script, so the ban does not come back.

To see how the ban works, list the nftables rule that Fail2Ban adds:

Terminal window
sudo nft list table inet f2b-table

Banned addresses are in the set addr-set-sshd, and a rule rejects their connections to port 22.

Troubleshooting a jail that never bans

If Total failed stays at zero, first check where you test from. Fail2Ban ignores failed logins from the server’s own addresses (ignoreself). A test from the server itself shows up in the log as Ignore 127.0.0.1 by ignoreself rule and is not counted. Test from another machine that you can afford to ban.

Check the Fail2Ban log for errors and ignored addresses:

Terminal window
sudo tail -n 20 /var/log/fail2ban.log
sudo journalctl -u fail2ban --since '15 minutes ago' --no-pager

If the jail is missing from fail2ban-client status, check that /etc/fail2ban/jail.d/sshd.local starts with [sshd], then run sudo fail2ban-client -t again. If you moved SSH to a different port, add port = <your port> to the same [sshd] section, or the ban blocks port 22 while SSH listens somewhere else.

For filter problems, see the upstream Fail2Ban troubleshooting guide.

Conclusion

You installed Fail2Ban, checked that the SSH jail sees failed logins, set the ban time to one hour, and removed a ban by hand. An address that fails to log in 5 times in 10 minutes is now blocked from port 22 for an hour. Fail2Ban only acts on failures it can read, so use it with SSH keys and a firewall, not in place of them.

To secure the server further:

Serversinc checks every day whether Fail2Ban is enabled on your servers, and shows the result in the server’s Insights tab. See what the check covers.

Run your apps on servers you own.

Connect a server you already run, or provision one on your cloud account. £5 a month covers the first three.