Skip to content
Search Fedora

    Browse this documentation

    fail2ban on Fedora: automatic bans for SSH brute force

    Any SSH port on the public internet gets brute-forced. fail2ban is the lowest-effort counter: count failures, ban the IP, unban after a timeout. Everything here measured on Fedora 45.

    Terminal window
    sudo dnf install -y fail2ban-server fail2ban-firewalld

    Measured version 1.1.0. Division of labour: fail2ban-server is the core; fail2ban-firewalld brings the firewalld actions (firewallcmd-* configs, measured under /etc/fail2ban/action.d/).

    Never edit jail.conf (overwritten on upgrade). Create /etc/fail2ban/jail.local:

    [sshd]
    enabled = true
    backend = systemd
    maxretry = 5
    findtime = 10m
    bantime = 1h

    What the keys mean:

    KeyValueWhy
    backendsystemdmandatory on F45. sshd logs live in the journal, not a file; the default auto looks for /var/log/auth.log, finds nothing, and silently does nothing
    maxretry5five failures trigger a ban
    findtime10mcounting window
    bantime1hban length; add bantime.increment = true for escalation
    Terminal window
    sudo systemctl enable --now fail2ban
    sudo fail2ban-client status sshd

    Measured output on a clean machine:

    Status for the jail: sshd
    |- Filter
    | |- Currently failed: 0
    | `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd + _COMM=sshd-session
    `- Actions
    |- Currently banned: 0

    The Journal matches line is the proof the filter is actually wired to the journal.

    Terminal window
    sudo fail2ban-client set sshd banip 203.0.113.5 # ban
    sudo fail2ban-client set sshd unbanip 203.0.113.5 # unban
    sudo fail2ban-client status sshd # counters and list

    Measured state after a manual ban:

    |- Currently banned: 1
    `- Banned IP list: 203.0.113.5

    After banning an IP, firewalld gains a rich rule:

    rule family="ipv4" source address="203.0.113.5"
    port port="ssh" protocol="tcp" reject type="icmp-port-unreachable"

    The same source address appears in the nftables ruleset, and the rule disappears on unban. This is why fail2ban-firewalld matters: bans go through the firewalld interface, stay compatible with the system firewall, and leave nothing behind after a reboot.

    Debugging filters: fail2ban-regex done right

    Section titled “Debugging filters: fail2ban-regex done right”

    When a filter matches nothing, feed it a line in the real format:

    Terminal window
    printf "Oct 29 07:30:00 fd45-repo sshd[123]: Failed password for invalid user admin from 203.0.113.5 port 4242 ssh2\n" | \
    sudo fail2ban-regex - /etc/fail2ban/filter.d/sshd.conf

    Measured: 1 matched. Two gotchas:

    • the line needs the sshd[pid] prefix — the filter’s prefregex requires that structure, and a bare message body yields 0 matched
    • lines ending in [preauth] such as Connection closed by ... do not count as failures under the default mode = normal; add mode = aggressive to the jail to count them (at the cost of more false positives)

    Next level: escalating bans and a whitelist

    Section titled “Next level: escalating bans and a whitelist”
    [DEFAULT]
    bantime.increment = true # repeat offenders get double each time
    bantime.maxtime = 1w
    [sshd]
    enabled = true
    backend = systemd
    ignoreip = 127.0.0.1/8 192.168.2.0/24 # never ban your own segment

    Write ignoreip before enabling anything — one config mistake plus a run of failures otherwise locks you out.

    SymptomCause
    Total failed stays 0backend is not systemd (F45 has no auth.log file)
    banned IPs still connectfail2ban-firewalld missing; actions fall back to nonexistent iptables
    locked yourself outno ignoreip for your own segment
    filter never matchestest line lacks the sshd[pid] prefix, or preauth lines need aggressive mode
    jail not up after rebootenabled = true must be inside the [sshd] section of a file named jail.local
    fail2ban-regex reports all missedtesting bare lines; the prefregex needs the journal-style prefix