Advanced
Searching inInstall guides
How to search
  • Paste an error message in quotes, e.g. "permission denied", to find the fix table that explains it.
  • Choose an area and a part of the guide (install, first settings, daily use, security, fixes) to go straight to the right step.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Guide 41Fail2ban · Security

Install and set up Fail2ban

Fail2ban watches your logs for attacks — wrong SSH passwords, failed web logins, scanners — and bans the attacking IP in the firewall for a time. It stops password guessing and cuts log noise on every server that is reachable from the internet. This guide installs Fail2ban on RHEL and Ubuntu, sets safe defaults in jail.local, protects SSH and Nginx, writes and tests a filter for your own app, and covers unbanning and the common mistakes.

  • 11 Lessons
  • 7 Commands

$ run as a normal user# run as root (or with sudo)

41.1

Know it before you install

A filter is a set of regular expressions that find a bad line and the IP in it (<HOST>). A jail = a filter + a log source + limits (maxretry tries in findtime) + an action (ban with firewalld, nftables or iptables for bantime).

ItemValue
PackagesRHEL: fail2ban + fail2ban-firewalld (EPEL); Ubuntu: fail2ban
Servicefail2ban.service
Defaults (do not edit)/etc/fail2ban/jail.conf, /etc/fail2ban/filter.d/, /etc/fail2ban/action.d/
Your settings/etc/fail2ban/jail.local or /etc/fail2ban/jail.d/*.local
Log/var/log/fail2ban.log
Toolsfail2ban-client, fail2ban-regex

Remember

Never edit jail.conf — updates overwrite it. Put every change in .local files; they win over the .conf files.

41.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install epel-release
dnf -y install fail2ban fail2ban-firewalld
systemctl enable --now fail2ban

fail2ban-firewalld sets the ban action to firewalld (rich rules), so bans work with the RHEL firewall.

41.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y fail2ban
systemctl enable --now fail2ban

41.4

First configuration: defaults and the SSH jail

/etc/fail2ban/jail.local
[DEFAULT]
ignoreip  = 127.0.0.1/8 ::1 10.0.0.0/24 203.0.113.10
bantime   = 1h
findtime  = 10m
maxretry  = 5
bantime.increment = true
bantime.maxtime   = 1w
backend   = systemd
 
[sshd]
enabled  = true
mode     = aggressive
maxretry = 4
SettingWhy
ignoreipYour office, VPN and admin IPs are never banned. Set it first.
bantime, findtime, maxretry5 failures in 10 minutes → banned 1 hour.
bantime.incrementRepeat attackers get longer bans (up to 1 week).
backend = systemdRead the journal (works on RHEL and new Ubuntu without auth log files).
mode = aggressive (sshd)Also catch scans and other bad SSH messages, not only wrong passwords.

On Ubuntu with UFW set banaction = ufw in [DEFAULT] (or nftables); on RHEL the fail2ban-firewalld package already sets firewalld.

fail2ban-client -t
OK: configuration test is successful
systemctl restart fail2ban
fail2ban-client status
Status
|- Number of jail:      1
`- Jail list:   sshd

41.5

Protect Nginx and other services

Fail2ban has ready filters for many services. For an Nginx web server:

/etc/fail2ban/jail.d/nginx.local
[nginx-http-auth]
enabled  = true
backend  = auto
logpath  = /var/log/nginx/error.log
 
[nginx-limit-req]
enabled  = true
backend  = auto
logpath  = /var/log/nginx/error.log
maxretry = 10
 
[nginx-botsearch]
enabled  = true
backend  = auto
logpath  = /var/log/nginx/access.log

(nginx-limit-req bans IPs that hit your limit_req limits again and again.) Other ready jails: postfix, dovecot, recidive (ban repeat offenders for a long time).

41.6

Your own app: write and test a filter

Your app writes lines like 2026-11-16 10:12:01 Login failed for user bob from 203.0.113.9. Write a filter:

/etc/fail2ban/filter.d/shop-login.conf
[Definition]
failregex = ^.*Login failed for user \S+ from <HOST>$
ignoreregex =

Test it against the real log before you enable the jail:

fail2ban-regex /var/log/shop/app.log /etc/fail2ban/filter.d/shop-login.conf
Lines: 1520 lines, 0 ignored, 37 matched, 1483 missed
/etc/fail2ban/jail.d/shop.local
[shop-login]
enabled  = true
backend  = auto
filter   = shop-login
logpath  = /var/log/shop/app.log
maxretry = 5
port     = http,https
fail2ban-client reload
fail2ban-client status shop-login

41.7

Daily use: the commands and the real uses

fail2ban-client status sshd
Status for the jail: sshd
|- Filter
 |- Currently failed: 3
 `- Total failed:     812
`- Actions
   |- Currently banned: 14
   `- Banned IP list:   198.51.100.7 203.0.113.9 …
fail2ban-client set sshd unbanip 203.0.113.9
fail2ban-client set sshd banip 198.51.100.66
fail2ban-client banned
tail -f /var/log/fail2ban.log
firewall-cmd --list-rich-rules

Real uses at work:

  • Every internet-facing server: SSH jail on, admin IPs in ignoreip.
  • Web servers and apps: ban login brute force and scanners.
  • Mail servers: SMTP/IMAP login attacks.

41.8

Make it safe

Security checklist:

  • ignoreip with your admin and monitoring IPs — never ban yourself or your load balancer's health checks.
  • Fail2ban is an extra layer: still use SSH keys only, strong passwords and MFA in apps.
  • Behind a proxy/load balancer, the app must log the real client IP (X-Forwarded-For), or Fail2ban bans the proxy.
  • Test every custom filter with fail2ban-regex.
  • Check fail2ban.log and the banned counts sometimes; alert when Fail2ban stops.

41.9

Update and remove

dnf -y upgrade fail2ban fail2ban-firewalld
apt-get install --only-upgrade -y fail2ban
systemctl disable --now fail2ban
dnf -y remove fail2ban

Stopping Fail2ban removes its bans from the firewall.

41.10

Fix common problems

ProblemCauseFix
A jail does not start: Have not found any log file for … jaillogpath does not exist, or wrong backend.Fix the path; for journald use backend = systemd.
Nobody gets bannedThe filter does not match your log format.fail2ban-regex LOG FILTER; adjust failregex.
You banned yourselfYour IP is not in ignoreip.From another IP or the console: fail2ban-client set sshd unbanip YOUR_IP; add it to ignoreip.
The load balancer IP gets bannedThe app logs the proxy IP.Log the real client IP; add the LB to ignoreip.
Bans exist but traffic still comes inThe ban action does not match your firewall.banaction = firewalld / nftables / ufw for your system; check the rules.
ERROR … Unable to read the filterTypo in the filter name or file.fail2ban-client -t shows the problem.
Service fails after an update on UbuntuOld config option.fail2ban-client -t; journalctl -u fail2ban.

41.11

Next steps

1Why put your settings in jail.local and not in jail.conf?

Package updates overwrite jail.conf; .local files are kept and win over it.

2How do you check that a new filter really matches your log?

fail2ban-regex /path/to/log /etc/fail2ban/filter.d/name.conf.

All commands in this chapter