On this page
- What does fail2ban actually do?
- How do I install fail2ban?
- Why use jail.local and the systemd backend?
- How do I avoid banning myself?
- How do I start fail2ban and test a jail?
- How do I protect Nginx with fail2ban?
- How do I read fail2ban logs and keep it healthy?
- Why is my sshd jail not counting failures?
- Questions
What does fail2ban actually do?
fail2ban reads log entries, counts failures per source address with a set of regular expressions called a filter, and when an address crosses the limit it runs an action, normally a firewall rule that blocks it for a while. A jail is the combination of a filter, a log source, limits and an action. It does not make weak passwords safe; key based SSH logins (see SSH keys and hardening) remain the real protection. What fail2ban adds is quieter logs, less CPU spent on pointless handshakes, and a brake on credential stuffing against web logins.
How do I install fail2ban?
On Ubuntu 24.04, 22.04 and Debian 12 or 13, install it together with the Python journal bindings and the nft tool used for bans:
sudo apt update
sudo apt install fail2ban python3-systemd nftables
fail2ban-client --versionDo not enable nftables.service: its default config starts by flushing all rules. Only the nft command is needed.
On Rocky Linux 9 and AlmaLinux 9 the package comes from EPEL and includes firewalld integration:
sudo dnf install -y epel-release
sudo dnf install -y fail2ban fail2ban-firewalld
fail2ban-client --versionThe EL package drops /etc/fail2ban/jail.d/00-firewalld.conf, which makes bans use firewalld rich rules, so firewalld must be running.
Why use jail.local and the systemd backend?
Never edit jail.conf; package updates replace it. Your settings go into jail.local, which is read after the defaults. Debian 12 and 13 no longer write SSH events to /var/log/auth.log unless rsyslog is installed, so a jail that watches that file never starts. The systemd backend reads the journal directly and works on every distribution here. Create the file:
sudo nano /etc/fail2ban/jail.local[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
bantime.increment = true
bantime.maxtime = 1w
ignoreip = 127.0.0.1/8 ::1 203.0.113.7
backend = systemd
[sshd]
enabled = true
mode = aggressive
port = sshOn Ubuntu and Debian add two more lines under [DEFAULT] so bans go through nftables: banaction = nftables-multiport and banaction_allports = nftables-allports. On Rocky and AlmaLinux leave them out; the firewalld file sets the action. If SSH runs on another port, write the number in port =.
How do I avoid banning myself?
Put every address you work from into ignoreip, separated by spaces, before you enable the jail. Addresses there are never banned, whatever they do. If your home address changes often, add the range your provider uses or the address of a small jump host (see advanced SSH settings). Should you lock yourself out anyway, the ban expires after bantime; another network, such as a phone hotspot, reaches the server meanwhile and lets you lift it:
sudo fail2ban-client set sshd unbanip 203.0.113.7
sudo fail2ban-client unban --allBecause fail2ban only blocks addresses that failed, it cannot lock out a working key login from an address that did not fail first.
How do I start fail2ban and test a jail?
Check the configuration, then enable the service:
sudo fail2ban-client -t
sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban --no-pager
sudo fail2ban-client status
sudo fail2ban-client status sshdThe status output lists the number of currently failed and banned addresses and the journal match it uses. To prove that the ban action reaches the firewall, ban a documentation address by hand and look for it:
sudo fail2ban-client set sshd banip 192.0.2.55
sudo nft list ruleset | grep -B2 -A2 192.0.2.55
sudo firewall-cmd --list-rich-rules | grep 192.0.2.55
sudo fail2ban-client set sshd unbanip 192.0.2.55Use the nft line on Ubuntu and Debian, the firewall-cmd line on Rocky and AlmaLinux.
How do I protect Nginx with fail2ban?
Nginx writes to log files, not the journal, so its jails switch back to file reading with backend = auto. Append these jails to jail.local:
[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/access.log
maxretry = 10
[nginx-limit-req]
enabled = true
backend = auto
port = http,https
logpath = /var/log/nginx/error.logThe first catches failed HTTP basic auth, the second repeated requests for paths that only scanners look for, and the third bans clients that keep tripping an Nginx limit_req zone, which you configure as shown in Nginx rate limiting. Check a filter against real log lines before you rely on it:
sudo fail2ban-regex /var/log/nginx/error.log /etc/fail2ban/filter.d/nginx-http-auth.conf
sudo systemctl reload fail2banHow do I read fail2ban logs and keep it healthy?
fail2ban logs its own decisions to /var/log/fail2ban.log:
sudo tail -n 50 /var/log/fail2ban.log
sudo grep "Ban " /var/log/fail2ban.log | awk '{print $NF}' | sort | uniq -c | sort -rn | headThe second command lists the addresses banned most often. Bans are stored in a small SQLite database, so with bantime.increment enabled, repeat visitors get longer bans even after a restart. Updates arrive through the normal package manager; after an upgrade run sudo fail2ban-client -t once more, since filter files can change between versions.
Why is my sshd jail not counting failures?
Look at the journal match first. Since OpenSSH 9.8 the authentication work happens in a process called sshd-session, and old fail2ban releases only watched messages from sshd; the versions shipped with Debian 13 and current EL builds handle both. Confirm that failures appear at all:
sudo journalctl -u ssh -u sshd --since "1 hour ago" | grep -i "invalid user"
sudo fail2ban-client get sshd journalmatchIf the journal shows failures but the jail stays at zero, compare the _COMM value in journalctl -o verbose with the journal match. If the jail does not start at all, sudo journalctl -u fail2ban -n 30 names the line that failed to parse.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| A1-1 | fail2ban needs well under 100 MB of memory | 1 vCPU, 1 GB RAM, 25 GB NVMe | $3.60/mo | $43.20 for 12 months |
Questions about Block brute force logins with fail2ban
Is fail2ban still useful with key-only SSH?
Yes, for log noise and for web logins. Bots keep trying passwords even when the server refuses them, and fail2ban cuts those connections short. The real security still comes from disabling passwords.
How long should a ban last?
One hour with bantime.increment = true is a good start. Repeat offenders then get doubled bans up to the bantime.maxtime limit, while a one-off typo costs you little.
Does fail2ban work over IPv6?
Yes. Current releases track IPv6 addresses and the nftables and firewalld actions ban them in the matching address family.
Should I use fail2ban or CrowdSec?
Both solve the same core problem. fail2ban is small and fully local; CrowdSec adds shared block lists and a separate bouncer process. Run one of them per server, not both on the same logs.
Where do I whitelist a monitoring service?
Add its addresses to ignoreip in the [DEFAULT] section of jail.local, or in a single jail if only that jail should ignore it, then run sudo systemctl reload fail2ban.
More in Security
- UFW firewall on Ubuntu and Debian
- firewalld on Rocky Linux and AlmaLinux
- iptables rules that survive a reboot
- CrowdSec intrusion prevention
- Two-factor SSH logins with TOTP
- Disable or restrict root login
- Alerts for every SSH login
- Advanced OpenSSH settings
- Publish SSH host keys in DNS (SSHFP)
- Audit open ports with nmap
- File integrity checks with AIDE and Tripwire
- CSF firewall (ConfigServer Security)
- First security steps on Rocky and Alma
- Free TLS certificates with Certbot
- Self-signed certificates and a private CA
- Rate and connection limits in Nginx
- Wazuh security monitoring
- Kernel and sysctl hardening