Fail2ban Basics: Auto-Block Brute-Force Attacks on Your Linux Server
Put any Linux server on the internet and within minutes, bots start hammering its SSH login — trying password after password, thousands of times a day. You cannot watch the logs forever. Fail2ban watches them for you, and bans the attackers automatically. It is free, simple, and one of the best first defenses for any server you own.
What Fail2ban Does
Fail2ban reads your server's log files — SSH, Nginx, Apache and more — and looks for patterns of failed logins. When an IP address fails too many times in a short window, Fail2ban adds a firewall rule that blocks that IP for a set time. The attacker is locked out before they ever guess right. Legitimate users who mistype a password once or twice are never affected.
Install It
On Debian or Ubuntu:
sudo apt update
sudo apt install fail2ban
On RHEL, Rocky or AlmaLinux, enable the EPEL repository first, then sudo dnf install fail2ban. Start the service and enable it at boot:
sudo systemctl enable --now fail2ban
Configure the SSH Jail
Fail2ban's rules are called jails. Never edit the default jail.conf — your changes get overwritten on updates. Create jail.local instead:
sudo nano /etc/fail2ban/jail.local
Add this:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
[sshd]
enabled = true
port = ssh
This means: 5 failed SSH logins within 10 minutes earns a 1-hour ban. Restart Fail2ban with sudo systemctl restart fail2ban and check it is running with sudo fail2ban-client status sshd.
Check Who Got Banned
sudo fail2ban-client status sshd
You will see the currently banned IPs and the total ban count. To unban an IP (for example, your own, after too many typos):
sudo fail2ban-client set sshd unbanip 203.0.113.45
Make It Stronger
- Ban longer for repeat offenders. Add
bantime.increment = trueto the DEFAULT section and repeat attackers get exponentially longer bans. - Protect your web server too. Enable the
nginx-http-authorapache-authjails the same way you enabledsshd. - Whitelist your own IP with
ignoreip = 127.0.0.1/8 ::1 203.0.113.10so you can never lock yourself out. - Get email alerts by setting
destemailandaction = %(action_mwl)sif your server can send mail.
What Fail2ban Does Not Do
It does not fix weak passwords. If your SSH password is "password123", a patient attacker who stays under the limit will eventually get in. Pair Fail2ban with the real fixes: disable password logins and use SSH keys, disable root login, and move SSH off port 22 if you like. Fail2ban is a bouncer at the door — strong locks still matter more.
Install it on every server you own. Ten minutes of setup, and the bots start hitting a wall instead of your login prompt.



Comments
Post a Comment