Check your web server or SSH logs. Every minute, automated scripts try password combinations to break in. Letting these bots try indefinitely wastes bandwidth and creates an unnecessary risk.
Fail2Ban addresses the problem by monitoring log files in real time, detecting repeated failed logins and updating firewall rules to block attackers before they find a way in.
Here is how to configure Fail2Ban to automate this protection and block intruders without constant supervision. For an additional collaborative layer of protection, CrowdSec can work alongside Fail2Ban to block malicious IP addresses globally.
In this article
Install and set up Fail2Ban on Linux
Start by installing Fail2Ban from your Linux distribution’s official repositories. Use the package manager that comes with your system.
On Debian or Ubuntu, run:
sudo apt install fail2ban
On Fedora or RHEL, use:
sudo dnf install fail2ban
After installation, start the service and enable it to launch automatically at boot. On a system that uses systemd, run:
sudo systemctl enable --now fail2ban
Check that the daemon is running:
sudo systemctl status fail2ban
If the status is active and running, Fail2Ban is already monitoring its default log files and protecting your server.
Configure Fail2Ban jails
Jail configuration is an important part of protecting your infrastructure. To preserve your settings across software updates, do not edit the original /etc/fail2ban/jail.conf or /etc/fail2ban/fail2ban.conf files. Make your changes in files with the .local extension or create dedicated files in /etc/fail2ban/jail.d/.
Set global Fail2Ban options
To define rules that apply to all services, create /etc/fail2ban/jail.d/jail.local and open it in your preferred text editor:
sudo nano /etc/fail2ban/jail.d/jail.local
Add the following configuration:
[DEFAULT]
ignoreip = 127.0.0.1 192.168.1.0/24
findtime = 10m
bantime = 24h
maxretry = 3
- ignoreip: IP addresses or networks that should never be banned. Add your own IP address to avoid locking yourself out.
- findtime: how long Fail2Ban checks logs for failed attempts; here, 10 minutes.
- maxretry: the number of failed attempts allowed before an action is taken; here, three.
- bantime: how long an IP address remains banned; here, 24 hours.
Enable Fail2Ban protection for SSH
Configure a jail for a specific service in the same /etc/fail2ban/jail.d/jail.local file. This example protects SSH:
[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
maxretry = 5
Variables such as %(sshd_log)s make the configuration portable and let the system adapt to your Linux distribution. After saving the changes, restart Fail2Ban to apply them:
sudo systemctl restart fail2ban
Use fail2ban-client to confirm that the jail is active and ready to filter connections.
Manage and monitor banned addresses
Once Fail2Ban is running, you can inspect logs and manage the list of banned addresses to keep control of your server’s security.
To check the status of the SSH jail and see which IP addresses are currently banned, run:
sudo fail2ban-client status sshd
If you or a legitimate user is blocked by mistake, remove the IP address from the ban list. Replace the example address with the one you want to unblock:
sudo fail2ban-client set sshd unbanip 192.168.1.100
You may also identify a malicious source before it reaches the retry limit. Ban an IP address immediately with:
sudo fail2ban-client set sshd banip 192.168.1.100
To monitor blocking events as they happen, follow the dedicated log file:
tail -f /var/log/fail2ban.log
This routine helps you respond quickly to intrusion attempts while keeping legitimate access under control.
Go further with Fail2Ban
You can customize default actions, for example to receive an email whenever an address is banned. Set the action directive in your .local file to %(action_mwl)s. Fail2Ban will then send a message with details of the ban and the related logs.
Review your rules regularly. To test a filter without waiting for a real attack, use fail2ban-regex. It checks your log files to confirm that the regular expressions detect intrusion attempts:
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf
This utility helps validate your settings and avoid false positives. Always restart Fail2Ban after changing a filter or action so the changes take effect.
We allow some readers to view our articles for free with an ad blocker. However, their number is currently too high for us to keep this access available to everyone.
You can disable your ad blocker to continue reading immediately, subscribe to enjoy all our content without ads, or come back a little later when the pressure has eased.
Access will be restored automatically as soon as the situation allows.