Protect SSH from brute-force attacks with Fail2ban
Install Fail2ban, create a safe jail.local for SSH, check which addresses are banned and unban yourself if you mistype a password too often.
- Intermediate
- Linux VPS
- 2 min read
Fail2ban watches your logs for repeated failed logins and blocks the offending IP address in the firewall for a while. It cuts noise in your logs and makes password guessing far slower.
Fail2ban is a useful extra layer, not a replacement for SSH keys. Keys plus disabled password logins stop guessing completely. See Disable root login and password authentication.
1. Install
Ubuntu and Debian:
sudo apt install -y fail2banAlmaLinux and Rocky Linux get it from EPEL:
sudo dnf install -y epel-release
sudo dnf install -y fail2ban2. Create jail.local
Never edit jail.conf; updates overwrite it. Put your settings in jail.local:
sudo nano /etc/fail2ban/jail.local[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
ignoreip = 127.0.0.1/8 ::1
[sshd]
enabled = trueIf you have a fixed home or office IP, add it to ignoreip (space separated) so you can never ban yourself. If SSH runs on another port, add port = 2222 under [sshd].
Debian 12 only: Debian 12 logs to the systemd journal and has no /var/log/auth.log by default, so tell the jail to read the journal. Add this line under [sshd]:
backend = systemd3. Start it
sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban --no-pager4. Check bans
sudo fail2ban-client status
sudo fail2ban-client status sshdThe second command shows how many failures were seen and which IPs are banned right now.
Unban an address
sudo fail2ban-client set sshd unbanip 198.51.100.7Banned yourself and cannot connect? Use the browser console from Open Control Panel on your service page, or wait for bantime to run out.
Troubleshooting
- Fail2ban will not start: run
sudo fail2ban-client -tto test the configuration. On Debian 12, a missing log file error meansbackend = systemdis not set. - No bans ever appear: check the jail reads the right port and log.
sudo fail2ban-client get sshd logpathandsudo fail2ban-client get sshd journalmatchshow what it watches. - Rules not visible in UFW or firewalld: Fail2ban adds its own chains or ipsets. Check
sudo fail2ban-client status sshdrather than the firewall rule list.
Next steps
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Security
Check the Windows Firewall rule for RDP port 12230
Find and verify the inbound TCP 12230 rule that lets Remote Desktop reach your RunRacks server, recreate it if it was deleted and scope it without locking yourself out.
- Intermediate
- Windows VPS · Admin RDP
- 5 min read
Security
Linux Security: Using aide for File Integrity Monitoring on VPS
AIDE builds a checksum database of your system files and reports anything that changed. This guide installs it, creates a baseline, tunes the rules and schedules daily checks on a Linux VPS.
- Advanced
- Linux VPS
- 6 min read
Security
iptables basics: list, allow, drop and save rules
Learn how iptables chains and rule order work, then build a small ruleset that allows SSH and web traffic, drops everything else and can be edited by line number.
- Intermediate
- Linux VPS
- 6 min read