Fail2ban for SSH
Fail2ban helps protect SSH by monitoring auth logs and banning IPs after repeated failed login attempts.
Install
Ubuntu/Debian:
sudo apt updatesudo apt install -y fail2banRHEL/CentOS:
sudo dnf install -y fail2banEnable and start
sudo systemctl enable fail2bansudo systemctl start fail2bansudo systemctl status fail2banConfigure SSH jail (recommended: use jail.local)
Create or edit /etc/fail2ban/jail.local:
[sshd]enabled = trueport = sshlogpath = /var/log/auth.logbackend = systemdmaxretry = 5findtime = 10mbantime = 1hFor RHEL-like systems, logpath is usually /var/log/secure.
Apply changes
sudo systemctl restart fail2banCheck status and banned IPs
sudo fail2ban-client statussudo fail2ban-client status sshdUnban an IP
sudo fail2ban-client set sshd unbanip <IP_ADDRESS>Useful notes
- Keep your own admin/VPN IP in
ignoreipto avoid locking yourself out. - Fail2ban is a layer, not a replacement: also use SSH keys and disable password login when possible.
- Test with
maxretryandfindtimevalues that match your server’s risk profile.