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.
Install
Section titled “Install”sudo dnf install -y fail2ban-server fail2ban-firewalldMeasured 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/).
Minimal config: jail.local
Section titled “Minimal config: jail.local”Never edit jail.conf (overwritten on upgrade). Create /etc/fail2ban/jail.local:
[sshd]enabled = truebackend = systemdmaxretry = 5findtime = 10mbantime = 1hWhat the keys mean:
| Key | Value | Why |
|---|---|---|
backend | systemd | mandatory 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 |
maxretry | 5 | five failures trigger a ban |
findtime | 10m | counting window |
bantime | 1h | ban length; add bantime.increment = true for escalation |
sudo systemctl enable --now fail2bansudo fail2ban-client status sshdMeasured 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: 0The Journal matches line is the proof the filter is actually wired to the journal.
Administration: manual ban and unban
Section titled “Administration: manual ban and unban”sudo fail2ban-client set sshd banip 203.0.113.5 # bansudo fail2ban-client set sshd unbanip 203.0.113.5 # unbansudo fail2ban-client status sshd # counters and listMeasured state after a manual ban:
|- Currently banned: 1`- Banned IP list: 203.0.113.5firewalld integration, verified
Section titled “firewalld integration, verified”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:
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.confMeasured: 1 matched. Two gotchas:
- the line needs the
sshd[pid]prefix — the filter’sprefregexrequires that structure, and a bare message body yields0 matched - lines ending in
[preauth]such asConnection closed by ...do not count as failures under the defaultmode = normal; addmode = aggressiveto 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 timebantime.maxtime = 1w
[sshd]enabled = truebackend = systemdignoreip = 127.0.0.1/8 192.168.2.0/24 # never ban your own segmentWrite ignoreip before enabling anything — one config mistake plus a run of failures otherwise locks you out.
Common traps
Section titled “Common traps”| Symptom | Cause |
|---|---|
| Total failed stays 0 | backend is not systemd (F45 has no auth.log file) |
| banned IPs still connect | fail2ban-firewalld missing; actions fall back to nonexistent iptables |
| locked yourself out | no ignoreip for your own segment |
| filter never matches | test line lacks the sshd[pid] prefix, or preauth lines need aggressive mode |
| jail not up after reboot | enabled = true must be inside the [sshd] section of a file named jail.local |
| fail2ban-regex reports all missed | testing bare lines; the prefregex needs the journal-style prefix |
Related pages
Section titled “Related pages”- journalctl — the data source
- firewalld — where rich rules live
- SSH and keys — keys-only login beats any amount of banning
- Upstream: fail2ban wiki