November 26, 2026

Fail2ban Setup for SSH: Jails, Bantime and Whether It Still Matters

A home server rack in a basement with a padlock on the rail and a laptop showing scrolling server logs.

Three wrong passwords from one address, a few seconds apart, and the fourth connection attempt came back as ssh: connect to host 10.99.0.1 port 2222: Connection refused. Nothing on the server had crashed. Fail2ban had read three lines out of a log file, decided the address at the other end was a problem, and written a firewall rule that slammed the door. The whole exchange took about ten seconds, which is roughly how long it takes a botnet to forget the address and move on to the next one anyway.

Video: Akamai Developers on YouTube

Every command and log excerpt below comes from an Ubuntu 24.04 test container running Fail2Ban v1.0.2 and OpenSSH 9.6p1. A second network namespace on a virtual Ethernet pair played the attacker at 10.99.0.2, so the bans landed on a real nftables ruleset rather than a simulation. Timestamps in the output are trimmed. Nothing here was run on a public server.

What fail2ban does, mechanically

A log reader with a hand on the firewall valve

Fail2ban is a log parser that holds a grudge. It tails a log file (or the systemd journal), runs each new line through a set of regular expressions called a filter, counts matches per IP address, and when an address crosses a threshold, runs an action, which is usually a firewall rule. The unit that ties one filter, one log source and one action together is a jail. The sshd jail ships in the package and only needs switching on.

The three numbers that decide everything are defined in the jail.conf(5) man page. findtime is the “time interval… before the current time where failures will count towards a ban.” maxretry is the “number of failures that have to occur in the last findtime seconds to ban the IP.” bantime is the “effective ban duration.” Upstream defaults in the project’s jail.conf are bantime = 10m, findtime = 10m and maxretry = 5.

So the default deal is five strikes in ten minutes for ten minutes in the penalty box, which slows a script down for about as long as a coffee break.

Do not edit jail.conf. The file’s own header says “YOU SHOULD NOT MODIFY THIS FILE” because a package upgrade can overwrite it, and the man page recommends that customizations go in .local files containing “only the settings you would like to change.” On Debian and Ubuntu that means /etc/fail2ban/jail.local or a file in /etc/fail2ban/jail.d/.

A terminal window full of authentication log lines with failed attempts highlighted in red.

A working jail.local for SSH

Six lines of config, one real ban

The test jail used a non-standard port and a short ban so the experiment would finish before dinner:

[DEFAULT]
backend = polling
banaction = nftables
bantime  = 2m
findtime = 10m
maxretry = 3

[sshd]
enabled = true
port    = 2222
logpath = /var/log/auth.log

fail2ban-client -t checks the configuration before anything starts. It printed OK: configuration test is successful. Then the attacker namespace tried three bad passwords for testuser. sshd, logging through rsyslog, wrote this to /var/log/auth.log:

sshd[11057]: Failed password for testuser from 10.99.0.2 port 36694 ssh2
sshd[11057]: Connection closed by authenticating user testuser 10.99.0.2 port 36694 [preauth]
sshd[11140]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=10.99.0.2  user=testuser
sshd[11140]: Failed password for testuser from 10.99.0.2 port 36696 ssh2
sshd[11140]: Connection closed by authenticating user testuser 10.99.0.2 port 36696 [preauth]
sshd[11187]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=10.99.0.2  user=testuser
sshd[11187]: Failed password for testuser from 10.99.0.2 port 36704 ssh2
sshd[11187]: Connection closed by authenticating user testuser 10.99.0.2 port 36704 [preauth]

Eight lines, three failures. The filter counted the Failed password lines once each and did not double-count the PAM line or the disconnect. The jail status afterward:

