$ run as a normal user# run as root (or with sudo)
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).
| Item | Value |
|---|---|
| Packages | RHEL: fail2ban + fail2ban-firewalld (EPEL); Ubuntu: fail2ban |
| Service | fail2ban.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 |
| Tools | fail2ban-client, fail2ban-regex |
Remember
Never edit jail.conf — updates overwrite it. Put every change in .local files; they win over the .conf files.
Install on RHEL, Rocky, Alma and Fedora
Install on Ubuntu and Debian
apt-get update
apt-get install -y fail2ban
systemctl enable --now fail2banFirst configuration: defaults and the SSH jail
[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| Setting | Why |
|---|---|
ignoreip | Your office, VPN and admin IPs are never banned. Set it first. |
bantime, findtime, maxretry | 5 failures in 10 minutes → banned 1 hour. |
bantime.increment | Repeat attackers get longer bans (up to 1 week). |
backend = systemd | Read 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: sshdProtect Nginx and other services
Fail2ban has ready filters for many services. For an Nginx web server:
[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).
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:
[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[shop-login]
enabled = true
backend = auto
filter = shop-login
logpath = /var/log/shop/app.log
maxretry = 5
port = http,httpsfail2ban-client reload
fail2ban-client status shop-loginDaily 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-rulesReal 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.
Make it safe
Security checklist:
ignoreipwith 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.logand the banned counts sometimes; alert when Fail2ban stops.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
A jail does not start: Have not found any log file for … jail | logpath does not exist, or wrong backend. | Fix the path; for journald use backend = systemd. |
| Nobody gets banned | The filter does not match your log format. | fail2ban-regex LOG FILTER; adjust failregex. |
| You banned yourself | Your 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 banned | The app logs the proxy IP. | Log the real client IP; add the LB to ignoreip. |
| Bans exist but traffic still comes in | The ban action does not match your firewall. | banaction = firewalld / nftables / ufw for your system; check the rules. |
ERROR … Unable to read the filter | Typo in the filter name or file. | fail2ban-client -t shows the problem. |
| Service fails after an update on Ubuntu | Old config option. | fail2ban-client -t; journalctl -u fail2ban. |
Next steps
- Quick how-to: Block brute-force logins with Fail2ban.
- Command page: fail2ban-client.
- At work: Block brute force.
- The other layers: OpenSSH server, firewalld, UFW, Nginx.
- Practise reading attack logs: Lab: investigate an SSH brute-force attack from the logs.
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.