How to Install fail2ban on CentOS 7

Share on Social Media

In this guide, you will learn, how to install fail2ban on CentOS 7 or other Redhat based Linux distros. #centlinux #linux #cybersecurity

What is fail2ban? :

Brute-force, Dictionary, DOS and DDOS attacks are quite frequent against the common network services like ssh, apache, nginx, mariadb, etc. None of these services provides native defense against these attacks. fail2ban is an intrusion prevention software that protects Linux based servers from Brute-force, DOS, DDOS and Dictionary attacks.

fail2ban is implemented as a service, that continuously monitors log files of services, for failures and then banned the hosts, that caused multiple authentication failures for a specified bantime.

fail2ban uses CentOS 7 firewall rules to ban host machines.

Due to its simplicity and effectiveness, fail2ban is considered as the preferred software to secure Linux services against DOS, DDOS, dictionary and brute-force attacks.

In this article, we will install fail2ban on CentOS 7 and then configure fail2ban to secure ssh, apache, nginx and mariadb servers against brute-force, dictionary, DDOS and DOS attacks.

fail2ban Features:

Some common features of fail2ban are:

  • Client/Server Architecture
  • Multi-threaded
  • Highly configurable using split configuration files
  • Parses log files and look for given patterns
  • Uses Netfilter/Iptables by default but can also use TCP Wrapper (/etc/hosts.deny) and many other firewalls/actions
  • Can handle multiple services at once (sshd, apache, vsftpd, etc)
  • Character set awareness in log files
  • Python3+ support
  • Supports famous Linux distributions including Red Hat Enterprise Linux, CentOS, Ubuntu, SUSE, Debian, etc.

Environment Specification:

We have configured a CentOS 7 virtual machine with following specifications:

  • Hostname – fail2ban-01.example.com
  • IP Address – 192.168.116.171 /24
  • Operating System – CentOS 7.6

Install fail2ban on CentOS 7:

Connect with fail2ban-01.example.com using ssh as root user.

fail2ban is available via EPEL (Extra Packages for Enterprise Linux) yum repository. Therefore, we must install epel-release to enable access to EPEL yum repository.

# yum install -y epel-release

Build cache for yum repositories.

# yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                     | 6.3 kB     00:00
 * base: mirrors.ges.net.pk
 * epel: my.fedora.ipserverone.com
 * extras: mirrors.ges.net.pk
 * updates: centosc6.centos.org
base                                                     | 3.6 kB     00:00
extras                                                   | 3.4 kB     00:00
updates                                                  | 3.4 kB     00:00
epel/x86_64/primary_db                                     | 6.7 MB   01:39
Metadata Cache Created

We are required to install following fail2ban packages.

fail2ban.noarch: Daemon to ban hosts that cause multiple authentication errors
fail2ban-firewalld.noarch: Firewalld support for fail2ban
fail2ban-systemd.noarch: Systemd journal configuration for fail2ban

EPEL yum repository always provides a stable release of fail2ban. However, you can download a latest experimental release from fail2ban official website.

# yum install -y fail2ban fail2ban-firewalld fail2ban-systemd

Since, we are installing fail2ban on a CentOS 7 system, therefore, fail2ban-firewalld and fail2ban-systemd packages are also required for integration with Firewalld and Systemd respectively.

Enable and start fail2ban.service.

# systemctl enable fail2ban.service
Created symlink from /etc/systemd/system/multi-user.target.wants/fail2ban.service to /usr/lib/systemd/system/fail2ban.service.
# systemctl start fail2ban.service

Understand fail2ban Configuration Files:

fail2ban configurations are exist in /etc/fail2ban/ and /etc/fail2ban/jail.d/.

fail2ban reads *.conf files first and then reads *.local files. Therefore, all settings in *.conf files are override by the settings in *.local files.

Thus, it is a best practice to create a custom jail.local file instead of editing the default jail.conf file.

fail2ban provides only a single configuration file /etc/fail2ban/jail.conf with initial configurations. This file contains sample jail configurations of common network services. Therefore, we can simply copy the required section in a jail.local file and enable the jail to apply it.

Copy the default jail.conf file as jail.local.

# cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

Now, we can customize this file to create fail2ban jails for different Linux services. Infact, most fail2ban jails that are related to common Linux services are predefined in this file. Therefore, we are only required to enable the required fail2ban jail.

But, before enabling a fail2ban jail, we are describing some settings in the DEFAULT section. These settings are globally applicable on all fail2ban jails unless override.

ignoreip – It is an IP address, hostname or CIDR mask. fail2ban does not ban a host, if it was listed here.
bantime – It is the time (in seconds) to ban a host.
findtime – It is the time span during which fail2ban must caught maxretry failures to ban a host.
maxretry – It is the number of failures before a host gets banned.

Configure fail2ban to Secure SSH Service:

There are many predefined fail2ban jails for sshd service in jail.local. Therefore, we are enabling only one of them as follows.

# sed -i "/^[sshd]/a\enabled=true" /etc/fail2ban/jail.local
# systemctl restart fail2ban.service

We also restarted fail2ban.service to reload configurations.

Now, try to connect with fail2ban-01.example.com using a ssh client with wrong password. Make 5 failed attempts and the host will be banned by fail2ban.

