Skip to content

Tutorials Security Intermediate1 min read

How to Protect SSH with Fail2ban

Install and configure Fail2ban on Ubuntu to automatically ban IP addresses that repeatedly fail SSH logins, and learn how to unban yourself.

By ZSOLNET Editorial Team Updated

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.

Share X LinkedIn Facebook

Log in to your account

Manage your domains, hosting and invoices in one place.

Secure connection straight to the ZSOLNET client area.

No account yet?