Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Install Fail2ban for SSH Security: Step-by-Step

· 6 min read · Home Security

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Fail2ban is a free intrusion-prevention tool that monitors server logs for repeated failed login attempts and temporarily bans the offending IP addresses using the system firewall. By default, it works with SSH, which is the most common attack vector on Linux servers. This guide will walk you through installing and configuring fail2ban to significantly reduce brute-force SSH attacks. You will need a Linux server (Ubuntu, Debian, CentOS, or similar) with root or sudo access, and an SSH connection to it.

What You'll Need

  • A Linux server (Ubuntu/Debian or CentOS/RHEL/Fedora) with SSH access.
  • Sudo or root privileges on that server.
  • Basic familiarity with the command line.

Step-by-Step

  1. Update your system packages. Before installing anything, run sudo apt update && sudo apt upgrade on Debian/Ubuntu, or sudo yum update on CentOS/RHEL/Fedora. This ensures you have the latest package lists and security patches.
  2. Install fail2ban. On Debian/Ubuntu: sudo apt install fail2ban. On CentOS/RHEL/Fedora: sudo yum install fail2ban or sudo dnf install fail2ban (for Fedora). The package manager will pull in the necessary dependencies.
  3. Start and enable the service. After installation, start fail2ban and enable it to run at boot: sudo systemctl start fail2ban and sudo systemctl enable fail2ban. This ensures it runs immediately and after reboots.
  4. Create a local configuration file. Fail2ban uses two main config files: /etc/fail2ban/jail.conf (default, do not edit) and /etc/fail2ban/jail.local (custom, overrides). Create the local file: sudo nano /etc/fail2ban/jail.local (or use your preferred editor). This file will hold your custom settings.
  5. Configure the SSH jail. In the jail.local file, add the following basic SSH jail configuration. This tells fail2ban to monitor SSH logs and ban IPs after 5 failed attempts for 10 minutes, with a ban lasting 1 hour. You can adjust these values later. The enabled = true line activates the jail.
    [sshd]
    enabled = true
    port = ssh
    filter = sshd
    logpath = /var/log/auth.log
    maxretry = 5
    bantime = 3600
    findtime = 600
    For CentOS/RHEL/Fedora, the log path is usually /var/log/secure instead of /var/log/auth.log. Adjust the logpath accordingly.
  6. Set a whitelist (optional but recommended). To avoid locking yourself out, add your own IP address to the ignore list. In the same jail.local, add a line like ignoreip = 127.0.0.1/8 ::1 192.168.1.100 (replace with your actual IP). You can add multiple IPs or CIDR ranges separated by spaces. This is crucial if you have a dynamic IP that might change, but it's still safer than being locked out.
  7. Save and close the file. In nano, press Ctrl+O to save, then Ctrl+X to exit. In vim, use :wq.
  8. Restart fail2ban to apply changes. Run sudo systemctl restart fail2ban. This reloads the configuration.
  9. Verify that fail2ban is running. Check the status with sudo systemctl status fail2ban or sudo fail2ban-client status. You should see the sshd jail listed. To see details of the sshd jail, run sudo fail2ban-client status sshd. It will show the number of currently banned IPs and the filter used.
  10. Test the configuration (optional but recommended). From a different machine, try to SSH into your server with the wrong password several times (more than maxretry). After the threshold, you should see the IP get banned in the fail2ban status output. Then try to connect again – the connection should be refused or timeout.

Common Mistakes

  • Editing jail.conf directly. This file is overwritten on package updates. Always use jail.local for customizations.
  • Wrong log path. Using /var/log/auth.log on CentOS/RHEL will not work because the log is at /var/log/secure. Check your distribution's default.
  • Not whitelisting your own IP. If you have a static IP, forgetting to add it to ignoreip can lock you out after repeated failed attempts (e.g., if you mistype your password).
  • Setting bantime too high. If you set bantime to a week or more, a temporary mistake could lock you out for a long time. Start with a shorter ban (e.g., 3600 seconds) and increase later if needed.
  • Forgetting to restart the service. Changes to jail.local only take effect after a restart.

Tips

  • Use a longer bantime for repeat offenders. Fail2ban supports exponential backoff by setting bantime.increment = true in the jail. This increases the ban time for each subsequent offense.
  • Enable email notifications. Add destemail = your@email.com and action = %(action_mw)s in the jail to receive emails when an IP is banned. This requires a mail transfer agent (MTA) like sendmail or postfix to be installed.
  • Monitor logs regularly. Check the fail2ban log at /var/log/fail2ban.log to see what it's doing. This helps you fine-tune settings.
  • Consider blocking on other services. Fail2ban can also protect web servers (Apache/Nginx) and mail servers. You can enable other jails by adding similar sections in jail.local.
  • Keep fail2ban updated. Regularly run package updates to get the latest filters and security fixes.

Troubleshooting

If fail2ban is not working as expected, follow these steps in order:

  1. Check service status. Run sudo systemctl status fail2ban. If it's not active, start it with sudo systemctl start fail2ban and check for errors in the output.
  2. Inspect the fail2ban log. Look at /var/log/fail2ban.log for error messages. Common issues include invalid configuration syntax or incorrect log paths.
  3. Verify the log path is correct. Use sudo tail -f /var/log/auth.log (or secure) to see if SSH login attempts are being logged there. If not, your SSH may be logging elsewhere (e.g., journald). In that case, you can set backend = systemd in the jail and change logpath to systemd (or omit it) to read from the systemd journal.
  4. Check firewall rules. Fail2ban uses iptables or nftables to add bans. Ensure your firewall is active and that fail2ban has permission to modify it. On Ubuntu with UFW, you may need to install fail2ban and it will automatically integrate, but if not, you can set banaction = ufw in the jail.
  5. Test with a manual ban. Use sudo fail2ban-client set sshd banip 192.168.1.1 to manually ban an IP. If that works, the issue is with detection, not banning. Then check the filter regex by running sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf to see if it matches.
  6. If all else fails, unban and reinstall. To unban an IP, use sudo fail2ban-client set sshd unbanip 192.168.1.1. If the configuration is corrupted, you can remove and reinstall fail2ban: sudo apt remove --purge fail2ban (or yum) and then reinstall. This will delete your custom settings, so back up jail.local first.

Frequently Asked Questions

Will fail2ban lock me out of my own server?

It can, if you have a dynamic IP and you don't whitelist your current IP in ignoreip. Always add your IP to the ignore list to prevent accidental lockouts.

How do I unban an IP address?

Use the command sudo fail2ban-client set sshd unbanip <IP_ADDRESS>. Replace <IP_ADDRESS> with the actual IP. This removes the ban immediately.

Can I use fail2ban with SSH on a non-standard port?

Yes. In the jail configuration, set port = 2222 (or your custom port) instead of ssh. Fail2ban will then ban based on that port.

Does fail2ban work with IPv6?

Yes, fail2ban supports IPv6 addresses. By default, it handles both IPv4 and IPv6. Ensure your firewall rules support IPv6 (e.g., ip6tables) if you want to ban IPv6 addresses.

How often should I check the fail2ban logs?

It's a good practice to review the logs weekly to see if any IPs are being repeatedly banned, which might indicate a persistent threat. You can also set up email notifications to be alerted in real-time.

Is fail2ban enough to secure SSH?

Fail2ban is a strong deterrent, but it's not a substitute for other security measures. Use key-based authentication, disable root login, and consider changing the SSH port to reduce automated attacks.