# ssh root@192.168.116.171
The authenticity of host '192.168.116.171 (192.168.116.171)' can't be established.
ECDSA key fingerprint is SHA256:kzyCimDDwGPsfsuGXxdrcBqlxVQlU8FZTsYrwbPzZHM.
ECDSA key fingerprint is MD5:b4:3f:a2:86:30:7a:b7:d7:b3:b0:10:8f:a3:3e:8a:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.116.171' (ECDSA) to the list of known hosts.
root@192.168.116.171's password:
Permission denied, please try again.
root@192.168.116.171's password:
Permission denied, please try again.
root@192.168.116.171's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
# ssh root@192.168.116.171
root@192.168.116.171's password:
Permission denied, please try again.
root@192.168.116.171's password:

Check sshd jail status by using fail2ban-client command.

# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 1
|  |- Total failed:     6
|  `- Journal matches:  _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   192.168.116.152

The host 192.168.116.152 has been banned by fail2ban due to multiple authentication failures for predefined bantime.

There are more predefined fail2ban jails related to sshd service in jail.local file. One of them is sshd-ddos, which can be used to secure ssh service against DDOS (Distributed Denial of Service) attacks. You should experiment with different jails on your own.

Configure fail2ban to Secure Apache Web Server:

There are various predefined fail2ban jails are available for Apache service. We can enable each of them on demand.

This time we are only enabling one jail apache-auth for demonstration of fail2ban.

# sed -i "/^[apache-auth]/a\enabled=true" /etc/fail2ban/jail.local
# systemctl restart fail2ban.service

We have also configured an Apache website with HTTP basic authentication on this machine. We will use this website to test fail2ban.

Browse URL http://fail2ban-01.example.com using a client’s browser.

The website will ask you for login credentials.

Perform login attempts with wrong credentials 6 times and the host will be banned by fail2ban automatically for the predefined bantime.

You can check status of apache-auth jail as follows.

# fail2ban-client status apache-auth
Status for the jail: apache-auth
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     13
|  `- File list:        /var/log/httpd/error_log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   192.168.116.1

Configure fail2ban to Secure nginx Web Server:

Just like Apache, we also have a fail2ban jail defined for nginx authentication failures. Therefore, we are going to enable and test it.

We have configured a nginx web server with basic http authentication. and now we are using it for demonstration purpose.

# sed -i "/^[nginx-http-auth]/a\enabled=true" /etc/fail2ban/jail.local
# systemctl restart fail2ban.service

Browse URL http://fail2ban-01.example.com using a client’s browser.

The website will ask you for login credentials.

Perform login attempt with wrong credentials 6 times and the host will be banned by fail2ban automatically for the predefined bantime.

# fail2ban-client status nginx-http-auth
Status for the jail: nginx-http-auth
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     5
|  `- File list:        /var/log/nginx/error.log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   192.168.116.1

Configure fail2ban to Secure MariaDB Server:

There are rare cases when we expose MariaDB service port to the network. However, exposing the default port for MariaDB also exposes it to various threats.

Therefore, in such cases, we can use fail2ban to secure MariaDB against brute force, dictionary, DOS and DDOS attacks.

# sed -i "/^[mysqld-auth]/a\enabled=true" /etc/fail2ban/jail.local
# systemctl restart fail2ban.service

MariaDB server default log level is 1 and MariaDB does not record failed login attempts in log file when log level is 1.

In this case, fail2ban does not work because it doesn’t find any login failures in MariaDB log file.

Therefore, we have to increase the log level of MariaDB server, so, it can record failed login attempts in log files.

Use the following commands to increase log level of MariaDB server.

# sed -i "/^[mysqld]/a\log-warnings=2" /etc/my.cnf
# systemctl restart mariadb.service

Perform multiple login attempts with wrong username/password from another host. The fail2ban will ban the host  for the predefined bantime, because of the suspicious activity.

# mysql -u ahmer -p12a -h 192.168.116.171
ERROR 1045 (28000): Access denied for user 'ahmer'@'192.168.116.152' (using password: YES)
# mysql -u root -p12a -h 192.168.116.171
ERROR 1045 (28000): Access denied for user 'root'@'192.168.116.152' (using password: YES)
# mysql -u root -p12a -h 192.168.116.171
ERROR 1045 (28000): Access denied for user 'root'@'192.168.116.152' (using password: YES)
# mysql -u r1 -p12a -h 192.168.116.171
ERROR 1045 (28000): Access denied for user 'r1'@'192.168.116.152' (using password: YES)
# mysql -u r2 -p12a -h 192.168.116.171
ERROR 1045 (28000): Access denied for user 'r2'@'192.168.116.152' (using password: YES)
# mysql -u r4 -p12a -h 192.168.116.171
ERROR 1045 (28000): Access denied for user 'r4'@'192.168.116.152' (using password: YES)
# mysql -u r5 -p12a -h 192.168.116.171
ERROR 1045 (28000): Access denied for user 'r5'@'192.168.116.152' (using password: YES)
# mysql -u r7 -p12a -h 192.168.116.171
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.116.171' (111)

Check the status of mysqld-jail using fail2ban-client command.

# fail2ban-client status mysqld-auth
Status for the jail: mysqld-auth
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     5
|  `- File list:        /var/log/mariadb/mariadb.log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   192.168.116.152

We have successfully install fail2ban on CentOS 7 and configure fail2ban to secure ssh, Apache, Nginx and MariaDB servers against brute-force, dictionary, DOS and DDOS attacks.

Conclusion – Install fail2ban on CentOS 7:

In this guide, you have learned, how to install fail2ban on CentOS 7 or other Redhat based Linux distros.

Scroll to Top