Securus Journal 43.65°N, 79.38°W

fail2ban: the lockouts iptables doesn't do

fail2ban's model of filters, jails and actions, the minimum useful config, how to confirm it is watching the right log, and where it sits beside iptables and a hardened SSH setup.

Contents

Why fail2ban exists

Every SSH server on the internet keeps a written record of the people trying to get into it. The record is the authentication log, and on a server with port 22 open it fills with failed logins from addresses nobody has heard of, at every hour, for as long as the machine is up. On most machines, for a long time, the log was the end of it. An administrator could read it, and usually did not.

Cyril Jaquier started fail2ban in 2004 to close that loop. The program reads the log, recognizes a failed login, counts, and when one address has failed too often, tells the firewall to stop listening to it for a while.

iptables decides who is allowed to connect. SSH hardening decides what they can do if they reach you. Neither addresses the problem in between: an attacker who is technically allowed to reach port 22 and is spending all day guessing passwords.

The manual version is to tail journalctl, watch for failed logins and write a rule. Nobody does that for long. fail2ban automates exactly that loop: read a log, match a pattern, push a temporary firewall rule against the offending IP, and expire the rule later.

That is the whole mental model. Everything else is configuration.


How it actually works

It is built from three pieces.

A filter is a regex that matches “this log line means a failed authentication.” Filters come pre-shipped for SSH, nginx, Postfix and dozens of other services.

A jail binds one filter to a backend (which log to watch), a threshold (how many matches before acting) and an action (what to do).

An action is usually an iptables or nftables rule that blocks the source IP, plus a timer that reverses it.

fail2ban-server runs as a daemon, tails the relevant logs and applies actions when thresholds are hit. fail2ban-client is how you talk to it.


Minimum useful config

The default install does almost nothing until a jail is enabled. Override the defaults in /etc/fail2ban/jail.local. Never edit jail.conf directly; its own header warns that it will be overwritten by package updates.

[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5
backend  = systemd
ignoreip = 127.0.0.1/8 ::1 100.64.0.0/10

[sshd]
enabled = true

What this says:

  • Five failed attempts within ten minutes earn a one-hour ban.
  • Logs come from the systemd journal, not a flat file, which suits any modern Fedora, Debian or Ubuntu.
  • ignoreip exempts loopback and the whole of 100.64.0.0/10. That range is the shared address space RFC 6598 set aside for carrier-grade NAT, and Tailscale assigns its devices addresses from it, so a typo on a machine reached over the tailnet cannot lock its administrator out.
  • The [sshd] jail uses the built-in filter at /etc/fail2ban/filter.d/sshd.conf.

An exemption is trust, and it is exactly as wide as the range written down. Exempting a whole private range means any device in it, including one that has been compromised, can guess passwords without limit. Keep the list as narrow as the access paths you actually use.

Enable and start:

sudo systemctl enable --now fail2ban

Verify it’s watching the right thing:

sudo fail2ban-client status sshd

The output looks something like this:

Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     0
|  `- Journal matches:  _SYSTEMD_UNIT=sshd.service + _COMM=sshd + _COMM=sshd-session
`- Actions
   |- Currently banned: 0
   |- Total banned:     0
   `- Banned IP list:

With the systemd backend, the third line of the filter section lists the journal matches the jail selects on; a file-based backend prints File list: and the paths instead. The exact matches depend on the fail2ban version. That is a working jail with no current offenders. On a server with port 22 exposed to the internet, the “Total banned” counter starts climbing within hours.


Watching it work

The most informative single command is the same one:

sudo fail2ban-client status sshd

It lists the currently banned IPs and the lifetime totals for that jail.

To unban an IP by hand (your own, after a self-inflicted lockout):

sudo fail2ban-client set sshd unbanip 203.0.113.42

To see the rules fail2ban’s actions have put in the firewall:

sudo iptables -L f2b-sshd -n --line-numbers

The f2b-sshd chain is created by the iptables actions on the first ban and holds the active blocks. When a ban expires, its line is removed automatically. With an nftables action the bans live in nftables instead, and sudo nft list ruleset shows them.


Gotchas worth knowing

Bans survive a restart. Since version 0.9, fail2ban keeps them in an SQLite database, /var/lib/fail2ban/fail2ban.sqlite3, and puts back the ones that have not expired when it starts. What it does not do by default is remember repeat offenders. Adding bantime.increment = true, and tuning bantime.maxtime, makes each new ban for the same address longer than the last, growing exponentially, which is closer to what you want. The database forgets bans after a day by default, so for that memory to last longer, raise dbpurgeage in fail2ban.local as well.

The backend decides what is read. With backend = systemd, fail2ban reads the journal and ignores any logpath. That is the right choice where no syslog daemon runs: Debian 12 no longer installs rsyslog by default, so on a fresh install there is no /var/log/auth.log, and a jail pointed at it has nothing to read. The silent failure is a journal filter that matches nothing. The sshd filter picks journal entries by unit and process name, and when OpenSSH 9.8 moved authentication into a separate sshd-session process, filters had to learn the new name. Current fail2ban matches it; older releases may not. The test is simple: fail one login on purpose, then check that “Total failed” went up.

ignoreip is the safety net. If you are remote and rely on a VPN or jump host to reach the machine, exempt that range explicitly. Locking yourself out of a remote host because of a sticky key is a fixable mistake, but an annoying one.

fail2ban needs something to install rules into. Without iptables or nftables available, the action layer has nowhere to put a ban. On a minimal container or a stripped-down system, firewalld or plain iptables may need installing first.


Where it fits

In layers, from the outside in. The firewall denies inbound traffic by default and opens only the ports actually served. SSH hardening takes away passwords and root logins, and closes port 22 where that can be managed. fail2ban catches what gets through anyway.

Each layer assumes the one before it is in place. fail2ban substitutes for neither of the others; it is the alarm installed after the door is already locked. It is cheap to set up and costs nothing to run, and it turns a constant stream of probes into a problem that mostly solves itself. With passwords switched off, the guesses it bans could never have succeeded. What it saves is the noise, and the log that an administrator might, one day, actually read.

Sources

  • fail2ban project. jail.conf, fail2ban.conf and filter.d/sshd.conf, and the fail2ban(1) manual pages.
  • Weil, J., V. Kuarsingh, C. Donley, C. Liljenstolpe and M. Azinger. RFC 6598, “IANA-Reserved IPv4 Prefix for Shared Address Space.” April 2012.
  • OpenSSH 9.8 release notes, July 2024.
  • Debian. Release notes for Debian 12 (bookworm).