Short answer
Run sudo apt update and sudo apt install -y fail2ban. On Ubuntu 26.04 and 24.04 that is all: the SSH jail is on by default and starts at once. An address with 5 failed logins in 10 minutes is banned for 10 minutes. Check it with sudo fail2ban-client status sshd and unban with sudo fail2ban-client set sshd unbanip <address>.
Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04? One section differs, each step marked “On Ubuntu 24.04” right under it: If fail2ban fails with “No module named ‘asynchat’”. Everything that differed
Before you start
- An Ubuntu 26.04 or 24.04 server and a user with sudo (create one).
- fail2ban does not need a firewall to work, but this test server had the UFW firewall on, as most servers should:
$ sudo ufw statusStatus: active To Action From-- ------ ----OpenSSH ALLOW AnywhereOpenSSH (v6) ALLOW Anywhere (v6)Step 1: Install fail2ban
Update the package list first, so you get the newest package (on 24.04 the first one was broken, see the asynchat error):
$ sudo apt update[… 1 lines not shown …]Hit:2 https://mirror.hetzner.com/ubuntu/packages resolute-updates InReleaseHit:3 https://mirror.hetzner.com/ubuntu/packages resolute-backports InReleaseHit:4 https://mirror.hetzner.com/ubuntu/security resolute-security InRelease158 packages can be upgraded. Run 'apt list --upgradable' to see them.$ sudo apt install -y fail2banInstalling: fail2ban Installing dependencies: python3-pyasyncore python3-pyinotify whois Suggested packages: mailx monit sqlite3 python-pyinotify-doc [… 43 lines not shown …]python3-pyasyncore is in that list on purpose: it is the part of Python that fail2ban needs and newer Pythons no longer have. Check the version you got: 26.04 gives 1.1.0-9, and on 24.04 it must be 1.0.2-3ubuntu0.1 or newer:
$ apt-cache policy fail2banfail2ban: Installed: 1.1.0-9 Candidate: 1.1.0-9[… 4 lines not shown …]Step 2: Check that it is running
The service starts straight after the install and starts again after every reboot (enabled):
$ sudo systemctl status fail2ban --no-pager● fail2ban.service - Fail2Ban Service Loaded: loaded (/usr/lib/systemd/system/fail2ban.service; enabled; preset: enabled) Active: active (running) since Wed 2026-11-18 12:21:16 UTC; 10s ago Invocation: ee79b427f1594d7d8eee35b84ecea96d Docs: man:fail2ban(1) Main PID: 33528 (fail2ban-server) Tasks: 5 (limit: 3647) Memory: 18.3M (peak: 20.7M) CPU: 276ms CGroup: /system.slice/fail2ban.service └─33528 /usr/bin/python3 /usr/bin/fail2ban-server -xf start Nov 18 12:21:16 web-server systemd[1]: Started fail2ban.service - Fail2Ban Service.Nov 18 12:21:16 web-server fail2ban-server[33528]: Server readyAsk fail2ban which jails it runs. A jail is one service it watches. Ubuntu turns on one, sshd:
$ sudo fail2ban-client statusStatus|- Number of jail: 1`- Jail list: sshdStep 3: See what Ubuntu turned on
Many guides start by copying jail.conf and turning the SSH jail on. On Ubuntu you do not need to: the package ships this file. The two releases differ here: 26.04 lists [DEFAULT] first and sets journalmatch for ssh.service, 24.04 sets backend = systemd under [DEFAULT]:
$ cat /etc/fail2ban/jail.d/defaults-debian.conf[DEFAULT]banaction = nftablesbanaction_allports = nftables[type=allports] [sshd]backend = systemdjournalmatch = _SYSTEMD_UNIT=ssh.service + _COMM=sshdenabled = trueenabled = trueunder[sshd]: SSH is protected from the start.backend = systemd: fail2ban reads SSH’s log from the systemd journal (on 26.04 it is set under[sshd], next to thejournalmatchline). Guides that addlogpath = /var/log/auth.logare written for older releases.banaction = nftables: a ban is a firewall rule in nftables, the firewall built into the kernel.
The limits, in seconds: 5 failures within 600 seconds get a 600-second ban:
$ sudo fail2ban-client get sshd maxretry && sudo fail2ban-client get sshd findtime && sudo fail2ban-client get sshd bantime5600600Step 4: Watch it ban an attacker
In the test, another computer, 198.51.100.2, tried to log in as admin five times, the way bots do all day. That user does not exist, and the server takes only keys, so sshd refused each try and logged it:
$ sudo journalctl -u ssh --no-pager | grep 'Invalid user' | tail -n 2Nov 18 12:22:10 web-server sshd-session[34241]: Invalid user admin from 198.51.100.2 port 54768Nov 18 12:22:16 web-server sshd-session[34309]: Invalid user admin from 198.51.100.2 port 38092After the fifth failure, fail2ban banned the address. It counts failures as they are logged, so the ban can come after more than five tries. Banned IP list shows who is banned right now. The test server is open to the internet, so real bots were banned too: their addresses show here as 198.18.0.N, which is how the run log hides strangers’ addresses:
$ sudo fail2ban-client status sshdStatus for the jail: sshd|- Filter| |- Currently failed: 4| |- Total failed: 92| `- Journal matches: _SYSTEMD_UNIT=ssh.service + _COMM=sshd`- Actions |- Currently banned: 4 |- Total banned: 4 `- Banned IP list: 198.18.0.2 198.18.0.3 198.18.0.4 198.51.100.2fail2ban’s own log says when and why:
$ sudo grep 'Ban ' /var/log/fail2ban.log2026-11-18 12:21:17,053 fail2ban.actions [33528]: NOTICE [sshd] Ban 198.18.0.22026-11-18 12:21:17,116 fail2ban.actions [33528]: NOTICE [sshd] Ban 198.18.0.32026-11-18 12:21:17,131 fail2ban.actions [33528]: NOTICE [sshd] Ban 198.18.0.42026-11-18 12:22:17,366 fail2ban.actions [33528]: NOTICE [sshd] Ban 198.51.100.2The banned computer no longer even reaches the login. It gets “Connection refused” at once:
$ ssh admin@203.0.113.10ssh: connect to host 203.0.113.10 port 22: Connection refusedThe ban is this nftables rule. priority filter - 1 means it runs before ufw’s rules, which is why it works even though ufw allows SSH:
$ sudo nft list table inet f2b-tabletable inet f2b-table { set addr-set-sshd { type ipv4_addr elements = { 198.18.0.4, 198.18.0.3, 198.18.0.2, 198.51.100.2 } } chain f2b-chain { type filter hook input priority filter - 1; policy accept; tcp dport 22 ip saddr @addr-set-sshd reject with icmp port-unreachable }}So ufw status never shows a ban. Look in fail2ban, not in ufw:
$ sudo ufw statusStatus: active To Action From-- ------ ----OpenSSH ALLOW AnywhereOpenSSH (v6) ALLOW Anywhere (v6)Only the failing address is banned. Your own session carried on as normal the whole time.
Step 5: Unban an address
If a real user, or you, got banned, unban the address. The 1 means one address was unbanned:
$ sudo fail2ban-client set sshd unbanip 198.51.100.21It works at once. The same computer reaches the login again (and is refused for having no key, as before):
$ ssh admin@203.0.113.10admin@203.0.113.10: Permission denied (publickey,password).Step 6: Your own settings, and never ban your office
Never edit jail.conf. The file says so itself, and tells you to put your changes in jail.local instead:
$ grep -n 'YOU SHOULD NOT MODIFY' /etc/fail2ban/jail.conf11:# YOU SHOULD NOT MODIFY THIS FILE.Put your changes in /etc/fail2ban/jail.local (sudo nano /etc/fail2ban/jail.local). This one bans for an hour and never bans 192.0.2.2. Put your own fixed address there instead. Leave it out if your address changes often, as many home connections do:
[DEFAULT]ignoreip = 127.0.0.1/8 ::1 192.0.2.2bantime = 1hLoad it. The allowipv6 warning is harmless; OK is what counts:
$ sudo fail2ban-client reloadOK$ sudo fail2ban-client get sshd bantime && sudo fail2ban-client get sshd ignoreip3600These IP addresses/networks are ignored:|- 127.0.0.0/8|- 192.0.2.2`- ::1In the test, 192.0.2.2 then failed six logins in a row. fail2ban noted each one and did not ban it:
$ sudo grep 'Ignore 192.0.2.2' /var/log/fail2ban.log | tail -n 12026-11-18 12:24:01,433 fail2ban.filter [33528]: INFO [sshd] Ignore 192.0.2.2 by ip$ sudo fail2ban-client status sshdStatus for the jail: sshd|- Filter| |- Currently failed: 5| |- Total failed: 95| `- Journal matches: _SYSTEMD_UNIT=ssh.service + _COMM=sshd`- Actions |- Currently banned: 4 |- Total banned: 5 `- Banned IP list: 198.18.0.2 198.18.0.3 198.18.0.4 198.18.0.5If fail2ban fails with “No module named ‘asynchat’”
This happens on Ubuntu 24.04 only. On 26.04 the package starts without trouble.
On Ubuntu 24.04
The fail2ban package Ubuntu 24.04 was released with (1.0.2-3) does not start on its Python 3.12. You see it on a server that still has that package. The test installed that release package to show it:
$ sudo systemctl is-active fail2banfailed$ sudo journalctl -u fail2ban --no-pager | grep asynchat | tail -n 1Nov 18 22:07:38 web-server fail2ban-server[66591]: 2026-11-18 22:07:38,585 fail2ban [66591]: ERROR No module named 'asynchat'$ apt-cache policy fail2banfail2ban: Installed: 1.0.2-3 Candidate: 1.0.2-3ubuntu0.1[… 6 lines not shown …]Installed: 1.0.2-3 is the broken one. Do not install anything with pip: the fix is Ubuntu’s own update. Install it, and your jail.local is kept:
$ sudo apt update && sudo apt install -y fail2ban$ sudo systemctl is-active fail2banactiveUndo
To stop fail2ban and keep it off after a reboot:
$ sudo systemctl disable --now fail2banSynchronizing state of fail2ban.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.Executing: /usr/lib/systemd/systemd-sysv-install disable fail2banRemoved '/etc/systemd/system/multi-user.target.wants/fail2ban.service'.When it stops, it removes its nftables table, so no ban is left behind. Only ufw’s tables remain:
$ sudo nft list tablestable ip filtertable ip6 filterWhat these steps changed on the server
Both runs recorded the server before the first step and after the last (see the Tested on box below). The install added fail2ban, python3-pyasyncore, python3-pyinotify and whois, about 6 MB on disk on both releases, and enabled one service, fail2ban.service. No port was opened or closed. A scan of the 1,000 most common TCP ports from a second server found only SSH (22) open before and after: the other 999 give no answer, because ufw drops them. A ban is invisible to the scan, because it only affects the address that failed.
Lynis, run on the test server before and after, moved from 61 to 62 on both releases, with the same 2 warnings and the same number of suggestions. That is Lynis’s own score for that one server, not a certificate.
Ubuntu 26.04 vs 24.04: what changed
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-30-generic | Ubuntu 24.04.4 LTS6.8.0-138-generic |
| fail2ban | 1.1.0-9 | 1.0.2-3ubuntu0.1 |
| python3 | 3.14.3-0ubuntu2 | 3.12.3-0ubuntu2.1 |
| nftables | 1.1.6-1 | 1.0.9-1ubuntu0.1 |
| openssh-server | 1:10.2p1-2ubuntu3.5 | 1:9.6p1-3ubuntu13.18 |
| fail2ban package | 1.1.0-9 | 1.0.2-3ubuntu0.1 (the release package 1.0.2-3 does not start) |
defaults-debian.conf | [DEFAULT] first, backend = systemd and a journalmatch for ssh.service under [sshd] | backend = systemd under [DEFAULT], no journalmatch line |
Journal matches shown by fail2ban-client status sshd | _SYSTEMD_UNIT=ssh.service + _COMM=sshd | _SYSTEMD_UNIT=sshd.service + _COMM=sshd |
| The “No module named ‘asynchat’” failure | does not happen | only with the release package 1.0.2-3; apt update fixes it |
| ban, unban, ignoreip, the nftables rule before ufw | same text | same text |
| Proof | Run log, 32 checks passed | Run log, 38 checks passed |
