Skip to content

Fail2ban for SSH — banning brute-force without locking yourself out

Fail2ban turns failed SSH attempts into temporary firewall bans. The configuration is straightforward; the gotchas (your own IP, IPv6, F2B vs. UFW interaction) are where the time goes.

A public SSH port on the internet sees brute-force attempts within minutes of being reachable. Most are noise — random username/password guessing against passwordless-key-only hosts that will never let them in. The noise is still expensive: it fills the journal, it forces the auth subsystem to do work, and on a host without rate-limiting it can saturate a small VM’s CPU. Fail2ban is the standard answer: parse the auth log, count failed attempts per source IP, drop them into the firewall for a configurable period. This article documents the configuration we apply on internet-facing SSH hosts.

How to verify

Check current state.

sudo systemctl status fail2ban --no-pager
sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo iptables -L -n | grep -i f2b
sudo nft list ruleset 2>/dev/null | grep -A 5 'fail2ban'

fail2ban-client status sshd is the most useful command — it lists currently-banned IPs, the count of failed attempts, and the file (or journal target) the jail is watching.

What’s happening

Fail2ban runs as a daemon. It watches a set of log sources for patterns (regex matches) defined per “jail” — a jail being a (filter, action, target) triple. The default sshd jail watches the auth log (or journalctl -u ssh) for “Failed password” / “Invalid user” / “authentication failure” lines. When the same source IP exceeds maxretry failures within findtime, fail2ban runs the action — typically inserting an iptables/nftables rule that drops traffic from that IP for bantime.

The interaction with UFW is the most common confusion. UFW’s rules live in their own chains; fail2ban inserts rules into chains that get jumped to from the INPUT chain. If your fail2ban-client status sshd says “banned 10 IPs” but sudo iptables -L INPUT -n shows none of them, the firewall is layered correctly and fail2ban is doing its job. To see the bans, look at the f2b-sshd chain.

A second consideration is journal-based vs. file-based watching. On modern systemd hosts, sshd often does not write to /var/log/auth.log; it logs to the journal. The default Debian/Ubuntu fail2ban package handles both, but the jail must specify backend = systemd to use the journal. We standardize on the systemd backend.

The procedure

  1. Install fail2ban.

    sudo apt install -y fail2ban

    On RHEL-family: sudo dnf install -y fail2ban fail2ban-firewalld.

  2. Create a local override. Never edit /etc/fail2ban/jail.conf directly — it is overwritten on package upgrade. Create /etc/fail2ban/jail.local:

    # /etc/fail2ban/jail.local
    [DEFAULT]
    # 10-minute window, 5 strikes, 1-hour ban
    bantime  = 1h
    findtime = 10m
    maxretry = 5
    
    # Use the systemd journal as the source — works on modern hosts
    backend = systemd
    
    # Whitelist trusted networks so you cannot lock yourself out
    ignoreip = 127.0.0.1/8 ::1 10.0.0.0/8 192.168.0.0/16 203.0.113.0/24
    
    # Use UFW as the action if installed
    banaction = ufw
    
    [sshd]
    enabled  = true
    port     = ssh
    filter   = sshd
    maxretry = 5
    bantime  = 1h

    The ignoreip line is the single most important one: it lists the networks that cannot be banned no matter what. Always include the host’s own management network, the office VPN range, and any CI runner IPs that might fail keys during deploy.

  3. Increase the ban for repeat offenders. A persistent attacker that comes back after a one-hour ban is not deterred. The recidive jail bans IPs that have been banned multiple times.

    [recidive]
    enabled  = true
    logpath  = /var/log/fail2ban.log
    banaction = ufw
    bantime  = 1w
    findtime = 1d
    maxretry = 5

    An IP banned by the sshd jail 5 times in 24 hours gets a one-week ban from the recidive jail.

  4. Restart and verify.

    sudo systemctl restart fail2ban
    sudo systemctl status fail2ban --no-pager
    sudo fail2ban-client status
    sudo fail2ban-client status sshd

    You should see Jail list: sshd, recidive. The sshd jail status line “Currently banned: 0” right after a restart is expected; it fills in over the next 10 minutes of internet noise.

  5. Test a ban locally. From a host you control (not the production VM itself), make a few failed SSH attempts:

    # From a test client, against the production host
    for i in 1 2 3 4 5 6; do ssh -o BatchMode=yes -o ConnectTimeout=5 nobody@server.example.net 2>/dev/null; done

    Then on the server:

    sudo fail2ban-client status sshd
    # Should list the test client's IP under "Banned IP list"

    To unban a test:

    sudo fail2ban-client set sshd unbanip 203.0.113.42
  6. Inspect the firewall chains.

    sudo iptables -L f2b-sshd -n --line-numbers
    sudo nft list chain inet filter f2b-sshd 2>/dev/null

    Each ban adds a DROP rule. The rule disappears when the bantime elapses.

  7. IPv6. Modern fail2ban handles IPv6 by default if allowipv6 = auto (or omitted, which defaults to auto on newer versions). Verify:

    sudo ip6tables -L f2b-sshd -n 2>/dev/null
    sudo grep allowipv6 /etc/fail2ban/fail2ban.conf

    If the firewall front-end is ufw, UFW handles both families uniformly.

  8. For production fleets, ship the config via configuration management. The ignoreip list is fleet-wide and should be version-controlled:

    - name: Fail2ban jail.local
      ansible.builtin.template:
        src: fail2ban-jail.local.j2
        dest: /etc/fail2ban/jail.local
        owner: root
        group: root
        mode: '0644'
      notify: Restart fail2ban
  9. Watch the log for false positives. A misconfigured CI runner or a colleague typing their password wrong three times in a row will get banned.

    sudo journalctl -u fail2ban --since today | grep -E 'Ban|Unban'

    If the same trusted IP shows up in bans, add it to ignoreip.

  10. Combine with the SSH-level rate limit. UFW’s limit directive and sshd’s MaxStartups work at different layers. Fail2ban handles the slower, persistent attackers; UFW limit handles the bursty scanner; MaxStartups handles the SSH-level connection storm. All three are complementary.

Common pitfalls

  • Not setting ignoreip — first time the on-call accidentally types the wrong password three times, they ban themselves and the cloud console is the only way back in.
  • Watching /var/log/auth.log on a system that no longer writes to that file (because rsyslog was disabled when journald was chosen). Result: zero bans, no errors. Set backend = systemd.
  • IPv6 traffic skipping the IPv4-only iptables rules. Verify with ip6tables -L or test from an IPv6-only client.
  • Banaction conflict — if UFW and iptables are both managing rules, fail2ban may insert into the wrong chain. Pick one (we pick UFW) and use banaction = ufw consistently.
  • bantime = -1 (permanent ban) sounds appealing but slowly accumulates a huge state table; use long-but-finite (a week, a month) instead.

In the engagements we run, fail2ban is on by default on every internet-facing SSH host, with a fleet-wide ignoreip and a recidive jail for the persistent offenders. The output is mostly invisible to operators because the right IPs are whitelisted up front — that whitelist discipline is part of the Managed Operations provisioning floor.