Skip to content

Checking fail2ban: does the protection really work?

· security, network, technology, server

How fail2ban turns logs into bans, why a jail can stay silent — and how one command proves that your own rules actually catch something.

apt install fail2ban installs the service, and on Ubuntu 26.04 the shipped SSH jail is already enabled. That does not mean any of the jails work the way you think.

What goes wrong is quiet: a jail reads a journal with nothing in it, a filter expects a different log format, or nginx does not write where fail2ban looks. In all three cases nothing reports anything — it just never bans. A filter that never finds anything looks exactly like one that works.

This article explains how fail2ban works, sets it up for SSH and nginx, and checks every step with a command that prints something. The values come from the server this blog runs on; measured on 28 November 2026.

The idea in one sentence

fail2ban reads along what is written into a log, counts how often the same address tries something suspicious, and after too many attempts closes the door for a while.

It is therefore not a protection that prevents something, but one that ends something. The first attempt gets through, the fifth does too. What fail2ban changes is the number of attempts an attacker manages in one night: thousands become five.

What a jail is made of

Anyone reading the configuration runs into three words that keep coming back.

The filter is a set of patterns that decides which log line is a failed attempt. A filter is a text file with regular expressions.

The action is what happens on a ban — on Ubuntu that is an entry in nftables by default, which drops the address.

The jail connects the two and fixes the numbers: which log file is read, how many hits within what period lead to a ban, and how long the ban lasts. A jail is a filter plus an action plus a time window.

Three numbers control it:

Setting Meaning
maxretry that many failed attempts lead to a ban
findtime the attempts are counted within this period
bantime the address stays blocked for this long

With the values in this article: five attempts within ten minutes result in an hour’s ban. Whoever mistypes a password once notices nothing; whoever runs through a dictionary gets no further after five attempts.

What the package brings

After installation, /etc/fail2ban/jail.d/defaults-debian.conf already contains the frame:

[DEFAULT]
banaction = nftables
banaction_allports = nftables[type=allports]
backend = systemd

[sshd]
enabled = true

Three things are set by that: bans are created as nftables rules, fail2ban reads the systemd journal by default, and the SSH jail is enabled. On the blog server the result sits in a table of its own:

sudo nft list tables | grep f2b
# table inet f2b-table

ufw status does not show that table — ufw lists only its own rules. Both can run side by side; on the blog server they do. Whoever wants to see what fail2ban blocks right now uses nft or the command in the section “Looking at whether it runs”.

The SSH jail

Your own values go into jail.local. That file wins over jail.conf and everything in jail.d/, and it survives a package update:

sudo tee /etc/fail2ban/jail.local > /dev/null <<'EOF'
[DEFAULT]
allowipv6 = auto
ignoreip  = 127.0.0.1/8 ::1 203.0.113.10
bantime   = 1h
findtime  = 10m
maxretry  = 5

[sshd]
enabled = true
port    = 22
journalmatch = _SYSTEMD_UNIT=ssh.service + _COMM=sshd
mode    = ddos
bantime = 24h
bantime.increment = true
bantime.factor    = 2
bantime.maxtime   = 168h

[recidive]
enabled  = true
backend  = auto
logpath  = /var/log/fail2ban.log
bantime  = 168h
findtime = 24h
maxretry = 5
EOF

Four lines in there deserve an explanation.

ignoreip takes your own addresses out. Whoever mistypes once and enters a wrong password three times is not locked out for an hour because of it. The list only works for addresses fail2ban recognises from the log. A manual ban bypasses it completely — see below.

journalmatch is the line without which the jail stays silent. On Ubuntu the unit is called ssh.service; a jail looking for sshd.service finds nothing. Measured on the blog server: without journalmatch, zero hits and zero bans; with the line, the jail works. That is the most unpleasant mistake in this article, because it reports nothing.

mode = ddos also counts aborted connections as a failed attempt. The attacker sends the banner abort before the actual attempt; in normal mode it falls through. Instead of a ban after five password attempts you get one after five approaches.

The escalation bantime.increment doubles the ban on every further ban of the same address: 24 hours, 48, 96 up to the ceiling of 168 hours. The [recidive] jail reads fail2ban.log and bans repeat offenders for a week, even when they show up in different jails.

nginx: files instead of the journal

nginx writes its logs into files under /var/log/nginx/, not into the journal. The nginx jails therefore use backend = auto, and the path goes into the jail:

sudo tee /etc/fail2ban/jail.d/nginx.conf > /dev/null <<'EOF'
[nginx-http-auth]
enabled  = true
backend  = auto
port     = http,https
logpath  = /var/log/nginx/error.log

[nginx-botsearch]
enabled  = true
backend  = auto
port     = http,https
logpath  = /var/log/nginx/example.com.access.log

[nginx-suchlauf]
enabled  = true
backend  = auto
port     = http,https
filter   = nginx-suchlauf
logpath  = /var/log/nginx/example.com.access.log
maxretry = 5
findtime = 10m
bantime  = 1h
EOF

nginx-http-auth acts on failed logins to protected directories, nginx-botsearch on the usual search paths. Both work with the rules the package ships. The third jail is custom-built — the next section shows it.

Your own filter for scans

Anyone running a web server sees requests for files that never existed there. This is what such a line looks like in an access log:

203.0.113.55 - - [28/Nov/2026:07:12:44 +0100] "GET /.env HTTP/1.1" 404 153 "-" "Mozilla/5.0"

From left to right: the address of the requester, a placeholder for the user name, date and time, the request itself, status code 404, the size of the response, and two fields for referrer and browser identification. A filter needs only two things from that: the address, and the information that the request went to /.env and was answered with 404.

