Put a Linux server on the internet and check its authentication log a day later. You will almost certainly find a long list of failed SSH logins from addresses you have never heard of, trying usernames like admin, test, and oracle. Most of these are automated scanners. They rarely get in if you use SSH keys, but they fill your logs, waste resources, and occasionally find a weak spot.
Fail2ban is a small, long-standing open-source tool that reads your log files, spots patterns like repeated failed logins, and tells the firewall to block the offending IP for a while. This guide explains how to install and configure Fail2ban on Ubuntu or Debian, with notes for RHEL-based systems.
How Fail2ban works
Fail2ban is built from three ideas:
- Filters are regular expressions that recognize a "failure" in a log, such as a rejected SSH password.
- Actions decide what happens when an IP misbehaves, usually adding a firewall rule through iptables, nftables, firewalld, or UFW.
- Jails tie a filter and an action to a log source, with rules like "5 failures within 10 minutes earns a 1-hour ban."
Fail2ban ships with filters for many common services, including SSH, Nginx, Apache, Postfix, and Dovecot, so you rarely need to write your own.
One important point: Fail2ban reduces noise and slows down brute-force attempts. It is not a replacement for strong authentication. Use SSH keys and disable password logins first; Fail2ban is the extra layer on top.
Step 1: Install Fail2ban
On Ubuntu and Debian:
sudo apt update
sudo apt install fail2ban -y
On Rocky Linux, AlmaLinux, and similar, Fail2ban comes from the EPEL repository:
sudo dnf install epel-release -y
sudo dnf install fail2ban -y
Enable and start the service:
sudo systemctl enable --now fail2ban
Step 2: Create jail.local (never edit jail.conf)
Fail2ban's defaults live in /etc/fail2ban/jail.conf, but package updates can overwrite that file. Put your settings in /etc/fail2ban/jail.local instead; anything there overrides the defaults.
sudo nano /etc/fail2ban/jail.local
A solid starting configuration:
[DEFAULT]
# Never ban yourself: add your home or office IP here
ignoreip = 127.0.0.1/8 ::1 203.0.113.10
bantime = 1h
findtime = 10m
maxretry = 5
# Ban repeat offenders for longer each time
bantime.increment = true
bantime.maxtime = 1w
[sshd]
enabled = true
port = ssh
What these settings mean:
- ignoreip: addresses that are never banned. Add your own static IP if you have one (the example address above is a documentation placeholder; replace it).
- findtime and maxretry: five failures within ten minutes triggers a ban.
- bantime: how long a ban lasts. With
bantime.incrementenabled, repeat offenders get progressively longer bans, capped atbantime.maxtime.
If you moved SSH to a non-standard port, set port = 2222 (or whatever you use) in the [sshd] section, otherwise the ban will block the wrong port.
Logs on systemd-based systems
Recent Debian releases may not write a traditional /var/log/auth.log file by default, logging only to the systemd journal. If Fail2ban complains it cannot find the log file, tell the jail to read from the journal:
[sshd]
enabled = true
backend = systemd
Step 3: Restart and verify
sudo systemctl restart fail2ban
sudo fail2ban-client status
You should see a list of active jails. Check one in detail:
sudo fail2ban-client status sshd
The output shows the number of currently failed attempts, total failures, currently banned IPs, and the list of banned addresses. On a public server, do not be surprised if that list starts filling within hours.
Protecting web applications
SSH is not the only target. Fail2ban can also watch web server logs.
Nginx HTTP basic auth
[nginx-http-auth]
enabled = true
Block bots hunting for scripts
The bundled nginx-botsearch filter catches requests for common exploit paths:
[nginx-botsearch]
enabled = true
logpath = /var/log/nginx/access.log
maxretry = 2
WordPress login attacks
There is no stock WordPress filter, but you can write a simple one. Create /etc/fail2ban/filter.d/wordpress-login.conf:
[Definition]
failregex = ^<HOST> .* "POST /wp-login\.php
ignoreregex =
Then add a jail with a stricter threshold, since legitimate users rarely post to the login page many times in a minute:
[wordpress-login]
enabled = true
port = http,https
filter = wordpress-login
logpath = /var/log/nginx/access.log
maxretry = 6
findtime = 1m
Because this counts every login POST, not just failures, keep the threshold reasonable. Test any custom filter before relying on it:
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/wordpress-login.conf
fail2ban-regex reports how many lines matched, so you can see whether the filter is too loose or too strict.
Working with UFW or firewalld
By default Fail2ban uses iptables or nftables actions directly. If you prefer bans to show up in your firewall tool, set the action in [DEFAULT]:
banaction = ufw # Ubuntu with UFW
# banaction = firewallcmd-rich-rules # RHEL-style with firewalld
Everyday commands
# Unban an IP (for example, you locked yourself out from another connection)
sudo fail2ban-client set sshd unbanip 198.51.100.25
# Ban an IP manually
sudo fail2ban-client set sshd banip 198.51.100.25
# Watch Fail2ban's own log
sudo tail -f /var/log/fail2ban.log
If you do lock yourself out, connect from a different IP or use your provider's web console, then unban your address and add it to ignoreip.
Limitations to keep in mind
- Distributed attacks from thousands of IPs, each trying only a few times, can stay under the threshold.
- IPv6 support depends on your Fail2ban version and firewall actions; current releases handle it, but verify on your system.
- Behind a proxy or CDN, your web server logs may show the proxy's IP instead of the visitor's. Configure real-IP handling first, or Fail2ban could ban your CDN.
Wrapping up
Installing and configuring Fail2ban takes about fifteen minutes and immediately cuts down the brute-force noise hitting your server. Put your settings in jail.local, whitelist your own IP, enable the SSH jail, add web jails where they make sense, and test custom filters with fail2ban-regex. Combined with SSH keys, a firewall, and regular updates, it makes your server a much harder target.