Protect SSH from brute-force attacks with Fail2ban
Updated 27 Nov 2026
Every server on the internet gets thousands of password guesses a day. Fail2ban watches your SSH log and bans any IP that fails to sign in too many times. It also keeps your logs readable. This guide sets it up with stronger settings than the defaults.
Run these commands as a user with sudo on Ubuntu.
1. Install it
sudo apt update
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
On Ubuntu, the SSH protection (the sshd jail) is on straight away.
2. Add your own settings
Never edit jail.conf; updates replace it. Put your settings in jail.local:
sudo nano /etc/fail2ban/jail.local
Add:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
bantime.increment = true
ignoreip = 127.0.0.1/8 ::1
[sshd]
enabled = true
What these mean:
| Setting | Meaning |
|---|---|
maxretry = 5 |
Ban after 5 failed sign-ins... |
findtime = 10m |
...within 10 minutes |
bantime = 1h |
Ban for 1 hour |
bantime.increment |
Ban for longer each time the same IP comes back |
ignoreip |
Addresses that are never banned |
Add your office's fixed IP to
ignoreip, separated by a space, for exampleignoreip = 127.0.0.1/8 ::1 196.43.10.20. Then a few wrong attempts won't lock you out. Only add addresses you control.
If you changed SSH to another port, add port = 2222 (your port) under [sshd].
Save with Ctrl+O, Enter, Ctrl+X, then check and restart:
sudo fail2ban-client -t
sudo systemctl restart fail2ban
3. Check it's working
sudo fail2ban-client status sshd
You'll see the number of failed attempts and a Banned IP list. On a busy server, it fills within hours.
See recent bans in the log:
sudo tail -n 30 /var/log/fail2ban.log
Unban an IP
If you or a colleague got banned, sign in from another connection (for example your phone's hotspot) and run:
sudo fail2ban-client set sshd unbanip 196.43.10.20
Is Fail2ban enough?
Fail2ban slows attackers down, but it can't stop a guess that's right first time. The real protection is SSH keys with passwords turned off. With that in place, guesses can't succeed, and Fail2ban just keeps the noise down. See Create a sudo user and sign in with an SSH key.
Protect other services
Fail2ban has ready-made jails for Nginx, Apache, Postfix and more. List what's running:
sudo fail2ban-client status
To add one, put a section such as [nginx-http-auth] with enabled = true in jail.local and restart Fail2ban. Only turn on jails for software you actually run. A jail whose log file doesn't exist stops Fail2ban from starting.
Need a server? Order a Cloud VPS and get root access within minutes. For help, open a ticket and choose Technical.