About fail2ban
The fail2ban application monitors server log files for intrusion attempts and other suspicious activity. After a predefined number of failures from a host, fail2ban blocks its IP address automatically for a specific duration. With fail2ban, you can help secure your server against unauthorized access attempts. It is particularly effective in reducing the risk from scripted attacks and botnets.Installing fail2ban
To install the fail2ban package for your Linux distribution:- For Debian and Ubuntu, type the following command:
- For AlmaLinux and Fedora, type the following command:
To download and install the fail2ban package on AlmaLinux and Fedora, you must have the EPEL (Extra Packages for Enterprise Linux) repository enabled for your system. For more information about this repository and how to enable it, please see this article.
Configuring fail2ban
After you install fail2ban, you are ready to configure it. To do this, follow these steps:- Log in to your server using SSH.
- At the command prompt, type the following command:
The jail.conf file contains a basic configuration that you can use as a starting point, but it may be overwritten during updates. Fail2ban uses the separate jail.local file to actually read your configuration settings.
- Open the jail.local file in your preferred text editor.
- Locate the [DEFAULT] section, which contains the following global options:
- ignoreip: This option enables you to specify IP addresses or hostnames that fail2ban will ignore. For example, you could add your home or office IP address so fail2ban does not prevent you from accessing your own server. To specify multiple addresses, separate them with a space. For example:
- bantime: This option defines in seconds how long an IP address or host is banned. The default is 600 seconds (10 minutes).
- maxretry: This option defines the number of failures a host is allowed before it is banned.
- findtime: This option is used together with the maxretry option. If a host exceeds the maxretry setting within the time period specified by the findtime option, it is banned for the length of time specified by the bantime option.
- With fail2ban’s global options configured, you are now ready to enable and disable jails for the specific protocols and services you want to protect. By default, fail2ban monitors SSH login attempts (you can search for the [ssh-iptables] section in the jail.local file to view the specific settings for the SSH jail).
- Save your changes to the jail.local file.
- To restart the fail2ban service and load the new configuration, type the following command:
To display a list of IP addresses currently banned by fail2ban, type the following command:For example, the following line shows an IP address that the SSH jail has banned:
More information
- For general information about fail2ban, please visit http://www.fail2ban.org/wiki/index.php/Main_Page.
- To view the official fail2ban documentation, please visit http://www.fail2ban.org/wiki/index.php/Manual.