fail2ban on Ubuntu: install, jail.local, sshd jail and unban
fail2ban on Ubuntu 24.04 and 26.04: what it does on a key-only server, the journal backend, the auth.log error, jail.local settings, nftables, unbanip, recidive.
fail2ban reads your logs, and when one address fails to log in too often it blocks that address in the firewall for a while. Install it with sudo apt install fail2ban: on Ubuntu 24.04 (version 1.0.2) and 26.04 (1.1.0) the package turns on the SSH jail, reads the systemd journal and bans through nftables, so it works without further setup. Put your own changes in /etc/fail2ban/jail.local, never in jail.conf, and list your own address and your Tailscale range in ignoreip so you cannot ban yourself. It is not a substitute for key-only SSH: on a server that already refuses passwords it mostly trims log noise, and on one reached only over Tailscale it has nothing to watch.
What it changes on a key-only server
The bots that hammer every public SSH port, the ones filling your SSH log, are guessing passwords. Once password login is off, no guess can succeed, with or without fail2ban. What fail2ban adds then is smaller: each bot gets a few tries before it is blocked for a while, so the log is shorter and a real anomaly is easier to spot.
Its SSH filter also counts less than many expect. In its default normal mode it counts lines such as Invalid user and Failed password, but a bot that tries root or another real account on a key-only server is turned away before any password is checked, and the Connection closed ... [preauth] line that leaves is counted only in the ddos and aggressive modes. Failed key attempts by real users are not counted by default either. So on a key-only server, set mode = aggressive if you want the bans to match the noise.
Where it does real work is a machine that still accepts passwords, for SSH or for anything else it serves: there it caps how many guesses one address gets. It caps nothing for an attacker with many addresses, which is why turning passwords off comes first. And on a server whose SSH port is closed to the internet and reached over Tailscale, no stranger reaches sshd to fail at all. Install it there only if something else on the machine faces the internet. A look at the open ports tells you which case you are in.
Installing it on Ubuntu or Debian
The SSH jail is live the moment the package is installed: five failures in ten minutes earn a ten-minute ban. Failed key attempts by your own account do not count, but mistyped passwords do, so if you still log in with a password, keep this session open until your address is in ignoreip (the jail.local below).
sudo apt update
sudo apt install fail2ban
sudo systemctl enable --now fail2banCopiedThe package is in Ubuntu’s universe component. Which version you get depends on the release:
| Release | fail2ban | Where the SSH jail reads |
|---|---|---|
Ubuntu 24.04 | 1.0.2 (1.0.2-3ubuntu0.1 in updates) | The journal: the package sets backend = systemd for every jail |
Ubuntu 26.04 | 1.1.0 (1.1.0-9) | The journal: the package sets it for the SSH jail and a few other services’ jails, and leaves the default at auto |
Upstream | 1.1.1, released August 15, 2026 | Not yet packaged for an Ubuntu LTS as of December 2026 |
Both LTS packages ship /etc/fail2ban/jail.d/defaults-debian.conf, which enables the sshd jail, bans through nftables, and points the SSH jail at the systemd journal, so it does not need /var/log/auth.log to exist. The package depends on python3-systemd, which the journal backend needs.
Ubuntu 24.04 had one known breakage at release: fail2ban 1.0.2-3 would not start under Python 3.12, which removed a module it used, and the service log showed:
ERROR No module named 'asynchat'CopiedThe fix is version 1.0.2-3ubuntu0.1 in noble-updates, so a server still showing it needs sudo apt update && sudo apt upgrade.
“Have not found any log file for sshd jail”
On Ubuntu 24.04 this error usually comes from following an older guide that says to copy jail.conf to jail.local. fail2ban reads jail.conf, then jail.d/*.conf, then jail.local, then jail.d/*.local, and a later file wins. A full copy of jail.conf brings back its backend = auto under [DEFAULT], overriding the package’s journal setting, and the SSH jail falls back to reading /var/log/auth.log. On a server where nothing writes that file, the service then stops with:
ERROR Failed during configuration: Have not found any log file for sshd jailCopiedDelete the copied file and write a short one, or add backend = systemd under [sshd], the fix fail2ban’s maintainers give in their issue tracker. fail2ban 1.1.1’s changelog adds an automatic switch from auto to the journal in this situation, which the Ubuntu LTS packages do not have yet. On Ubuntu 26.04 a copied jail.conf should not cause this, since the package sets the SSH jail’s backend to the journal in a file the copy does not override; there the error more likely comes from a backend line set by hand under [sshd].
A jail.local with the settings that matter
jail.conf begins “YOU SHOULD NOT MODIFY THIS FILE.” and a package upgrade may replace it. jail.local holds only what you change. Find the address you connect from first: in your SSH session, echo $SSH_CONNECTION prints it as the first of four values. Then create the file:
sudo nano /etc/fail2ban/jail.localCopied[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 100.64.0.0/10 203.0.113.10
bantime = 1h
findtime = 10m
maxretry = 5
bantime.increment = true
bantime.maxtime = 604800
[sshd]
enabled = true
mode = aggressiveCopiedReplace 203.0.113.10 with your own address, or remove it if your address changes often and you reach the server over Tailscale.
| Setting | Upstream default | What it does |
|---|---|---|
ignoreip | Empty (and ignoreself = true) | Addresses and CIDR ranges never banned. 100.64.0.0/10 is the range Tailscale gives its devices |
bantime | 10m | How long a ban lasts; accepts m, h, d, w. With the increment on, the first ban’s length |
findtime | 10m | The window failures are counted in |
maxretry | 5 | Failures within findtime that trigger a ban |
bantime.increment | Off | Doubles the ban each time the same address is banned again: 1, 2, 4, 8 times bantime |
bantime.maxtime | None | The ceiling for that growth, in seconds; 604800 is a week |
mode (in [sshd]) | normal | Which SSH log lines count as failures; above |
Leave the [sshd] jail’s port as it is unless SSH listens elsewhere, in which case set port = 2222 (your port) there, or the ban blocks a port nothing uses. Check the file and apply it:
sudo fail2ban-client -t
sudo fail2ban-client reloadCopiedHow the ban is made: nftables or ufw
With Ubuntu’s defaults, a ban is an entry in fail2ban’s own nftables table, f2b-table, hooked into incoming traffic at priority -1 and answering banned addresses with a reject. For the SSH jail it covers the SSH port only. It does not show in ufw status; to see it:
sudo nft list table inet f2b-tableCopiedIf you would rather see bans as ufw rules, set banaction = ufw and banaction_allports = ufw under [DEFAULT] in jail.local. The ufw action adds a rule from the address with no port, so it blocks every port, and its file notes that ufw has to be enabled, with sudo ufw enable, before it does anything. Before you enable ufw on a remote server, allow SSH with sudo ufw allow OpenSSH, or the command cuts your own session.
The commands you will use
The overall state, then one jail’s counts and banned addresses:
sudo fail2ban-client status
sudo fail2ban-client status sshdCopiedThe second prints “Currently failed”, “Total failed”, “Currently banned”, “Total banned” and the “Banned IP list”. If Total failed stays at 0 while the log shows attempts, the jail is reading the wrong source or the wrong mode. Every jail’s bans at once, or where one address is banned:
sudo fail2ban-client banned
sudo fail2ban-client banned 198.51.100.7CopiedTo lift a ban from one jail, or from every jail and fail2ban’s database at once:
sudo fail2ban-client set sshd unbanip 198.51.100.7
sudo fail2ban-client unban 198.51.100.7CopiedIf the banned address is yours, SSH from it is refused, so run these from another route in: your provider’s web console, or the server’s Tailscale address, which ignoreip keeps open. Your server’s log of what fail2ban did is /var/log/fail2ban.log.
Repeat offenders: bantime.increment and recidive
Two features deal with the address that comes back after its ban. bantime.increment, in the file above, is the simpler: it looks the address up in fail2ban’s database and doubles its ban each time, up to bantime.maxtime.
The older recidive jail watches fail2ban’s own log for addresses banned again and again, and bans them for a week (bantime = 1w, counted over findtime = 1d) on every port. Add it to jail.local:
[recidive]
enabled = true
backend = autoCopiedThe backend = auto line is there for Ubuntu 24.04, and follows from reading the configuration rather than from a test: the package there makes the journal every jail’s default, so without the line this jail would look for bans in the journal, while fail2ban writes its own log to the file /var/log/fail2ban.log. On 26.04 auto is already the default and the line changes nothing.
Two warnings from jail.conf come with it. Keep fail2ban’s log level above DEBUG, or the jail can feed on its own lines. And fail2ban’s database forgets entries after a day by default, shorter than this jail’s week; jail.conf suggests raising dbpurgeage to 648000 seconds, seven and a half days. On both 24.04 and 26.04 the setting sits under [DEFAULT], so create /etc/fail2ban/fail2ban.local with:
[DEFAULT]
dbpurgeage = 648000CopiedThen restart it with sudo systemctl restart fail2ban. Because it bans every port, a recidive ban on your own address shuts you out of the whole server, not just SSH: one more reason the ignoreip line comes first.