HowTo : Install and Configure Fail2ban on CentOS/Ubuntu

Fail2Ban helps to protect Linux servers from brute-force attacks.

It scans logs for IP addresses that show the malicious signs and bans that IP addresses for a specified amount of time using firewall.

This article describes how to install and configure Fail2ban on CentOS/Ubuntu and similar Linux distributions.

You'll also learn how to protect SSH server from brute-force attacks and how to manually unban IP address that was banned by Fail2Ban.

Fail2ban Installation

Install Fail2ban on Linux Mint, Ubuntu

Type the following command to install Fail2Ban on Linux Mint or Ubuntu :
$ sudo apt-get install fail2ban

Install Fail2ban on CentOS, RHEL

There is no Fail2Ban package in default CentOS and RHEL repositories, but it can be found in EPEL.

Add EPEL Repository and then install Fail2Ban :
$ sudo yum install fail2ban

Fail2ban Configuration

The default Fail2Ban configuration file is /etc/fail2ban/jail.conf, however it is not recommended to modify it directly.

Instead, we should work with a local copy called jail.local, which will override the jail.conf file.

Make a local copy of Fail2Ban configuration file :

$ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

Edit /etc/fail2ban/jail.local :

$ sudo vi /etc/fail2ban/jail.local

Pay attention to the global options in [DEFAULT] section.

It covers the basic rules that Fail2Ban will follow :

Option Description
ignoreip Don't ban a host which matches an address in this list. Several addresses can be defined using space separator.
bantime Duration (in seconds) for IP to be banned for. Negative number for permanent ban. Default is 10 minutes.
findtime The time window (in seconds) within which Fail2Ban keeps track failed login attempts. Default is 10 minutes.
maxretry The number of failures before a host get banned. Default is 3 attempts.

If you want to set up more nuanced protection, you can override the global options and customize the details in each jail (section with rules for each application).

Protect SSH Server with Fail2Ban

After completing with [DEFAULT] section, go down and update [ssh-iptables] section as below :

[ssh-iptables]

# 'enabled = true' means that SSH protection is on.
# It can be turned off with 'enabled = false'.
enabled  = true
# Use filter : /etc/fail2ban/filter.d/sshd.conf
filter   = sshd
# Action describes the steps that Fail2Ban will take to ban a matching IP address.
action   = iptables[name=SSH, port=ssh, protocol=tcp]
# Send notifications to admin@example.com
           sendmail-whois[name=SSH, dest=admin@example.com, sendername="Fail2Ban"]
# Log location that Fail2Ban will track
logpath  = /var/log/secure
# if during 1 hour
findtime    = 3600
# 5 failed login attempts would be detected
maxretry    = 5
# host will be banned for 24 hours
bantime     = 86400

Fail2ban is not limited to SSH only. Out of the box Fail2Ban comes with filters for various services (SSH, Apache, Asterisk, Postfix, etc), but only [ssh-iptables] jail is activated by default.

Tweaking Fail2Ban Filters

If you wish to tweak the existing filters or add new filters, you can find them in the /etc/fail2ban/filter.d directory.

For example, to modify Fail2Ban filter for OpenSSH, edit the following file :

$ sudo vi /etc/fail2ban/filter.d/sshd.conf

Start Fail2Ban

Fail2ban is already configured to start during the system boot by default.

Don't forget to restart Fail2Ban, each time after making any changes in it's settings :

$ sudo service fail2ban restart

Test Fail2Ban

To test Fail2Ban and to see the rules that Fail2Ban puts in effect, look at iptables :

$ sudo iptables -L

Manually UnBan IP Banned by Fail2Ban

Use the flowing command to manually unban IP address, banned by Fail2Ban :

$ sudo fail2ban-client set JAIL unbanip IP

Unban IP 192.168.1.101, that was banned according to [ssh-iptables] jail :

$ sudo fail2ban-client set ssh-iptables unbanip 192.168.1.101