Skip to content

Fail2ban deep configuration — jails, filters, and actions

How Fail2ban's jail/filter/action model actually composes, how to write custom filters with regex, and how to keep the ban list short and useful.

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

  1. Never edit /etc/fail2ban/jail.conf. Create /etc/fail2ban/jail.local with your global defaults: ignoreip = 127.0.0.1/8 ::1 10.0.0.0/8, bantime = 3600, findtime = 600, maxretry = 5, banaction = nftables-multiport (or iptables-multiport on legacy hosts).
  2. Enable the jails you actually want, one file per concern. /etc/fail2ban/jail.d/sshd.local with [sshd]\nenabled = true\nbantime = 1d\nfindtime = 10m\nmaxretry = 3\nport = ssh.
  3. Write a custom filter when the bundled one doesn’t fit. Create /etc/fail2ban/filter.d/myapp-auth.conf with [Definition]\nfailregex = ^.*auth failed for user .* from <HOST>$\nignoreregex =. Test with fail2ban-regex /var/log/myapp.log /etc/fail2ban/filter.d/myapp-auth.conf before enabling the jail.
  4. Wire the recidive jail explicitly. /etc/fail2ban/jail.d/recidive.local with [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.
  5. 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. The slack action ships in modern packages; older systems can pipe to curl directly from a custom action.
  6. Persist bans across restarts. The default state file /var/lib/fail2ban/fail2ban.sqlite3 already does this; verify with sudo fail2ban-client get sshd banip after a systemctl restart fail2ban.
  7. Reload after every config change. sudo fail2ban-client reload re-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. A failregex that writes a literal (?P<host>...) group breaks IPv6 matching.
  • ignoreip accepts 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 nftables backend creates its own table (f2b-table); a nft flush ruleset removes 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 copytruncate instead of moving the file confuses Fail2ban’s inotify watch — switch the application’s logrotate config to standard rotation with a postrotate fail2ban-client reload.
  • Whitelisting your office IP in ignoreip is 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.