$ sudo fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed: 3
|  `- File list:    /var/log/auth.log
`- Actions
   |- Currently banned: 1
   |- Total banned: 1
   `- Banned IP list:   10.99.0.2

And the rule it wrote, read straight out of nftables:

$ sudo nft list table inet f2b-table
table inet f2b-table {
    set addr-set-sshd {
        type ipv4_addr
        elements = { 10.99.0.2 }
    }

    chain f2b-chain {
        type filter hook input priority filter - 1; policy accept;
        tcp dport 2222 ip saddr @addr-set-sshd reject with icmp port-unreachable
    }
}

That reject with icmp port-unreachable is why the client saw “Connection refused” instead of a hang. Fail2ban bans one port by default, the one in the jail’s port line, so a banned address can still reach everything else the box serves.

To let someone back in, sudo fail2ban-client set sshd unbanip 10.99.0.2. To keep the owner from getting banned in the first place, set ignoreip to the LAN range. The man page describes ignoreself, which defaults to true, as preventing “the banning of own IP addresses,” but “own” means the server’s addresses, not the laptop on the couch.

Reading fail2ban’s own log

The line that matters says NOTICE

Fail2ban keeps its diary in /var/log/fail2ban.log. For the run above it read like a short police report:

fail2ban.jail      [10838]: INFO    Jail 'sshd' started
fail2ban.filter    [10838]: INFO    [sshd] Found 10.99.0.2 - <time>
fail2ban.filter    [10838]: INFO    [sshd] Found 10.99.0.2 - <time>
fail2ban.filter    [10838]: INFO    [sshd] Found 10.99.0.2 - <time>
fail2ban.actions   [10838]: NOTICE  [sshd] Ban 10.99.0.2
fail2ban.actions   [10838]: NOTICE  [sshd] Unban 10.99.0.2

Found means a line matched the filter and counted as a strike. Ban and Unban are the action firing. If an attacker’s lines are in auth.log but no Found lines appear here, the filter is not matching, and the tool for that is fail2ban-regex, which runs a log file against a filter and reports hits and misses without touching the firewall.

For repeat visitors there is a doubling option. The upstream config describes bantime.increment as using the database of earlier bans to raise the ban time with a formula that defaults to “banTime * 1, 2, 4, 8, 16, 32…” With bantime = 30s and the increment turned on, the test address was banned, allowed to expire, then caught again:

$ sudo fail2ban-client get sshd banip --with-time
10.99.0.2   <time> + 30 = <time>
...
10.99.0.2   <time> + 60 = <time>

fail2ban.observer  [10838]: NOTICE  [sshd] Increase Ban 10.99.0.2 (2 # 1m -> <time>)

Thirty seconds, then sixty. Set bantime.maxtime or the arithmetic will eventually ban a persistent address for longer than the hardware lasts. The same file also ships a [recidive] jail, “for more extended banning of persistent abusers,” which watches fail2ban’s own log and hands out a week-long ban across all ports to anyone banned repeatedly within a day.

A mini PC and network switch with one Ethernet cable connected and another unplugged next to a red lockout tag.

Where people get burned

The jail that never opens because the log isn’t there

The most common failure happens before a single ban. Fresh Debian 12 installs log to the systemd journal and do not install rsyslog, so /var/log/auth.log does not exist. Debian bug #1037437, filed in June 2023, is titled “From fresh bookworm install default sshd jail in fail2ban won’t work without rsyslog installed,” and the report says the service “then fails to start.” Upstream, fail2ban issue #3292 collects the same error: “Have not found any log file for sshd jail.” The fix is backend = systemd, which the man page says “uses systemd python library to access the systemd journal.”

Ubuntu 24.04’s package already carries that fix. On the test container, /etc/fail2ban/jail.d/defaults-debian.conf set backend = systemd and banaction = nftables out of the box. The container had no systemd journal, which is exactly why the test above needed rsyslog and backend = polling. A guide written for one of those setups will break the other one, silently, and the only symptom is a jail that never finds anything.

The Docker side door

Look again at the nftables chain above: hook input. Fail2ban’s rule sits on traffic addressed to the host itself. A service published from a Docker container does not take that road. Docker’s packet filtering documentation explains that ufw gets bypassed because “packets are diverted before it reaches the INPUT and OUTPUT chains that ufw uses,” and the iptables page points anyone filtering container traffic to the DOCKER-USER chain. The same physics applies to a fail2ban ban. A jail protecting a containerized web app can log Ban all day while the banned address keeps walking in through the forwarding path. Self-hosted stacks like the ones in the Docker Compose self-hosting guide need their jail’s action pointed at the right chain, or the ban is a padlock on a door nobody uses.

Locking out the owner

A maxretry of 3 plus a bantime.increment with no ceiling plus no ignoreip means one fumbled password from a phone at a gas station can cost the owner an hour. Set the ignore list before turning anything aggressive on.

Once SSH keys are on, does fail2ban do anything?

Eight hostile logins against a key-only server, and the default jail counted none of them

With the server switched to PasswordAuthentication no and sshd restarted, the attacker tried four password logins, then four logins with an ed25519 key the server had never seen. Every attempt got Permission denied (publickey). The jail’s status before and after:

|  |- Total failed: 3
   |- Currently banned: 0

The total stayed at 3, the number left over from the password test. Eight hostile attempts, zero strikes. sshd’s side of the story explains why:

sshd[11721]: Connection closed by authenticating user testuser 10.99.0.2 port 52806 [preauth]
sshd[11785]: Connection closed by authenticating user root 10.99.0.2 port 48810 [preauth]

With passwords off, there is no Failed password line to match. The client asks, gets refused, and hangs up. Running those nine log lines through fail2ban-regex in each of the filter’s modes:

== mode=normal
Lines: 9 lines, 8 ignored, 0 matched, 1 missed
== mode=ddos
Lines: 9 lines, 0 ignored, 8 matched, 1 missed
== mode=extra
Lines: 9 lines, 0 ignored, 0 matched, 9 missed
== mode=aggressive
Lines: 9 lines, 0 ignored, 8 matched, 1 missed

In normal mode, the filter does see those disconnect lines, but the upstream sshd filter wraps that pattern in a <F-NOFAIL> tag, which uses the line to identify an address without counting it as a failure. The ddos and aggressive modes use a variant that the filter’s own comment describes as the same pattern “but as failure.” Switch the jail to mode = aggressive and key-only probing starts earning bans. So does a legitimate client whose agent offers the wrong key first, which is the price.

With keys on and the default jail, fail2ban sits on a server and catches nobody. The password it was built to defend no longer exists.

An old brass key and a modern security key on a workbench, representing password versus key-based SSH login.

The case for keeping it anyway

sshd grew its own fail2ban, and that changes the math

The strongest argument for fail2ban on a key-only box came in July 2024, when Qualys published CVE-2024-6387, “regreSSHion”, a pre-authentication race condition in OpenSSH’s sshd on glibc Linux. Keys did not help. The bug fires before authentication. What the attack needed was volume: Qualys reported that against Debian 12’s OpenSSH 9.2p1 on i386, winning the race took about 10,000 tries, roughly 6 to 8 hours to a root shell once address randomization was included. Anything that throttles one address from hammering connection after connection stretches that clock, and only an aggressive-mode jail, not the default, counts those preauth disconnects at all.

Then OpenSSH answered the question itself. The OpenSSH 9.8 release notes, dated 2024-07-01, say “sshd(8) will now penalise client addresses that, for various reasons, do not successfully complete authentication,” controlled by a PerSourcePenalties option that “is on by default.” The current sshd_config man page lists penalties for crashes, failed authentication, invalid users and clients that never authenticate, accumulating to a 10-minute cap by default, with PerSourcePenaltyExemptList as the built-in version of ignoreip.

The catch is the version number. Ubuntu 24.04 ships 9.6p1, and on the test container sshd -T returned no penalty settings at all. On a box with 9.8 or newer, the daemon already does a lighter version of what the jail does, without parsing a single log line. On an LTS box sitting on 9.6, it does not, and an aggressive jail is the closest substitute.

Where fail2ban earns its keep without argument is everything that isn’t SSH: a web login form, a mail server, a self-hosted app with its own password page. Those services still take passwords, still log failures, and still have no penalty system built into the daemon. Pair it with patched packages, which the unattended-upgrades guide covers, and keep an eye on log growth with logrotate, since a [recidive] jail reads fail2ban’s own log and the upstream config warns that DEBUG-level logging can send it into “an infinite loop.” Better still, take SSH off the internet entirely and reach it over a VPN, as in the WireGuard home VPN walkthrough or the Tailscale comparison. A port nobody can reach writes no log lines to parse.

Quick reference

Commands that came up, all tested on the container

Task Command
Check config before starting sudo fail2ban-client -t
List jails sudo fail2ban-client status
One jail’s counts and bans sudo fail2ban-client status sshd
Bans with expiry times sudo fail2ban-client get sshd banip --with-time
Unban an address sudo fail2ban-client set sshd unbanip <IP>
Test a filter against a log sudo fail2ban-regex /var/log/auth.log "sshd[mode=aggressive]"
See the firewall rule sudo nft list table inet f2b-table

The log on a key-only server keeps filling with Connection closed by authenticating user root from addresses nobody invited, and the default jail lets every one of them walk past without a strike. Which leaves a question for anyone running a home server with port 22 open: has the jail on that box ever logged a single Ban since the passwords were turned off?

Sources

  • jail.conf(5) man page, Debian: https://manpages.debian.org/bookworm/fail2ban/jail.conf.5.en.html
  • Fail2ban upstream jail.conf: https://raw.githubusercontent.com/fail2ban/fail2ban/master/config/jail.conf
  • Fail2ban upstream sshd filter: https://github.com/fail2ban/fail2ban/blob/master/config/filter.d/sshd.conf
  • Debian bug #1037437 (via linux.debian.bugs.dist): https://groups.google.com/g/linux.debian.bugs.dist/c/nOsQs2RW1bA
  • Fail2ban issue #3292: https://github.com/fail2ban/fail2ban/issues/3292
  • Docker, Packet filtering and firewalls: https://docs.docker.com/engine/network/packet-filtering-firewalls/
  • Docker, Docker with iptables: https://docs.docker.com/engine/network/firewall-iptables/
  • Qualys, CVE-2024-6387 regreSSHion advisory: https://www.qualys.com/2024/07/01/cve-2024-6387/regresshion.txt
  • OpenSSH 9.8 release notes: https://www.openssh.org/txt/release-9.8
  • sshd_config(5), OpenBSD manual: https://man.openbsd.org/sshd_config

487 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *