How to Install and Use Fail2ban (Stop SSH Brute-Force)
If your SSH logs are full of failed login attempts, fail2ban is the answer. It watches your logs and automatically bans any IP that fails too many times — turning a constant stream of attacks into background noise. Here is how to set it up.
Fail2ban scans log files for repeated failures (such as bad SSH logins) and adds a temporary firewall ban for the offending IP. It is the standard first line against brute-force bots.
Step 1: install it
On Debian/Ubuntu and RHEL-family systems it is one package:
# Debian / Ubuntu
sudo apt install fail2ban
# RHEL / AlmaLinux / Rocky
sudo dnf install fail2ban
Step 2: create a local config
Never edit the shipped config directly — it gets overwritten on updates. Create a jail.local that overrides it:
sudo nano /etc/fail2ban/jail.local
A sensible SSH jail looks like this:
[sshd]
enabled = true
port = ssh
maxretry = 4
findtime = 10m
bantime = 1h
That bans an IP for one hour after four failed attempts within ten minutes. Raise bantime for repeat offenders.
Step 3: start and enable it
sudo systemctl enable --now fail2ban
Step 4: check status and bans
# overall status
sudo fail2ban-client status
# the SSH jail (shows banned IPs)
sudo fail2ban-client status sshd
Unbanning an IP (e.g. yourself)
Banned your own IP by mistake? Unban it:
sudo fail2ban-client set sshd unbanip 1.2.3.4
Fail2ban will not stop a targeted attacker, but it makes your server invisible to the endless background noise of credential-guessing bots.
Combine it with key-only SSH and a firewall for proper protection — all in the VPS hardening checklist.
Whitelist your own IP so you never lock yourself out
The most common fail2ban mishap is banning your own address after a few fat-fingered logins. Add your static IP (and any office/home ranges) to the ignoreip setting in jail.local so fail2ban never bans you, then reload the service.
Protecting more than just SSH
Fail2ban ships with jails for far more than SSH — web logins, mail (Postfix/Dovecot), FTP and WordPress with the right filter. Enable the jails that match the services you actually run, so brute-force protection covers your whole stack, not only the shell.
How to keep fail2ban effective
- Put all changes in jail.local, never jail.conf, so updates don't wipe them.
- Whitelist your own trusted IPs with ignoreip.
- Increase bantime for repeat offenders (or use recidive jail).
- Pair it with key-only SSH so brute-force is impossible even before bans kick in.
Related guides
Fail2ban is one layer. Combine it with hardened SSH, a UFW firewall, and the wider VPS hardening checklist. If you're seeing an SSH cutoff rather than a ban, see too many authentication failures.
Less log spam, more uptime
Run fail2ban on a protected ESAGAMES VPS — brute-force noise filtered, attacks absorbed at the network.
Frequently asked questions
Does fail2ban replace strong SSH security?
No — it complements it. The strongest setup is key-only SSH (so brute-force is impossible anyway) plus fail2ban to keep your logs clean and ban noisy IPs.
Where does fail2ban store its config?
The defaults live in /etc/fail2ban/jail.conf — but put your changes in /etc/fail2ban/jail.local, which overrides the defaults and survives package updates.
How do I unban an IP?
Run: sudo fail2ban-client set
How do I stop fail2ban from banning my own IP?
Add your address to the ignoreip line in jail.local (space-separated, and you can include CIDR ranges), then reload fail2ban. Whitelisted IPs are never banned no matter how many times they fail.
Can fail2ban protect services other than SSH?
Yes — it includes jails and filters for web servers, mail (Postfix/Dovecot), FTP and even WordPress logins. Enable the jails matching the services you run so protection covers your whole stack.
How long should I ban an IP for?
An hour is a sane default for casual bots; raise it (hours or days) for repeat offenders, or enable the recidive jail to escalate bans for IPs that keep coming back after their first ban expires.
Related articles
Fix SSH "Connection refused" / "Connection timed out"
SSH "connection refused" vs "timed out" — what each means and the exact steps to fix them.
Read fix Linux & VPSFix "Permission denied" on Linux (chmod & chown Explained)
"Permission denied" on a script or file? Understand chmod/chown and fix it the right way.
Read fix Linux & VPSHow to Check CPU, RAM & Disk Usage on a Linux Server
The essential commands to check CPU, RAM and disk on Linux — and find what's eating them.
Read fix