Put any Linux machine with SSH open to the internet and within minutes it is being probed. Bots from a rotating pool of addresses try root and admin with a dictionary of common passwords, thousands of times a day. If your server only accepts key-based logins the attacks fail, but they still fill your logs, still consume connections and still hammer the daemon. The tool that turns that noise off is fail2ban, and on Debian and Ubuntu it is one config file away from doing its job.
This is a practical, verifiable walkthrough: install it, write a proper local jail, understand the three numbers that define a ban, watch it work, and know how to undo a mistake when you ban yourself.
What fail2ban actually does
fail2ban watches log files. When it sees a pattern that looks like a failed login repeated too often from the same address, it adds a firewall rule to drop that address for a while. That is the whole idea — it does not patch SSH, it does not change how authentication works, it just stops the conversation for a source that is clearly not a legitimate user.
It is not a replacement for good SSH hygiene. Key-only authentication, no root login and a non-default port all matter more. fail2ban is the layer that handles the traffic that gets past those: automated scanning, credential stuffing and the constant background noise.
Install it
sudo apt update
sudo apt install fail2ban
The package starts a service immediately and ships with jails for common services. But there is one rule to learn before you touch anything: never edit the files in /etc/fail2ban/jail.conf. That file is overwritten on every upgrade. Your changes go in jail.local, which fail2ban reads after jail.conf and which no package update will clobber.
Write a proper local jail
Create /etc/fail2ban/jail.local with your settings. A sensible, modern starting point for a Debian or Ubuntu server:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
backend = systemd
ignoreip = 127.0.0.1/8 ::1
[sshd]
enabled = true
Those four [DEFAULT] values are the heart of it:
| Setting | Meaning |
|---|---|
maxretry |
How many failures are allowed before a ban |
findtime |
The window in which those failures must happen |
bantime |
How long the address is blocked once banned |
ignoreip |
Addresses that are never banned — your own admin IP belongs here |
Read together, the defaults say: five failed logins within ten minutes earns a one-hour ban. Raise bantime to 1d or 1w if you want to be stricter; a bot that fails once will be back, so longer bans pay off. And put your own static IP in ignoreip before you enable anything, or you will lock yourself out of your own server.
The backend = systemd line matters on modern Debian and Ubuntu: instead of tailing /var/log/auth.log, fail2ban reads from the journal, which is where SSH actually writes now. Without it, fail2ban may watch a file that no longer receives the entries.
Turn it on and watch it work
Reload the configuration and check the sshd jail:
sudo systemctl restart fail2ban
sudo fail2ban-client status
sudo fail2ban-client status sshd
The sshd status shows how many failures it has seen, how many addresses are currently banned and which ones. To see the current ban list directly, and to release an address by hand:
sudo fail2ban-client get sshd banip
sudo fail2ban-client set sshd unbanip 203.0.113.7
Captura: la salida de fail2ban-client status sshd, con la lista de IPs actualmente baneadas y los contadores de intentos fallidos.
If the banned list stays empty during a real attack, the jail is watching the wrong log. Confirm the backend and that the service is actually logging to the journal with sudo journalctl -u ssh -n 20.
Test a filter before you trust it
fail2ban decides what a “failure” looks like from a filter — a file of regular expressions. You can run any filter against a log to see what it would match, without waiting for a real attack:
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf
The output lists how many lines matched and how many were ignored. If a log line you know is a failure is not matching, that is your problem to fix before blaming the bans. On a journal-backed system, pass systemd-journal instead of a file path.
Ban the repeat offenders harder
Once fail2ban is working, one upgrade is worth it: the recidive jail. It watches fail2ban’s own log for addresses that get banned again and again, and bans them for a much longer time — a week, a month — at the first sign of recidivism.
[recidive]
enabled = true
logpath = /var/log/fail2ban.log
bantime = 1w
findtime = 1d
maxretry = 3
Add it to the same jail.local, reload, and the persistent scanners that rotate through a pool of addresses stop being a daily nuisance.
Do not ban your own network
The most common fail2ban incident is self-inflicted: you fat-finger your password a few times, or a monitoring system does, and you get locked out. The fixes are the ones already mentioned — a solid ignoreip list — plus a mental rule: always keep a second way in. If the machine is a VPS, use the provider’s console before you rely on SSH. If it is local, keep a keyboard. A one-hour ban is annoying; a permanent lockout from a wrong maxretry on your only account is a long afternoon.
To check whether an address is currently banned, from any other machine, just try to connect; and from the server itself, fail2ban-client status sshd is always the first command to run.
What changes for you
Installing fail2ban does not make your SSH configuration more secure in the cryptographic sense — that is what keys and disabled passwords are for. What it does is remove the background radiation: the endless dictionary attacks that fill your logs and waste your server’s connections. On Debian and Ubuntu it is a ten-minute job: one package, one jail.local, one reload. Do it on every machine you expose, set an ignoreip you trust, and the noise drops to near zero while the legitimate traffic never notices.
Sources
- fail2ban Wiki — GitHub: jails, filters and the recidive configuration.
- jail.conf(5) — Debian manpages: the full list of jail and default settings.
- fail2ban — Ubuntu Server documentation: installation and the local-jail workflow.





Leave a Reply