Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

What Fail2ban Does

Each jail joins a filter, a set of patterns that recognize a failure in a log, with an action, normally a firewall rule. When one address fails maxretry times within findtime, Fail2ban bans it for bantime. The standard actions reject the banned address's packets rather than drop them. Fail2ban cuts log noise and slows guessing; it does not replace SSH keys, as described in how to disable SSH password authentication.

Before You Start

  • Your own address: add the address you connect from to ignoreip, so a few typing mistakes cannot ban you.
  • The way back: keep your session open and know where the server console is in your provider's control panel. From the console you can unban any address.
  • Leave jail.conf alone: package updates replace it. Local settings go in /etc/fail2ban/jail.local or in files in /etc/fail2ban/jail.d/.

Install Fail2ban

# Ubuntu, Debian
sudo apt install fail2ban

# AlmaLinux, Rocky Linux (from EPEL)
sudo dnf install epel-release
sudo dnf install fail2ban
sudo systemctl enable --now fail2ban

On Ubuntu and Debian the service starts after installation, and the package's /etc/fail2ban/jail.d/defaults-debian.conf enables the sshd jail. On AlmaLinux and Rocky Linux, the EPEL package pulls in fail2ban-firewalld, which sends bans through firewalld, and you enable the jails yourself.

Create jail.local

sudo nano /etc/fail2ban/jail.local
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
ignoreip = 127.0.0.1/8 ::1 203.0.113.10

[sshd]
enabled = true
port = ssh
  • bantime: how long a ban lasts. The shipped default is 10 minutes.
  • findtime: the window in which failures are counted.
  • maxretry: how many failures are allowed within that window.
  • ignoreip: addresses and networks that are never banned, separated by spaces.

The SSH Jail on Each System

  • Custom SSH port: set port = 2222, with your number, in the [sshd] section. Otherwise the ban blocks port 22, which SSH no longer uses.
  • Ubuntu 24.04 and 26.04, Debian 13: the package already sets the sshd jail to read the systemd journal and to ban through nftables. No change is needed.
  • Debian 12: there is no text authentication log by default, only the journal, and the package still expects /var/log/auth.log and iptables. Add backend = systemd to the [sshd] section, and banaction = nftables and banaction_allports = nftables[type=allports] to [DEFAULT], as the newer packages do.
  • AlmaLinux and Rocky Linux: the shipped settings for these systems read the journal and ban through firewalld, so they usually work without a change.

Check the Configuration and the Status

sudo fail2ban-client -t
sudo systemctl restart fail2ban
sudo fail2ban-client status
sudo fail2ban-client status sshd

-t tests the configuration before the restart. status lists the active jails; status sshd shows the failures counted, the currently banned addresses, and the total number of bans. If the jail never counts a failure on a server reachable from the internet, it is probably reading the wrong log or backend.

Unban an Address

sudo fail2ban-client set sshd unbanip 203.0.113.10

If you banned yourself, run this from the server console or from another address, then add your address to ignoreip. A banned client usually sees "connection refused" rather than a timeout, because the standard actions reject packets.

Nginx Jails

Fail2ban ships filters for Nginx. Enable the ones that fit your site in jail.local:

[nginx-http-auth]
enabled = true

[nginx-botsearch]
enabled = true

nginx-http-auth bans repeated failed logins to pages protected with basic authentication, described in Nginx basic auth. nginx-botsearch bans clients that probe for well-known paths that do not exist. Both read the Nginx error log at its default location; set logpath if yours is elsewhere. On Ubuntu 24.04 the package makes the journal the default source for every jail, so add backend = auto to each Nginx section there. Test a filter against a real log with fail2ban-regex before you rely on it.

Longer Bans for Repeat Offenders

bantime.increment = true
bantime.maxtime = 1w

Add these lines to the existing [DEFAULT] section. Each new ban of the same address then lasts longer than the one before, up to the maximum. For a second factor on top of SSH keys, see SSH two-factor authentication.

Was this answer helpful?
Loading...