Fail2ban monitors your service logs for repeated failures and blocks offending IPs using your firewall. This guide explains the concept and gives copy paste jails for SSH on 22, HTTP on 80 or 443, and MySQL or MariaDB on 3306.

How Fail2ban works

  • Reads logs from services (for example: /var/log/auth.log, web server error or access logs, MySQL error log).
  • Filters match suspicious lines.
  • If failures exceed maxretry within findtime, Fail2ban bans the source IP for bantime via nftables on Debian 12.

Key terms:

  • jail: the rule that ties a filter and an action to specific logs and ports
  • filter: regex rules that match bad events in logs
  • action: how to ban (we use nftables-multiport)

Install Fail2ban

Base configuration

Do not edit jail.conf. Create your own overrides.

/etc/fail2ban/jail.local

Reload after changes:

Jail 1, protect SSH on port 22

/etc/fail2ban/jail.d/sshd.local

Check status:

Jail 2, protect HTTP on ports 80 and 443

Fail2ban reacts to patterns in logs. Two common web auth cases are below. Use only the one that applies to your stack.

Nginx basic auth:
/etc/fail2ban/jail.d/nginx-http-auth.local

Apache basic auth:
/etc/fail2ban/jail.d/apache-auth.local

Tip: run ls /etc/fail2ban/filter.d to see other web filters such as nginx-noscript or apache-badbots, then point a jail at the filter and the correct log path.

Jail 3, watch MySQL or MariaDB on port 3306

Best practice is not to expose 3306 publicly. Bind to localhost or a private network and restrict with your firewall. If you must watch for brute force on 3306:

/etc/fail2ban/jail.d/mysqld-auth.local

Confirm the error log path inside MariaDB or MySQL:

If your DB logs only to the journal, you can switch a jail to use systemd:

Optional, recidive jail for repeat offenders

/etc/fail2ban/jail.d/recidive.local

Apply and verify

Check configuration syntax:

Reload Fail2ban:

List jails and show bans:

Unban a specific IP:

Check nftables sets for banned IPs:

Troubleshooting

  • Filters available:
  • Test a filter against a log:
  • Reverse proxy or CDN:
    Ensure your web server logs the real client IP, not the proxy, otherwise you will ban the proxy’s IP.
  • Dockerized services:
    Make sure containers write logs to files on the host or to journald so Fail2ban can read them.

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.