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:
sudo apt updatesudo apt install fail2banOn 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:
cat /etc/fail2ban/jail.d/defaults-debian.conf[DEFAULT]banaction = nftablesbanaction_allports = nftables[type=allports]backend = systemd
[sshd]enabled = truebackend = 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:
sudo systemctl is-active fail2bansudo fail2ban-client statusactiveStatus|- Number of jail: 1`- Jail list: sshdThen look at the SSH jail:
sudo fail2ban-client status sshdStatus 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:
sudo fail2ban-client get sshd maxretrysudo fail2ban-client get sshd findtimesudo fail2ban-client get sshd bantime5600600With 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:
sudo tee /etc/fail2ban/jail.d/sshd.local >/dev/null <<'EOF'[sshd]bantime = 1hEOFTest the configuration, then restart Fail2Ban to load it:
sudo fail2ban-client -tsudo systemctl restart fail2bansudo fail2ban-client get sshd bantimeThe 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:
sudo fail2ban-client set sshd unbanip 203.0.113.10Replace 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:
sudo nft list table inet f2b-tableBanned 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:
sudo tail -n 20 /var/log/fail2ban.logsudo journalctl -u fail2ban --since '15 minutes ago' --no-pagerIf 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:
- Set up SSH keys and connect to your server, so there are no passwords for bots to guess.
- Close the ports Docker opens past UFW, so containers are not exposed by accident.
- Run the Linux server health-check script, so you can see the rest of the server’s state in one report.
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.