Fail2ban becomes useful the moment you treat the default sshd jail as a starting point, not the finish line. The real value is in custom jails for your own services, action wiring that integrates with your firewall and alerting, and the recidive meta-jail that escalates repeat offenders. This article goes past “install and walk away” into the configuration you actually need for a multi-service host.
How to verify
Inspect the running state, the jails, and the rules they emit into the firewall:
sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo iptables -S | grep -i f2b
sudo fail2ban-client get sshd banip
sudo journalctl -u fail2ban --since "1 hour ago"
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf
fail2ban-client status lists active jails. The per-jail status shows current bans, total bans, and the filter regex it uses. fail2ban-regex is the test tool that proves a filter matches a log line without restarting the daemon.
What’s happening
Fail2ban watches log files. When a filter regex matches maxretry times within findtime, an action runs — typically inserting an iptables/nftables rule to drop the offending IP for bantime seconds. A jail is the named composition of a filter, an action, and the log files to watch. The architecture is plain: filters are regex, actions are shell snippets, jails glue them together.
The defaults ship in /etc/fail2ban/jail.conf (overwritten by package upgrades) and /etc/fail2ban/jail.d/*.local (operator-owned, persistent). Filters live in /etc/fail2ban/filter.d/ and use a slightly extended regex syntax with the <HOST> placeholder that matches the offending IP. Actions in /etc/fail2ban/action.d/ choose the firewall (iptables-multiport, nftables-allports, firewalld-rich-rules), plus optional secondary actions like SMTP notification or Slack webhook.
The recidive jail watches Fail2ban’s own log and bans IPs that have been banned multiple times across multiple jails — the right hammer for the brute-forcer who patiently tries SSH, then SMTP, then WordPress wp-login.
The procedure
- Never edit
/etc/fail2ban/jail.conf. Create/etc/fail2ban/jail.localwith your global defaults:ignoreip = 127.0.0.1/8 ::1 10.0.0.0/8,bantime = 3600,findtime = 600,maxretry = 5,banaction = nftables-multiport(oriptables-multiporton legacy hosts). - Enable the jails you actually want, one file per concern.
/etc/fail2ban/jail.d/sshd.localwith[sshd]\nenabled = true\nbantime = 1d\nfindtime = 10m\nmaxretry = 3\nport = ssh. - Write a custom filter when the bundled one doesn’t fit. Create
/etc/fail2ban/filter.d/myapp-auth.confwith[Definition]\nfailregex = ^.*auth failed for user .* from <HOST>$\nignoreregex =. Test withfail2ban-regex /var/log/myapp.log /etc/fail2ban/filter.d/myapp-auth.confbefore enabling the jail. - Wire the recidive jail explicitly.
/etc/fail2ban/jail.d/recidive.localwith[recidive]\nenabled = true\nlogpath = /var/log/fail2ban.log\nbantime = 1w\nfindtime = 1d\nmaxretry = 5\nbanaction = nftables-allports. Recidive bans cover all ports, not just the one that triggered the original ban. - Add a secondary action for visibility. Edit your jail to use
action = %(action_)s\n slack[webhook=<url>]— a denial that no one sees might as well not exist. Theslackaction ships in modern packages; older systems can pipe tocurldirectly from a custom action. - Persist bans across restarts. The default state file
/var/lib/fail2ban/fail2ban.sqlite3already does this; verify withsudo fail2ban-client get sshd banipafter asystemctl restart fail2ban. - Reload after every config change.
sudo fail2ban-client reloadre-reads everything without dropping the in-memory ban list.sudo fail2ban-client reload <jail>reloads one jail only.
Common pitfalls
- Filters use the
<HOST>token, which is alias-mapped to an IPv4 OR IPv6 capture group. Afailregexthat writes a literal(?P<host>...)group breaks IPv6 matching. ignoreipaccepts CIDR blocks and DNS names. A DNS-based entry is resolved at load time, not at ban time — a dynamic IP that moves in mid-incident still gets banned.- The
nftablesbackend creates its own table (f2b-table); anft flush rulesetremoves Fail2ban’s rules without telling Fail2ban, and the daemon believes the bans are still active. Reload Fail2ban after any global firewall change. - Log rotation that uses
copytruncateinstead of moving the file confuses Fail2ban’s inotify watch — switch the application’s logrotate config to standard rotation with a postrotatefail2ban-client reload. - Whitelisting your office IP in
ignoreipis not enough if you SSH from a laptop on the road — pair it with a bastion + ssh-key-only access and let Fail2ban handle the rest.
In the engagements we run, Fail2ban is one piece of the layered defence — UFW or nftables provides the static perimeter, Fail2ban handles dynamic abuse, and a SIEM ties both to user activity. That layered model is part of Managed Operations; for the standalone basics, see the wave’s ufw-firewall-deep-dive.