Find Out What the Default Jail Is Costing You
Fail2ban ships with sshd enabled and every other jail commented out in jail.conf. On a small VPS that default is weak in both directions: it watches one service, and it uses a 10-minute window with 5 retries, which a distributed SSH spray walks straight through. Check what is live with fail2ban-client status and then fail2ban-client status sshd for the ban counters.
Look at fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf output. The line count and the “matched” percentage matter: if fewer than 80 percent of failed-auth lines match, the filter is missing your log format and bans are silently never issued. Distro log lines differ, and systemd journal output is not a file at all, which is why backend=systemd is often required.
Put Local Overrides in jail.local, Never jail.conf
Package upgrades overwrite jail.conf. Everything you change belongs in /etc/fail2ban/jail.local, which is read after and wins. Set bantime=1h, findtime=1h and maxretry=4 as a baseline for SSH, and enable the distributed-friendly ignoreip for your own management addresses so you cannot lock yourself out mid-session.
Also decide the action carefully. The default iptables-multiport inserts one rule per banned IP, which is fine for hundreds and painful for tens of thousands. Check iptables -L f2b-sshd -n | wc -l after a week; if that number climbs past a few thousand, switch to ipset based actions so bans live in a hash table instead of a linear rule chain.
Use One Jail Per Service You Actually Expose
A default install protects SSH and nothing else. If nginx serves a login form, a jail on nginx-http-auth or a custom filter for your 401/403 patterns is what stops credential stuffing. For a mail server, postfix and dovecot jails exist already. List enabled services with ss -tlnp and only write jails for ports that are genuinely exposed to the internet.
Writing a custom filter is a four-line file in filter.d/: a failregex that anchors on the offending log line, and datepattern if your log format is unusual. Test it with fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/myapp.conf before enabling. Never enable a filter that has not been pointed at a real log sample — a broken regex fails open and looks healthy.
Tune findtime and maxretry Around Real Attack Shape
The default 10-minute findtime assumes an attacker tries five times quickly and gives up. Real SSH sprays from botnets use one attempt per IP per hour across thousands of addresses, so a short window never accumulates enough failures to trigger a ban. Widening findtime to 1–6 hours catches slow-and-low patterns that the default misses entirely.
The trade-off is false positives. If bantime is also long, a legitimate user with three typos is locked out for hours. The usual compromise is findtime=6h, maxretry=5, bantime=12h, plus ignoreip for known staff addresses. Track how many bans you issue per day with fail2ban-client status sshd; a sudden jump is either an attack or a filter that started over-matching.
Watch the CPU Cost of the Jail Itself
Fail2ban parses every new log line, and on a single-vCPU VPS with a busy nginx access log this is not free. Watch with top -p $(pgrep -d, -f fail2ban) during peak traffic. Above roughly 10 percent steady CPU on one core, the bottleneck is usually a filter with an inefficient regex or a findtime window so wide that the in-memory ban database grows large.
Two fixes work reliably. First, restrict each jail’s log source to the file or journal unit that matters instead of a broad glob. Second, consider dbpurgeage: entries older than this are purged from the ban database, and leaving it at the 1-day default keeps memory flat. On a virtualserversvps.com box with 1 GB or 2 GB of RAM, raising it for long ban windows means more resident memory, so weigh the ban durability against the headroom you have.
Avoid Common Bans That Break Your Own Stack
The most self-inflicted failure is banning the load balancer or monitoring probe. If Uptime Kuma or your provider’s health check polls an endpoint that returns 401, an auth-based filter will ban the monitor within hours and you will get a false outage alert. Add those source addresses to ignoreip before enabling any HTTP jail.
The second is banning your own SSH session because ignoreip lists an address that changed. Use a jump host or a fixed management IP. If you are already locked out, get console access, then run fail2ban-client set sshd unbanip 203.0.113.7 to lift the single ban rather than flushing the whole chain and losing all protection.
Confirm the Jail Actually Blocks
Verification is a real test, not a config read. From an external host, fail authentication repeatedly past maxretry, then confirm on the server with fail2ban-client status sshd that the count incremented and the IP appears in Banned IP list. Then check iptables -S f2b-sshd or ipset list f2b-sshd for the actual rule.
Finally, make sure the jail survives reboot: systemctl is-enabled fail2ban should read enabled, and the ban database at /var/lib/fail2ban/fail2ban.sqlite3 should persist bans across restarts. If you are rebuilding the box, the virtualserversvps.com plans all include root access, so you can install and tune fail2ban from a clean image and keep the config in version control rather than patching a legacy server by hand.

Leave a Reply
You must be logged in to post a comment.