To protect a server from brute force attacks with Fail2ban, install the package, create a jail.local file that enables the sshd jail with sensible maxretry, findtime and bantime values, then restart the service and check it with fail2ban-client status sshd. Fail2ban reads your log files, spots repeated failed logins from one address and bans that address for a while through the firewall.
It is a good extra layer on top of key-only SSH. It also protects web logins and other services when you add jails for them.
Fail2ban has three concepts. A filter is a set of regular expressions that match failure lines in a log file. A jail ties a filter to a log source and a set of limits. An action is what happens on a match, usually adding a firewall rule that drops traffic from the offending IP.
When an address produces maxretry failures within findtime seconds, it is banned for bantime seconds. After the ban expires, the rule is removed.
Check that it runs:
The package ships /etc/fail2ban/jail.conf, which is replaced on upgrades. Put your settings in /etc/fail2ban/jail.local or a file in /etc/fail2ban/jail.d/, which override the defaults:
What these mean:
| Option | Meaning |
|---|---|
bantime | How long an address stays banned |
findtime | The window in which failures are counted |
maxretry | Failures allowed inside the window |
ignoreip | Addresses that are never banned, such as your own |
banaction | The firewall method; ufw fits servers that use UFW, and the default is iptables or nftables based |
backend = systemd | Reads the journal instead of a log file, which suits modern Ubuntu |
Set port to your real SSH port. Always add your own address to ignoreip, so a typo cannot ban you.
Apply the changes:
To remove a ban by hand:
To test a filter against a log, use fail2ban-regex, for example sudo fail2ban-regex /var/log/auth.log sshd.
Fail2ban ships filters for more than SSH. For an Nginx site with HTTP basic auth you can enable:
For WordPress or an app login you write a custom filter that matches your log line for a failed login, put it in /etc/fail2ban/filter.d/, and reference it from a jail. Keep custom filters small and test them with fail2ban-regex before enabling.
For attackers who return after each ban, increase the ban time with the recidive jail, which watches Fail2ban's own log and bans repeat offenders for much longer:
After enabling a jail, prove it works instead of assuming. From a machine whose address is not in ignoreip, make several failed SSH logins with a wrong user name. Then run sudo fail2ban-client status sshd on the server and confirm the address appears in the banned list. Remove the test ban with unbanip afterwards. If nothing is banned, check /var/log/fail2ban.log for errors, confirm backend matches where your system writes authentication messages, and run fail2ban-regex against the real log.
Fail2ban reacts after failures happen, so it cannot replace strong authentication. Attackers using many addresses can stay below the threshold. IPv6 bans work, but a single attacker can rotate through a large address range. Fail2ban also only sees services that write logs it can parse.
For that reason combine it with key-only SSH, a firewall and updates.
Yes, if you mistype your password enough times. Add your static address to ignoreip and keep a console login from your provider as a fallback.
Yes. Set banaction = ufw or use the default action, which adds rules through nftables or iptables beside UFW's rules.
Fail2ban logs to /var/log/fail2ban.log. Each ban and unban is recorded there with the jail name and address.
Not always. Very long bans on shared or changing addresses can block legitimate users, so use the recidive jail for repeat offenders and keep normal bans short.
Install Fail2ban, put your settings in jail.local, enable the sshd jail with the right port, whitelist your own address, and verify with fail2ban-client status sshd. Add jails for other services and use recidive for repeat offenders.