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.
Note: 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.7Tip: Banned 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.
