What does fail2ban do and what does it not do?
fail2ban reads log files or the systemd journal, matches lines with regular expressions called filters, and counts failures per IP address. When an address passes the limit inside a time window, an action runs, normally a firewall rule that blocks it for a while. A filter plus its settings is a jail. It is an excellent answer to password guessing against SSH, mail logins and web login forms. It does not stop floods, which is what our edge DDoS filtering is for, and it is no substitute for key-only SSH: with keys enforced, fail2ban mostly keeps logs clean and saves a little CPU.
How do I install it?
# Ubuntu 24.04 / 22.04, Debian 13 / 12 apt update && apt install -y fail2ban # Rocky Linux 9, AlmaLinux 9, CentOS Stream 9 dnf install -y epel-release && dnf install -y fail2ban systemctl enable fail2ban --now
On Debian 12 and newer there is no /var/log/auth.log unless rsyslog is installed, so jails must read the systemd journal. The setting below handles that.
How do I configure the SSH jail?
Never edit jail.conf; package upgrades replace it. Put your settings in /etc/fail2ban/jail.local or a file under /etc/fail2ban/jail.d/:
# /etc/fail2ban/jail.d/10-local.conf [DEFAULT] backend = systemd bantime = 2h findtime = 15m maxretry = 4 ignoreip = 127.0.0.1/8 198.51.100.7 [sshd] enabled = true port = 2222 mode = aggressive
Four failures in fifteen minutes earns a two hour ban. ignoreip lists addresses that are never banned; put your office or home address there so you cannot lock yourself out. Set port to your real SSH port if you changed it as in Change the SSH port. The aggressive mode also catches clients that fail key exchange or probe with invalid user names. Apply with systemctl reload fail2ban.
Which firewall does fail2ban use?
The banaction setting decides. Debian and Ubuntu packages default to an nftables based action on current releases, and the Enterprise Linux package ships 00-firewalld.conf so bans go through firewalld. If you replaced firewalld with plain nftables, set it explicitly:
[DEFAULT] banaction = nftables-multiport banaction_allports = nftables-allports
Check that bans really land in the firewall with nft list ruleset | grep -A3 f2b.
How do I protect nginx logins and probes?
fail2ban ships filters for common nginx problems. Two useful jails for a public web server:
[nginx-http-auth] enabled = true backend = auto logpath = /var/log/nginx/error.log [nginx-botsearch] enabled = true backend = auto logpath = /var/log/nginx/access.log maxretry = 6
The first bans repeated failed basic-auth logins; the second bans scanners that hammer paths like /wp-login.php or /.env on a site that has neither. These jails read files, so set backend = auto on them even when the default is systemd.
How do I see and remove bans?
| Task | Command |
|---|---|
| List active jails | fail2ban-client status |
| Banned addresses in one jail | fail2ban-client status sshd |
| Unban one address everywhere | fail2ban-client unban 203.0.113.44 |
| Ban an address by hand | fail2ban-client set sshd banip 203.0.113.99 |
| Show effective settings | fail2ban-client -d | grep sshd |
If you lock yourself out, connect from any other address (a phone hotspot works) and unban yourself, or wait for the ban to expire. Bans are dropped on a fail2ban restart unless the database keeps them, which it does by default for the remaining ban time.
How do I test a filter before trusting it?
fail2ban-regex runs a filter against a log and reports matches without banning anyone:
fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-botsearch.conf fail2ban-regex systemd-journal sshd
Zero matches on a log that clearly contains failures usually means a wrong log path or date format. Fix that before relying on the jail.
How do I deal with repeat offenders?
Enable the recidive jail. It reads fail2ban's own log and bans addresses that keep getting banned, for much longer:
[recidive] enabled = true bantime = 1w findtime = 1d maxretry = 3
Increasing ban times are also built in: bantime.increment = true doubles the ban each time the same address returns.
fail2ban is light: a few tens of megabytes of memory on even the smallest box, such as E1. It complements a default-deny packet filter from iptables basics and key-only logins from Harden SSH on Linux.
Machines Where fail2ban Runs With Room to Spare
| Server | Processor | Cores / threads | Memory | Storage | Network | Per month | Smallest order |
|---|---|---|---|---|---|---|---|
| E1 | Intel Xeon E3-1240 v3 | 4 / 8 | 16 GB DDR3 | 2x 240 GB SSD | 2x 1 Gbps | $30.52 | $91.56 (3 months) |
| E2 | Intel Xeon E3-1270 v3 | 4 / 8 | 32 GB DDR3 | 1x 480 GB SSD | 2x 1 Gbps | $38.32 | $114.96 (3 months) |
| S1 | Intel Xeon E-2276G | 6 / 12 | 64 GB DDR4 | 1x 1 TB NVMe | 2x 10 Gbps | $53.32 | $53.32 (1 month) |
| S6 | Intel Xeon E-2288G | 8 / 16 | 64 GB DDR4 | 2x 1 TB NVMe | 2x 10 Gbps | $89.92 | $89.92 (1 month) |
Keep Reading
Harden SSH on Linux
A practical hardening list for sshd on a fresh dedicated box: keys, users, rate limits, firewall scope and an outside audit.
Openiptables basics
How iptables tables, chains and targets fit together, plus a minimal default-deny rule set for a new bare metal server.
OpenChange the SSH port
Move sshd off port 22 on Ubuntu, Debian, Rocky, AlmaLinux and FreeBSD without locking yourself out of the machine.
OpenLog in with an SSH key
Create an ed25519 key, install it on your machine through the panel or ssh-copy-id, and log in without typing the root password.
OpenFAQ
Quick Answers
It is optional then. Password guesses cannot succeed, but fail2ban still trims log noise and protects other logins such as web forms and mail.
There is no auth.log by default. Set backend = systemd so the jail reads the journal.
Run fail2ban-client unban followed by the address, from a session on another address or after the ban expires.
No. Keep changes in jail.local or files under jail.d, which upgrades leave alone.
No. It reacts to failed logins in logs. Floods are absorbed by the edge DDoS filtering in front of the machine.