On the blog, that was 5,947 requests answered with 404 on two days in November 2026; the most frequent targets were /.env, /.git/config and wp-login.php. The shipped nginx-botsearch filter only recognises what is in its list; whoever wants the list derived from their own log writes it themselves:

sudo tee /etc/fail2ban/filter.d/nginx-suchlauf.conf > /dev/null <<'EOF'
[Definition]

failregex = ^<HOST> - \S+ \[[^\]]*\] "(?:GET|POST|HEAD) /[^"]*?(?:\.env|\.git/|\.aws|\.ssh|\.sql|\.bak|\.zip|wp-login\.php|wp-admin|xmlrpc\.php|phpmyadmin|adminer|config\.(?:json|php|yml))[^"]*" 404
            ^<HOST> - \S+ \[[^\]]*\] "(?:GET|POST|HEAD) /[^"]*\.ht[^"]*" 403

ignoreregex = ^<HOST> - \S+ \[[^\]]*\] "(?:GET|POST|HEAD) /(?:robots\.txt|favicon\.ico|sitemap\.xml|index\.xml)[^"]*" 404
EOF

<HOST> is fail2ban’s placeholder for the address; fail2ban inserts a pattern of its own there that recognises IPv4 and IPv6. Everything else is ordinary regular expression syntax: \S+ stands for a word without spaces, \[[^\]]*\] for the timestamp in square brackets, (?:GET|POST|HEAD) for one of the three methods, [^"]*? for arbitrary text up to the next quotation mark.

The rest of the first rule is a list of paths nobody calls up by accident: .env files, .git, .aws, .ssh, database backups, WordPress login, phpMyAdmin. The second rule catches the attempt to read .htaccess — answered with 403, not with 404.

ignoreregex takes out lines that match the pattern but are harmless: robots.txt, favicon and sitemap are asked for by an honest crawler too.

An exception for “Googlebot” in the user agent is deliberately absent. Of the measured requests, most came with exactly that label, meaning from the attacker itself. Whoever exempts that does not block the scanner.

Two properties of the configuration surprise people: a failregex of your own in the jail replaces the filter’s, it does not add to it. And the list of paths is never finished — it grows with what appears in the log. Both are reasons to write the rules in the filter and not in the jail.

Checking instead of hoping

The difference can be measured before anyone is banned:

sudo fail2ban-regex /var/log/nginx/example.com.access.log \
     /etc/fail2ban/filter.d/nginx-suchlauf.conf

The output names hits, misses and the number of lines read. With --print-all-matched it also shows which lines matched:

sudo fail2ban-regex --print-all-matched \
     /var/log/nginx/example.com.access.log \
     /etc/fail2ban/filter.d/nginx-suchlauf.conf

Tested against this blog’s log: 13 of 328 lines match. The hits are exclusively GET /wp-admin/install.php, /xmlrpc.php, /wp-login.php, /wp-admin/user/index.php and /.git/HEAD — not a single request a reader would have made. A zero here does not mean the filter is bad, but that the line format does not match: nginx writes differently than the rule assumes.

Looking at whether it runs

sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo fail2ban-client get sshd journalmatch

The first output lists the jails, the second the blocked addresses and the number of failures, the third the journal filter the jail actually uses. The third is the important one: if it does not show the expected line, the jail is reading something other than you think.

Manual bans work like this, and they bypass ignoreip:

sudo fail2ban-client set sshd banip 198.51.100.7
sudo fail2ban-client set sshd unbanip 198.51.100.7

Whoever wants to get rid of an address permanently does not do it in fail2ban but in the firewall.

What commonly goes wrong

Five mistakes cover almost everything that does not work in practice:

  • Wrong backend. The journal is read, but the program writes to a file (or the other way round). Symptom: zero hits with a running jail.
  • Wrong unit name. ssh.service instead of sshd.service, see above. Same symptom.
  • Wrong log path. The jail reads a file that exists but is the wrong one — for instance the access log of a site that has long been renamed.
  • Filter does not match the format. Usually after switching the web server or after changing the logging. Symptom: fail2ban-regex finds nothing.
  • Time window too generous. With maxretry = 20 and findtime = 1h, a slow attacker never triggers anything.

What they all share is that fail2ban reports nothing. There is no warning “this jail has found nothing for three weeks”. That is why a look at fail2ban-client status belongs to the routine, just like a look at the logs.

What fail2ban does not do

It reacts after the event, and it reads logs. An attacker who tries slowly enough stays below any threshold. A distributed attempt from a hundred addresses with one try each is invisible to any jail. And against a hole in a web application it does nothing at all — it only sees that someone keeps running into it.

fail2ban is therefore no replacement for the firewall and none for updates. It is the addition that turns a log into a decision.

Sources

All addresses retrieved on 28 November 2026, each answered HTTP 200.

  1. fail2ban, manual page jail.conf (structure of jail.local, load order, failregex replacing the filter’s default): https://manpages.ubuntu.com/manpages/resolute/man5/jail.conf.5.html
  2. fail2ban-regex, manual page (hits, --print-all-matched, --print-all-missed): https://manpages.ubuntu.com/manpages/resolute/man1/fail2ban-regex.1.html
  3. fail2ban-client, manual page (status, get, set … banip): https://manpages.ubuntu.com/manpages/resolute/man1/fail2ban-client.1.html
  4. Ubuntu Server, firewall with ufw: https://documentation.ubuntu.com/server/how-to/security/firewalls/
  5. Ubuntu package fail2ban in 26.04 (1.1.0-9): https://packages.ubuntu.com/resolute/fail2ban
  6. RFC 5737, address range for examples (203.0.113.0/24, TEST-NET-1): https://www.rfc-editor.org/rfc/rfc5737
  7. IANA, example domains (example.com for documentation): https://www.iana.org/help/example-domains

← all posts