NewCode CLOUD — 20% off your first month on all cloud VPSClaim

Fail2ban: Stop SSH Brute-Force Attacks and Protect Nginx, WordPress and Mail

Look at the authentication log of any server with SSH open to the internet and you will find thousands of failed logins from bots trying root, admin and ubuntu with common passwords. With key-only authentication they cannot get in, but they still fill your logs, consume CPU and — on web login pages — can slow the site. Fail2ban watches log files, spots repeated failures from the same IP and blocks that IP in the firewall for a while.

How Fail2ban works

  • A filter is a set of regular expressions that match failure lines in a log.
  • A jail connects a filter to a log file, with thresholds: how many failures (maxretry) within what time (findtime) trigger a ban, and for how long (bantime).
  • An action performs the ban — usually a firewall rule via nftables, iptables, UFW or firewalld.

Fail2ban ships with filters for SSH, Nginx, Apache, Postfix, Dovecot and many more.

Install

# Ubuntu / Debian
sudo apt install fail2ban

# AlmaLinux / Rocky (from EPEL)
sudo dnf install epel-release
sudo dnf install fail2ban fail2ban-firewalld

Configure: never edit jail.conf

/etc/fail2ban/jail.conf is replaced on upgrades. Put your settings in /etc/fail2ban/jail.local or in files under /etc/fail2ban/jail.d/.

sudo nano /etc/fail2ban/jail.local
[DEFAULT]
# Your own static IPs — never banned. Add your office/home IPs here.
ignoreip = 127.0.0.1/8 ::1 198.51.100.7

bantime  = 1h
findtime = 10m
maxretry = 5

# Increase ban time for repeat offenders (Fail2ban 0.11+)
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 1w

# Ubuntu/Debian: read the systemd journal for SSH
backend = systemd

[sshd]
enabled = true
port    = ssh

If you moved SSH to another port, set port = 2222 (or whatever you use) so the ban blocks the right port. On AlmaLinux/Rocky with firewalld, also add:

[DEFAULT]
banaction = firewallcmd-rich-rules

Start it:

sudo systemctl enable --now fail2ban
sudo fail2ban-client status
sudo fail2ban-client status sshd

The sshd status shows how many IPs failed and how many are currently banned.

Choosing sensible values

Setting Typical Notes
maxretry 3–5 Lower for SSH with keys only (real users do not mistype keys)
findtime 10m Window in which failures are counted
bantime 1h with increment Long first bans mostly punish legitimate users who mistyped
ignoreip Your fixed IPs Essential — otherwise you can ban yourself

If you lock yourself out, log in through the VNC console in your control panel and unban your IP (see below).

Protecting Nginx

Password-protected areas (HTTP basic auth)

[nginx-http-auth]
enabled  = true
port     = http,https
logpath  = /var/log/nginx/error.log
backend  = auto

Bad bots scanning for vulnerabilities

Scanners request URLs such as /wp-login.php, /.env or /phpmyadmin on sites that do not have them. A simple custom filter catches repeated 404s on such paths. Create /etc/fail2ban/filter.d/nginx-probe.conf:

[Definition]
failregex = ^<HOST> -.*"(GET|POST) /(\.env|\.git|wp-config\.php|phpmyadmin|pma|xmlrpc\.php|vendor/phpunit)[^"]*" (403|404)
ignoreregex =

And the jail:

[nginx-probe]
enabled  = true
port     = http,https
logpath  = /var/log/nginx/access.log
backend  = auto
maxretry = 3
bantime  = 1d

Do not include xmlrpc.php if your WordPress site uses it (the Jetpack plugin and some apps do).

Protecting WordPress logins

WordPress returns HTTP 200 on a failed login, so it cannot be matched from the access log alone. Two approaches:

  1. Match POSTs to wp-login.php. Many failed POSTs in a short time are an attack, whatever the response:
# /etc/fail2ban/filter.d/wordpress-login.conf
[Definition]
failregex = ^<HOST> -.*"POST /wp-login\.php
ignoreregex =
[wordpress-login]
enabled  = true
port     = http,https
logpath  = /var/log/nginx/access.log
backend  = auto
maxretry = 6
findtime = 5m
  1. Use a plugin that logs failed logins in a Fail2ban-friendly way (for example “WP fail2ban”), then use the matching filter it provides.

Rate limiting in Nginx complements this — see Nginx rate limiting.

Mail servers

If you run Postfix and Dovecot (only possible where port 25 is open, such as US VPS), enable the built-in jails:

[postfix]
enabled = true
mode    = aggressive

[dovecot]
enabled = true

Repeat offenders: the recidive jail

recidive watches Fail2ban’s own log and bans IPs that were banned several times — for much longer:

[recidive]
enabled  = true
logpath  = /var/log/fail2ban.log
backend  = auto
bantime  = 1w
findtime = 1d
maxretry = 3

With bantime.increment enabled, you may not need it, but it is a simple extra layer.

Test filters before trusting them

sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-probe.conf

It reports how many lines matched. If the number is zero, the regex or log path is wrong; if it is huge, you may ban real visitors.

Managing bans

sudo fail2ban-client status sshd                 # list banned IPs
sudo fail2ban-client set sshd unbanip 203.0.113.5
sudo fail2ban-client set sshd banip 203.0.113.5  # ban manually
sudo fail2ban-client reload                      # after editing config
sudo tail -f /var/log/fail2ban.log

What Fail2ban does not do

  • It is not DDoS protection. Attacks from thousands of IPs, or floods that saturate your bandwidth, need network-level mitigation — see DDoS protected hosting.
  • It does not fix weak passwords. Use SSH keys and strong unique passwords for web logins.
  • It reacts after failures are logged; it reduces noise and slows attackers, but your first line of defence is still a firewall that exposes only the ports you need.

Checklist

  • Fail2ban installed and running, sshd jail enabled
  • Your own IPs in ignoreip
  • Correct SSH port if you changed it
  • Web jails tested with fail2ban-regex
  • Incremental bans or recidive enabled
  • You know how to unban an IP from the VNC console