Skip to content

Setting up fail2ban without locking yourself out

A senior-engineer setup for fail2ban on a production Linux server — strict enough to be useful, careful enough not to ban the operator at 2am.

Fail2ban is one of those tools that’s easy to install and easy to misconfigure into uselessness — or, worse, into a lockout. The common failure modes are predictable: defaults that are too lenient to stop anything, defaults that are too aggressive and ban the on-call engineer, or a regex that silently stops matching after a log format change.

What’s happening

A default fail2ban install enables the sshd jail and stops there. Meanwhile, wp-login.php brute force, mail auth abuse, and HTTP scanner traffic happily continue. Worse, the default bantime is short enough that a determined attacker just paces themselves under the threshold. The instinct to “make it stricter” then turns into a 24-hour ban on the office IP after one fat-fingered password.

How to verify

Check what’s actually enabled and what’s currently banned:

sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo journalctl -u fail2ban --since "1 hour ago" | grep -E 'Ban|Unban|Found'

Then confirm the jail is actually reading the right log file and matching real entries:

sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf

If the regex matches zero lines on a server you know has had failed logins, the filter is wrong — usually a log format change after a distro upgrade.

The fix

Always set an allowlist before tightening anything. In /etc/fail2ban/jail.local:

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

[sshd]
enabled  = true
mode     = aggressive

[wordpress-auth]
enabled  = true
filter   = wordpress-auth
logpath  = /var/log/nginx/access.log
port     = http,https
maxretry = 8
findtime = 5m
bantime  = 6h

ignoreip covers your office egress, your VPN, and any monitoring vendor. bantime = 1h for SSH is a sweet spot: long enough to deter the noisy bots, short enough that a real engineer typo doesn’t lock them out for the night. A persistent attacker should be caught by repeat offenses; configure bantime.increment = true to escalate cleanly.

Test before reloading: fail2ban-client -d. Reload with fail2ban-client reload. Then deliberately fail an SSH login from a non-allowlisted IP and confirm the ban appears in fail2ban-client status sshd.

The other thing nobody mentions: keep an out-of-band path to the server — console access via your cloud provider, or a second IP not subject to the same rules. Fail2ban will eventually ban you, and the question is only what you do when it does.

For environments where login surface is wide — WordPress, mail, VPN — we maintain the jail definitions and the ignoreip list as part of Deployment & Management.