How to Use Fail2ban to Secure Your Linux Server

[‘

n

Improving your server security should be one of your top priorities when it comes to managing a linux server. By reviewing your server logs, you may often find different attempts for brute force login, web floods, exploit seeking and many others.

n

With an intrusion prevention software such as fail2ban, you can examine your server logs and add extra iptables rules to block problematic IP addresses.

n

Read Also: How to Install Fail2Ban to Protect SSH on CentOS/RHEL 8

n

This tutorial will show you how to install fail2ban and setup basic configuration to protect your Linux system from brute-force attacks.

n

Requirements

n

Fail2ban is written in python and the only requirement is to have python installed:

n

    n

  • Fail2ban branch 0.9.x requires Python >=2.6 or Python >=3.2
  • n

  • Fail2ban branch 0.8.x requires Python >=2.4
  • n

  • Root access to your system
  • n

  • Optionally, iptables or showewall and sendmail
  • n

n

How to Install Fail2Ban in Linux Systems

n

Installation of fail2ban is relatively easy:

n

Install Fail2Ban on CentOS/RHEL

n

First, update your packages, enable the Epel repository and install fail2ban as shown.

n

# yum updatern# yum install epel-releasern# yum install fail2banrn

n

Install Fail2Ban on Debian/Ubuntu

n

First, update your packages and install fail2ban as shown.

n

# apt-get update && apt-get upgrade -yrn# apt-get install fail2banrn

n

Optionally, if you wish to enable mail support (for mail notifications), you can install sendmail.

n

# yum install sendmail                   [On CentOS/RHEL]rn# apt-get install sendmail-bin sendmail  [On Debian/Ubuntu]rn

n

To enable fail2ban and sendmail use the following commands:

n

# systemctl start fail2banrn# systemctl enable fail2banrn# systemctl start sendmailrn# systemctl enable sendmailrn

n

How to Configure Fail2ban in Linux Systems

n

By default, fail2ban uses the .conf files located in /etc/fail2ban/ which are read first. However, those can be overridden by .local files located in the same directory.

n

Thus, the .local file does not need to include all settings from the .conf file, but only the ones you wish to override. Changes should be made in the .local files, not in the .conf. This will prevent overwriting changes when upgrading the fail2ban packet.

n

For the purpose of this tutorial, we will copy the existing fail2ban.conf file to fail2ban.local.

n

# cp /etc/fail2ban/fail2ban.conf /etc/fail2ban/fail2ban.localrn

n

Now you can make the changes in the .local file by using your favorite text editor. The values you can edit are:

n

    n

  • loglevel – this is the level of detail to be logged. Possible options are:n
      n

    • CRITICAL
    • n

    • ERROR
    • n

    • WARNING
    • n

    • NOTICE
    • n

    • INFO
    • n

    • DEBUG
    • n

    n

  • n

  • logtarget – log the actions in a specific file. The default value is /var/log/fail2ban.log. You can, however, change this to:n
      n

    • STDOUT – output any data
    • n

    • STDERR – output any errors
    • n

    • SYSLOG – message-based logging
    • n

    • File – output to a file
    • n

    n

  • n

  • socket – directory in which socket file will be placed.
  • n

  • pidfile – location of the pidfile.
  • n

n

Configure Fail2ban jail.local

n

One of the most important files in fail2ban is jail.conf which defines your jails. This is where you define the services for which fail2ban should be enabled.

n

As we mentioned earlier .conf files can be altered during upgrades, thus you should create a jail.local file where you can apply your modifications.

n

Another way to do this is to simply copy the .conf file with:

n

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

n

In case you are using CentOS or Fedora, you will need to change the backend in jail.local from “auto” to “systemd”.

n

Enable Backend in Fail2ban
Enable Backend in Fail2ban

n

If you are using Ubuntu/Debian, there is no need to make this modification, even though they too use systemd.

n

The jail file will enable SSH by default for Debian and Ubuntu, but not on CentOS. If you wish to enable it, simply change the following line in /etc/fail2ban/jail.local:

n

[sshd]rnenabled = truern

n

Ban and Retry Times

n

You can configure the circumstance after which an IP address is blocked. For that purpose, fail2ban uses bantime, findtime and maxretry.

n

    n

  • bantime – this is the number of seconds that an IP address will remain banned (default 10 min).
  • n

  • findtime – the amount of time between login attempts, before the host is banned. (default 10 min). In other words, if fail2ban is set to block an IP address after 3 failed login attempts, those 3 attempts, must be done within the findtime period (10 mins).
  • n

  • maxretry – number of attempts to be made before a ban is applied. (default 3).
  • n

n

Whitelist IP Address

n

Of course, you will want to whitelist certain IP addresses. To configure such IP addresses open /etc/fail2ban/jail.local with your favorite text editor and uncomment the following line:

n

ignoreip = 127.0.0.1/8  ::1rn

n

Then, you can put the IP addresses that you want to be ignored. IP addresses should be separated from space or comma.

n

Email Alerts

n

If you wish to receive mail alerts upon the event, you will have to configure the following settings in /etc/fail2ban/jail.local:

n

    n

  • destemail – mail address, where you will receive the notification.
  • n

  • Sendername – the sender that you will see when receiving the message.
  • n

  • sender – email address from which fail2ban will send the emails.
  • n

n

The default mta (mail transfer agent) is set to sendmail.

n

In order to receive mail notifications, you will also need to change the “action” setting from:

n

Action = %(action_)srn

n

To one of these:

n

action = %(action_mw)srnaction = %(action_mwl)srn

n

    n

  • %(action_mw)s – will ban the host and send a mail with a whois report.
  • n

  • %(action_mwl)s – will ban the host, provide whois info and all relevant information from the log file.
  • n

n

Additional Fail2ban Jail Configuration

n

So far we have looked at the basic configuration options. If you wish to configure a jail you will need to enable it in the jail.local file. The syntax is pretty simple:

n

[jail_to_enable]rn. . .rnenabled = truern

n

Where you should replace jail_to_enable with the actual jail, for example, “sshd”. In the jail.local file, the following values will be predefined for ssh service:

n

[sshd]rnrnport = sshrnlogpath = %(sshd_log)srn

n

You can enable filter which will help identify if a line in the log is a failed one. The filter value is actually a reference to a file with the service name followed by .conf. For example: /etc/fail2ban/filter.d/sshd.conf.

n

The syntax is:

n

filter = servicern

n

For example:

n

filter = sshdrn

n

You can review the existing filters in the following directory: /etc/fail2ban/filter.d/.

n

Use fail2ban-client

n

Fail2ban comes with a client that can be used for reviewing and changing the current configuration. Since it provides many options, you can go through its manual with:

n

# man fail2ban-client rn

n

Here you will see some of the basic commands you can use. To review the current status of fail2ban or for specific jail, you can use:

n

# fail2ban-client statusrn

n

The result will look similar to this:

n

Check Fail2ban Status
Check Fail2ban Status

n

For individual jail, you can run:

n

# fail2ban-client status sshdrn

n

In the screenshot below, you will see that I have purposely failed multiple logins so fail2ban can block the IP address from which I was trying to connect:

n

Check Fail2ban SSH Status
Check Fail2ban SSH Status

n

Conclusion

n

Fail2ban is an excellent, well-documented intrusion prevention system, that provides extra security to your Linux system. It requires some time to get used to its setup and syntax, but once you familiarize yourself with it, you will feel free to change and extend its rules.

n

‘]