Skip to content
subuilds.dev

← logbuild #16 passedb1fbdb7

Fail2ban on Debian 13: The Right Config for OpenSSH 9.x

The basic Fail2ban configuration that protects a Linux server, tested with real wrong-key attempts.

· 9 min read

This is a basic Fail2ban setup on AWS EC2 with Debian 13. Fail2ban is a tool that bans IPs after too many failed logins. We install it and configure it. Then we run a live test: we use a wrong key to trigger a ban on a real EC2 instance.


In Simple Words#

The problem
Fail2ban runs and the jail is active, but the failure count stays at zero. Wrong-key attempts fill the journal and nobody gets banned.
The root cause
On Debian 13, the default config does not match what sshd logs. At the default log level, a wrong key only logs Connection closed. The Fail2ban filter does not match that line.
The solution
Write a small jail.local that reads the systemd journal and uses the aggressive filter mode. Set LogLevel VERBOSE in sshd_config, so each wrong key logs a Failed publickey line. Then test with a wrong key.
Think of it like this
A guard must lock out anyone who tries a wrong key five times. The guard counts from the visitor book, but the book only says “a visitor left”, so the count stays at zero. Make the book say “wrong key”, and the guard can count and lock people out.

How Fail2ban Works#

Fail2ban watches a log source: a file or the systemd journal (the system log on Debian). It looks for lines that match a filter. A filter is a regex, which is a text pattern. When the same IP matches the filter maxretry times within findtime, Fail2ban runs a ban action. The action is usually a UFW or nftables rule that drops traffic from that IP for bantime.

  1. An attacker tries a wrong key. sshd writes a line about it to the systemd journal.
  2. At the default log level, the line only says Connection closed. The Fail2ban filter does not match it, so the count stays at zero.
  3. With LogLevel VERBOSE, the journal also gets a Failed publickey line. The filter matches it and counts one failure for this IP.
  4. The attacker keeps trying. The count reaches maxretry, here 5, within findtime.
  5. Fail2ban bans the IP with a UFW DENY rule. The next attempt does not reach sshd any more.
5 / 5

On Debian 13, the default config ends at “no match” for three different reasons. Each one drops the event without any message. Fail2ban runs, the jail is active, and the count stays at zero.

Prerequisites#

  • A Debian 13 server, hardened as in the Linux Server Security Baseline
  • Custom SSH port configured (this guide uses 2233)
  • systemd is running (the default on Debian 13)

Step 1: Install Fail2ban#

bash
sudo apt install -y fail2ban

Do not start it yet. Configure it first.

Step 2: Create a Minimal jail.local#

A jail is the Fail2ban name for one set of rules that protects one service. The common advice is to copy jail.conf to jail.local and edit it. Here we write a small new file.

bash
sudo nano /etc/fail2ban/jail.local
ini
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 <your-ip>

[sshd]
enabled      = true
port         = 2233
backend      = systemd
journalmatch = _SYSTEMD_UNIT=ssh.service
filter       = sshd[mode=aggressive]
bantime      = 1h
findtime     = 10m
maxretry     = 5
Setting What It Does
ignoreip IPs that will never be banned — add your own IP or CIDR
port Must match your SSH port
backend systemd — reads from the journal, not a file
journalmatch Which journal entries to watch — see Step 3
filter sshd[mode=aggressive] — counts publickey failures, not just passwords
bantime How long a banned IP stays blocked
findtime The time window for counting failures
maxretry Failed attempts within findtime that trigger a ban

Step 3: Enable LogLevel VERBOSE in sshd#

This is the second problem that is hard to see.

At the default INFO log level, a wrong-key attempt is logged as:

text
Connection closed by authenticating user admin x.x.x.32 port 60869 [preauth]

Fail2ban’s sshd filter does not match Connection closed. Fail2ban never sees the failure.

With LogLevel VERBOSE, the same event logs:

text
Failed publickey for admin from x.x.x.32 port 27370 ssh2: ED25519 SHA256:pjC7...
Connection closed by authenticating user admin x.x.x.32 port 27370 [preauth]

The filter does match the Failed publickey line.

Add this to /etc/ssh/sshd_config:

text
LogLevel VERBOSE

Restart sshd:

bash
sudo systemctl restart sshd

Step 6: Start and Enable Fail2ban#

bash
sudo systemctl enable --now fail2ban

Verify that the SSH jail is active:

