Key takeaways
- Fail2ban watches logs and bans IPs after repeated failures.
- Configure it in jail.local, never jail.conf.
- Add your own IP to ignoreip so you can't lock yourself out.
Install
sudo apt install fail2ban
Configure
Create /etc/fail2ban/jail.local:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
ignoreip = 127.0.0.1/8 ::1 198.51.100.7 # add your own IP
[sshd]
enabled = true
backend = systemd
Restart and check:
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
Useful commands
sudo fail2ban-client status # list jails
sudo fail2ban-client set sshd unbanip 198.51.100.7
sudo tail -f /var/log/fail2ban.log
Fail2ban has ready-made jails for Nginx, Apache and mail servers too — enable them the same way in jail.local.
Frequently asked questions
Do I still need Fail2ban if I use SSH keys?
Keys stop the attacks from succeeding; Fail2ban reduces the noise and load from them. Both together are a good default.
How do I unban an IP?
Run sudo fail2ban-client set sshd unbanip 198.51.100.7.