Fail2Ban protects Linux servers against repeated login failures by reading service logs and banning abusive IP addresses through the firewall. Install it using apt on Ubuntu or Debian, or dnf with EPEL on Rocky Linux or AlmaLinux. Configure SSH, Nginx and Apache in /etc/fail2ban/jail.local, then check active bans with fail2ban-client status sshd.

Tested on / compatibility: Ubuntu, Debian, Rocky Linux and AlmaLinux; Fail2Ban supplied by distribution repositories. Documentation reviewed: November 6, 2026.

Requirements

A Linux server with root or sudo access, running SSH, a supported firewall and readable service logs. Nginx or Apache is needed only if you want to protect a web server. Keep console access available before changing SSH protection.

Steps

Step 1. Install Fail2Ban

Ubuntu / Debian:

sudo apt update
sudo apt install -y fail2ban python3-systemd

Rocky Linux / AlmaLinux (EPEL):

sudo dnf install -y epel-release
sudo dnf install -y fail2ban fail2ban-systemd fail2ban-firewalld

On Rocky/AlmaLinux 9, enable CRB if required before installing EPEL. If using firewalld, keep its SSH rules and firewall integration working.

sudo systemctl enable --now fail2ban
fail2ban-client --version

Step 2. Configure jail.local

Keep local changes in /etc/fail2ban/jail.local, not jail.conf, because package updates can replace default configuration files. Create or edit the local file:

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

[sshd]
enabled = true
port = ssh
filter = sshd
backend = systemd

These settings control when addresses are blocked:

Parameter Purpose
bantime Ban duration, here 1 hour
findtime Time window for counting failures, here 10 minutes
maxretry Number of failures before a ban, here 5
ignoreip Trusted addresses excluded from bans
bantime.increment Increase bans for repeat offenders

Five matching SSH failures within ten minutes trigger a one-hour ban. Replace 203.0.113.10 with your own trusted management IP only if you need an exception. An unlimited ban uses bantime = -1.

Step 3. Enable SSH protection

The standard sshd filter can read authentication events directly from systemd-journald. If SSH uses a custom port, set port to that number. On Rocky/AlmaLinux, install the fail2ban-systemd integration as shown above.

For SSH, use the actual SSH port in the jail configuration; do not expose administration ports unnecessarily.

Step 4. Enable Nginx or Apache protection

Enable only the jail for the web server actually installed. The following standard filters detect HTTP authentication failures, not ordinary page views.

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

[apache-auth]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache2/error.log
backend = auto
maxretry = 5

For Apache on Rocky/AlmaLinux, use /var/log/httpd/error_log instead of /var/log/apache2/error.log. Confirm that each log file exists before enabling its jail. Do not use the old http-get-dos regex that matches every GET or POST request.

Step 5. Apply the configuration

Check the syntax, restart Fail2Ban and list the active jails:

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

Verify

Check the SSH jail, unban an address if necessary and test that filters match the expected log records:

sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip 203.0.113.10
sudo fail2ban-regex systemd-journal sshd

For Nginx or Apache, replace the file path and filter with the corresponding error log and filter name. Zero matches may simply mean no relevant errors have been logged.

sudo fail2ban-regex /var/log/nginx/error.log nginx-http-auth
sudo fail2ban-regex /var/log/apache2/error.log apache-auth

Troubleshooting

Fail2Ban does not start

Run fail2ban-client -t and inspect journalctl -u fail2ban -n 50. Check the syntax and whether configured log files exist.

SSH failures are not detected

Check backend = systemd, the systemd integration package and the SSH journal entries.

IP addresses are not banned

Check the jail status, ignoreip, firewall action and whether the actual events match the filter.

Your IP was banned

Use your provider console and run fail2ban-client set sshd unbanip with your IP address.

FAQ

What do bantime, findtime and maxretry mean?

bantime sets how long an IP remains blocked, findtime defines the observation window, and maxretry sets the failure threshold.

How do I unban an IP address?

Run sudo fail2ban-client set sshd unbanip 203.0.113.10, replacing the sample address with the blocked IP.

How do I make bans permanent?

Set bantime = -1 in the applicable jail. Use permanent bans carefully to avoid locking out legitimate users.

Does Fail2Ban work with Docker containers?

Yes, but the host must have access to container logs and the ban action must affect container traffic, often in Docker forwarding chains.

Fail2Ban or a firewall: do I need both?

Use both. A firewall enforces network rules, while Fail2Ban reads logs and dynamically creates bans through the firewall.

Related Articles

Protect Windows RDP from Brute Force; Official Fail2Ban documentation.

Protect your Linux server with Unihost

Choose a VPS for your application or use managed server administration for ongoing security maintenance.

VPS Hosting | Server Management