Fail2ban SSH Setup: Stop Brute-Force Attacks on Your VPS
Every public VPS gets hammered by SSH login bots within hours. Here is a working fail2ban SSH setup, plus the lockout mistakes to avoid.

Infrastructure & Support Team
On this page
A fail2ban SSH setup takes about five minutes: install the package, drop a small jail.local file in /etc/fail2ban/, enable the sshd jail and restart the service. After that, any IP that fails to log in too many times within a time window gets blocked at the firewall for a while. This guide gives you a working configuration for Ubuntu and Debian, explains each setting, and shows how to test it without locking yourself out.
If you have ever run journalctl -u ssh or read /var/log/auth.log on a fresh VPS, you know why this matters. Within hours of getting a public IP, bots start trying root, admin, ubuntu and thousands of other usernames. Most of them will never get in, but they fill your logs, burn a little CPU and wait for the one weak password.
What fail2ban actually does#
Fail2ban reads log files (or the systemd journal), looks for patterns that mean "failed login", and adds a temporary firewall rule against the offending address. The project describes it as scanning logs such as /var/log/auth.log and banning IPs with too many failed logins by updating firewall rules for a configurable time (Fail2Ban on GitHub).
It is a reaction tool, not a lock. It does not make a weak password strong. Treat it as the second layer after the basics in our security hardening guide: key-based login, no root password login and a firewall.
Install fail2ban on Ubuntu or Debian#
On a fresh VPS, connect over SSH (see connecting through SSH if you need a refresher) and run:
sudo apt update
sudo apt install fail2ban
sudo systemctl enable --now fail2ban
fail2ban-client versionUbuntu and Debian both ship it in the default repositories. If you are still choosing a distribution, our comparison of Ubuntu vs Debian for a VPS covers the trade-offs. The steps below work on both.
Write jail.local, not jail.conf#
Fail2ban's main file is /etc/fail2ban/jail.conf, but you should not edit it. The manual page says *.conf files should remain unchanged to ease upgrades, and that your changes belong in .local files where you specify only the settings you want to change (jail.conf(5)). So create /etc/fail2ban/jail.local:
[DEFAULT]
# Never ban yourself: add your home/office IP and your VPN
ignoreip = 127.0.0.1/8 ::1 203.0.113.10
bantime = 1h
findtime = 10m
maxretry = 5
[sshd]
enabled = true
port = sshReplace 203.0.113.10 with your own public IP. The ignoreip option accepts addresses, CIDR ranges and DNS names according to the manual. Many Indian home and mobile connections use dynamic or shared (CGNAT) addresses, so do not rely on a single IP being yours tomorrow. Always keep a second way in, such as the web console or VNC from your provider's panel.
Then apply it:
sudo systemctl restart fail2ban
sudo fail2ban-client status sshdWhat bantime, findtime and maxretry mean#
These three settings do most of the work:
- maxretry is how many failures within the window trigger a ban. 5 is a sane start.
- findtime is the window in which those failures are counted. With
10m, five failures in ten minutes means a ban. - bantime is how long the ban lasts.
1his gentle; some admins use a day. Setting it to-1bans permanently, which tends to bloat your firewall rules and bite you when an address is reassigned.
The manual defines them the same way: bantime is the effective ban duration, findtime is the time window in which failures count, and maxretry is the number of failures required within that window.
Escalating bans for repeat offenders#
Recent fail2ban releases support increasing the ban each time the same IP returns, via bantime.increment. Check that your installed version has it by looking for bantime.increment in /etc/fail2ban/jail.conf, then add something like:
[DEFAULT]
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 5wIf the keys are missing from your version's jail.conf, skip this and upgrade or use a longer flat bantime instead.
Check that it works#
Look at the jail status:
sudo fail2ban-client status sshdYou will see the number of failed attempts, the banned IP list and the log source it is watching. The list of commands is in the fail2ban-client manual.
To prove a ban actually happens, use a throwaway second machine or a phone hotspot. Try to log in with a wrong password six times, then watch status sshd list that IP. Do not test from the connection you are administering the server on.
Reading the logs#
Fail2ban logs its own decisions to /var/log/fail2ban.log. Search for Ban and Unban lines:
sudo grep -E "Ban|Unban" /var/log/fail2ban.log | tailHow to unban an IP#
If you do lock yourself out, use another route (web console, or a second IP you whitelisted) and run:
sudo fail2ban-client set sshd unbanip 198.51.100.7Then add that address to ignoreip and reload with sudo fail2ban-client reload, which reloads the configuration without restarting the server.
Common mistakes with fail2ban and SSH#
Wrong log backend. On newer Debian and Ubuntu releases, SSH logins may live only in the systemd journal rather than /var/log/auth.log. The backend option defaults to auto, which tries pyinotify, gamin, systemd and then polling. If status sshd shows zero matched failures while your logs clearly show attacks, set backend = systemd in the [sshd] section and restart.
Changed SSH port, unchanged jail. If you move SSH to port 2222, set port = 2222 in the [sshd] jail and update your firewall to match. Our firewall configuration docs cover opening ports properly.
Docker and firewall surprises. Bans are only as good as the firewall path they use. If you run containers, read why Docker's published ports bypass UFW before assuming a ban covers every service.
Leaving password login on. Fail2ban reduces the odds; it does not remove them. Once your key works, set PasswordAuthentication no in /etc/ssh/sshd_config, test a second session, then reload SSH.
Where fail2ban fits in a VPS security routine#
A reasonable order for a new server is: create a non-root user, add your SSH key, disable password login, enable the firewall, install fail2ban, enable automatic security updates and set up backups. If you are starting from scratch, a LordCloud VPS gives you full root access and a clean Linux install to practise on, and fail2ban works the same way on it as on any Ubuntu or Debian box.
For real-time visibility, pair it with basic server monitoring so a sudden spike in bans or CPU does not go unnoticed.
Frequently asked questions#
Is fail2ban enough to secure SSH?#
No. It is a useful layer, but keys-only login, a firewall and regular updates matter more. Fail2ban mostly reduces noise and blocks persistent guessers.
Will fail2ban slow down my VPS?#
On a normal VPS the overhead is small: it tails a log and occasionally changes a firewall rule. If you add many jails on a very small plan, watch memory use with your monitoring tool.
Can fail2ban protect other services?#
Yes. It ships with filters for common services, and you can enable more jails the same way you enabled sshd. Start with SSH, confirm it works, then add others one at a time.
Frequently asked questions
How do I set up fail2ban for SSH on Ubuntu?
Install the fail2ban package, create /etc/fail2ban/jail.local with an [sshd] section that sets enabled = true, maxretry, findtime and bantime, then restart the fail2ban service and check it with fail2ban-client status sshd.
Does fail2ban stop SSH brute-force attacks completely?
No. It slows them down by banning IPs after repeated failures. SSH keys with password login disabled is the stronger fix, and fail2ban is a useful second layer that also cuts log noise.
How do I unban an IP in fail2ban?
Run fail2ban-client set sshd unbanip followed by the address. Add your own IP to ignoreip so you are never banned in the first place.
Should I change the SSH port if I use fail2ban?
It is optional. A non-standard port reduces bot noise, but it is not security by itself. If you change it, update the port setting in your sshd jail and your firewall rules.
References
Drafted with AI assistance and checked against LordCloud's published product information. Spotted a mistake?Suggest an edit.