bash
sudo fail2ban-client status sshd
text
Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     0
|  `- Journal matches:  _SYSTEMD_UNIT=ssh.service
`- Actions
   |- Currently banned: 0
   |- Total banned:     0
   `- Banned IP list:

Two things to check:

  • Journal matches shows _SYSTEMD_UNIT=ssh.service
  • The jail is active with 0 errors

Step 7: Test — Wrong Key Attempt#

Use a different key pair (or generate a temporary one) and try to connect again and again:

bash
ssh -i wrong-key.pem admin@<ec2-public-ip> -p 2233

The first attempts fail with the expected login error:

text
admin@<ec2-public-ip>: Permission denied (publickey).

Check the journal. Each attempt now writes a Failed publickey line from sshd-session:

bash
sudo journalctl -u ssh -n 10
text
Mar 16 11:07:43 hostname sshd-session[10804]: Connection from 203.0.113.50 port 59262 on 10.0.1.100 port 2233 rdomain ""
Mar 16 11:07:44 hostname sshd-session[10804]: Failed publickey for admin from 203.0.113.50 port 59262 ssh2: ED25519 SHA256:pjC7...
Mar 16 11:07:44 hostname sshd-session[10804]: Connection closed by authenticating user admin 203.0.113.50 port 59262 [preauth]
Mar 16 11:07:44 hostname sshd[5634]: srclimit_penalise: ipv4: new 203.0.113.50/32 deferred penalty of 5 seconds
Mar 16 11:07:46 hostname sshd-session[10806]: Failed publickey for admin from 203.0.113.50 port 7397 ssh2: ED25519 SHA256:pjC7...
Mar 16 11:07:48 hostname sshd-session[10808]: Failed publickey for admin from 203.0.113.50 port 26010 ssh2: ED25519 SHA256:pjC7...
Mar 16 11:07:51 hostname sshd-session[10810]: Failed publickey for admin from 203.0.113.50 port 16599 ssh2: ED25519 SHA256:pjC7...

Keep trying. On the 5th attempt, the ban starts. The 6th attempt looks different. SSH does not even answer with Permission denied:

text
Connection closed by 198.51.100.10 port 2233

That is the ban at work. Fail2ban dropped the connection before SSH could answer.

Step 8: Verify the Ban#

bash
sudo fail2ban-client status sshd
text
Status for the jail: sshd
|- Filter
|  |- Currently failed:	0
|  |- Total failed:	5
|  `- Journal matches:	_SYSTEMD_UNIT=ssh.service
`- Actions
   |- Currently banned:	1
   |- Total banned:	1
   `- Banned IP list:	203.0.113.50

Total failed: 5 means all five wrong-key attempts were counted. Currently banned: 1 means the ban started after the 5th failure. Currently failed: 0 means the counter resets after a ban.

Verify that the UFW rule was added:

bash
sudo ufw status
text
Anywhere                   DENY IN     203.0.113.50

To unban an IP by hand (your own, if you locked yourself out):

bash
sudo fail2ban-client set sshd unbanip <ip>

Tuning for Production#

Setting Development Production
bantime 1h 24h or 1w
findtime 10m 10m
maxretry 5 3
ignoreip Your home IP Your team’s IPs or VPN CIDR

For production servers that are really open to the internet, a 24-hour ban and 3 retries fit better. Bots retry thousands of times. With a short ban, they only come back in an hour.

Useful Commands#

bash
# Check which jails are active
sudo fail2ban-client status

# Status of the SSH jail
sudo fail2ban-client status sshd

# Unban an IP
sudo fail2ban-client set sshd unbanip <ip>

# Reload config after editing jail.local
sudo fail2ban-client reload

# View Fail2ban logs
sudo journalctl -u fail2ban -n 50

# Test the sshd filter against the journal manually
sudo fail2ban-regex systemd-journal "/etc/fail2ban/filter.d/sshd.conf[mode=aggressive]" --journalmatch="_SYSTEMD_UNIT=ssh.service"

Wrapping Up#

I spent a long time looking at a Fail2ban jail that seemed healthy: running, no errors, zero failures. At the same time, wrong-key attempts filled the journal.

If you followed the steps, your server now has a working Fail2ban setup that really bans wrong-key attempts. Test it yourself: use a wrong key, watch the counter go up, and see the ban drop the connection.

The full config is eight lines in jail.local and one line in sshd_config. Copy it, verify it, and move on.