Janna Theme License is not validated, go to the theme options page to validate the license, you need a single license for each domain name.

How to Protect Your Server with Fail2Ban in Linux

Securing your server is a top priority for any business or organization. Fail2Ban is one of the most important tools in a system administrator's toolkit. This guide explains what Fail2Ban is and how you can protect your server from attacks using Fail2Ban on your Linux machine.

install-fail2ban-linux-00-featured-image-800x400.jpg How to Protect Your Server with Fail2Ban in Linux
What is Fail2Ban?

Fail2Ban is an intrusion prevention tool that monitors log files and blocks IP addresses that show signs of malicious activity. It does this by creating "filters" that match certain patterns in log files and taking actions, such as blocking the offending IP address.

Why use Fail2Ban in Linux?

There are several reasons to use Fail2Ban in Linux. They can:

  • Saves you time by automatically blocking malicious IP addresses.
  • Helps secure your server by reducing the chances of a successful attack.
  • It gives you peace of mind knowing that your server is monitored and protected.

Installing Fail2Ban on Linux

By default, Fail2Ban is available in the Ubuntu repositories. Install it using apt.

sudo add-apt-repository universe sudo apt update && sudo apt install fail2ban -y

install-fail2ban-linux-01-installing-fail2ban.png How to Protect Your Server with Fail2Ban in Linux

Fail2Ban will set its service to run in the background, but systemd will disable it by default. You can start and enable it with the following commands:

sudo systemctl start fail2ban sudo systemctl enable fail2ban

Verify that Fail2Ban is running using the following command:

sudo systemctl status fail2ban

You will see an output similar to the image below.

install-fail2ban-linux-02-systemd-service-status.png How to Protect Your Server with Fail2Ban in Linux

Configuring Fail2Ban in Linux

The /etc/fail2ban directory contains the configuration files for Fail2Ban. By default, Fail2Ban comes with a “jail.conf” file containing settings that will be applied to all services.

install-fail2ban-linux-03-program-etc-directory.png How to Protect Your Server with Fail2Ban in Linux

However, it is good practice to create a local “jail.local” file and override the settings in “jail.conf”, as any changes you make to “jail.conf” will be lost whenever the software is updated.

You can create your own “jail.local” file using the following commands:

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

Open the jail.local file for editing:

sudo nano /etc/fail2ban/jail.local

install-fail2ban-linux-04-jail-local-file.png How to Protect Your Server with Fail2Ban in Linux

Good to know: Learn How to enable passwordless SSH logins on Linux.

Explore jail.local file

You'll see a similar output as shown below, and you may be overwhelmed by the number of options available. But don't worry, we'll walk you through the most important ones.

install-fail2ban-linux-05-jail-local-crop-comment.png How to Secure Your Server with Fail2Ban in Linux

You will see the icon "#" at the beginning of the line, indicating that it is a comment. Fail2Ban uses this to explain what a particular setting does. You can enable some settings here by removing this code.

Section contains “[DEFAULT]” Options applied to all jails. This is the general configuration for Fail2Ban. The following screenshot shows an example.

Also read:  Surprising facts I discovered after using openSUSE Linux for the first time

install-fail2ban-linux-06-jail-local-bantime-crop.png How to Protect Your Server with Fail2Ban in Linux

There are also other sections beginning with the service name. This prison applies to a specific service at the top of the world's prisons.

For example, there is a section for prison. “sshd”. This section contains options for the sshd service.

install-fail2ban-linux-07-jail-local-sshd-crop.png How to Secure Your Server with Fail2Ban in Linux

Tip: You can also learn more about server hardening by: Securing your SSH server.

Enable Autoban feature

Go to Settings bantime = 1h"And remove the code "#" At the beginning of the line to enable it. This line specifies how long Fail2Ban will take to disable the IP address. The default unit is one hour. You can also use other units, such as minutes. (M) Or days (D) or even weeks (w).

install-fail2ban-linux-08-enable-bantime-length.png How to Protect Your Server with Fail2Ban in Linux

You can increase or decrease this value as you see fit. For example, you could change this value to 30 meters to reduce the block duration to 30 minutes.

install-fail2ban-linux-09-reduce-bantime-length.png How to Protect Your Server with Fail2Ban in Linux

Change the default length of the login window

The next settings are “maxretry” and “findtime.” They determine the number of login attempts an attacker can make before Fail2Ban bans their IP address.

The default values for "maxretry" and "findtime" are 5 and 10 m. If an IP fails authentication five times within ten minutes, Fail2Ban will ban it for the duration specified by the bantime setting.

install-fail2ban-linux-10-maximum-login-attempts.png How to Protect Your Server with Fail2Ban in Linux

You can change these values to whatever you want. For example, you can set "maxretry" to 3 and "findtime" to 5m: Fail2Ban will disable an IP address if it fails authentication three times within five minutes.

install-fail2ban-linux-11-reduce-maximum-login-attempts.png How to Protect Your Server with Fail2Ban in Linux

Enable Fail2Ban Notification Feature in Linux

The following settings are destemail, sendername, and mta. These settings are what Fail2Ban will use to configure email notifications.

  • Email setting is the email address to which the program will send its notifications.
  • The sender's name is the name that will appear in the "From" field of the notification email.
  • mta is the mail transfer agent that Fail2Ban will use to send emails. The default mta is sendmail, but you can change it to something else like mail.

If you want to receive email notifications, you need to uncomment these lines and enter the appropriate values as shown.

install-fail2ban-linux-12-email-settings.png How to Protect Your Server with Fail2Ban in Linux

When a ban occurs, you will receive an email notification with details about the ban as shown.

install-fail2ban-linux-13-sample-mail-notification.png How to Protect Your Server with Fail2Ban in Linux

Create custom block orders

The next setting is “action_=.” This specifies the action Fail2ban takes when it bans an IP address. The default action is to use iptables to ban the IP until the “bantime” expires.

You can also use other procedures, as described below. This tutorial sticks to the default setting for simplicity.

  • action_mw: Sends an email notification when an IP address is blocked, along with the relevant WHOIS information.
  • action_mwl: Sends an email notification when an IP address is banned, along with the relevant WHOIS information and log file entries that led to the ban.
  • action_xarf: Sends an email notification in X-ARF format when an IP address is banned along with log file entries that triggered the ban.
Also read:  Why Qubes Linux Might Not Be the Right Choice for You

There are many other procedures available, but it's impossible to cover them all in this tutorial. You can Read about all available procedures. In Fail2ban documentation.

install-fail2ban-linux-14-fail2ban-action-lists.png How to Protect Your Server with Fail2Ban in Linux

Enable service configuration

In addition to configuring Fail2ban's default behavior, it's also possible to use pre-configured "filter files" for some popular internet services. These are small files written by developers to search for specific log output for a particular server.

For example, the “apache-shellshock.conf” file contains all the necessary settings to allow Fail2ban to check for any malicious attempts to create a shellshock error.

install-fail2ban-linux-15-sample-filter-file.png How to Protect Your Server with Fail2Ban in Linux

You can find all available filter files for your system by listing the directory “/etc/fail2ban/filter.d”:

ls /etc/fail2ban/filter.d

install-fail2ban-linux-16-list-all-filter-files.png How to Protect Your Server with Fail2Ban in Linux

Once you know which filters you want to use, tell Fail2ban to load them during startup by opening your “jail.local” file:

sudo nano /etc/fail2ban/jail.local

Create a space in "jail.local" where you can activate your new filters. I create mine between the comment header and the [INCLUDES] block.

install-fail2ban-linux-17-create-new-space.png How to Protect Your Server with Fail2Ban in Linux

Add the filters you want to activate. For example, here's a snippet I use in my configuration:

# Comments: use '#' for comment lines and ';' (following a space) for inline comments [sshd] enabled = true [nginx-bad-request] enabled = true [bitwarden] enabled = true [INCLUDES]

install-fail2ban-linux-18-add-new-filter-modules.png How to Protect Your Server with Fail2Ban in Linux

Once you're done, save and close the file. Restart fail2ban to apply the changes.

sudo systemctl restart fail2ban

Test your configuration

Now that you have configured Fail2Ban on Linux, it's time to test it.

The simplest way to test your configuration is to try logging in with an incorrect password several times in quick succession. You can do this using an SSH connection.

On the disposal device, try SSH into your Fail2ban server using the username “admin.” Replace “your_server_ip_address” with the actual IP address of your Fail2ban server.

ssh admin@your_server_ip_address

Enter a random password when prompted and repeat it several times. After several attempts, you'll see a message stating that the server rejected your connection attempt.

install-fail2ban-linux-19-login-fail-attempt.png How to Protect Your Server with Fail2Ban in Linux

Aside from using SSH, you can also test other filtering features in Fail2ban by running the “failure condition” filter. In my case, I’m using the “nginx-bad-request” filter, which detects if a host is flooding the server with bad requests.

Also read:  Run Adobe Photoshop on Linux using Wine

install-fail2ban-linux-20-nginx-bad-request-filter.png How to Protect Your Server with Fail2Ban in Linux

Knowing this, you can test this feature by deliberately sending empty requests to your web server using curl:

curl -H "User-Agent:" -H "Host:" https://yourwebsite.com -v

install-fail2ban-linux-21-curl-bad-request.png How to Protect Your Server with Fail2Ban in Linux

Checking Fail2Ban Procedures in Linux

Finally, run the following command on the Fail2ban server to verify that fail2ban has added the necessary rules to iptables.

The grep command filters the output of the iptables command. The -S option tells iptables to print the rules in a format that can be easily parsed.

sudo iptables -S | grep f2b

You'll see output similar to the image below. The "-reject-with icmp-port-unreachable" argument tells iptables to send an ICMP port unreachable message to the client when it tries to connect. You can also see the IP addresses of the devices blocked by the server.

install-fail2ban-linux-22-blocked-ip-address-fail2ban.png How to Protect Your Server with Fail2Ban in Linux

Tip: You should also use SELinux to secure your Linux server.

Frequently Asked Questions

Q1: Why do I get a blank email notification when Fail2ban bans an IP address?
The answer: If you receive a blank email notification, it's likely that your mail server isn't configured correctly. Check your mail server's configuration and make sure it can send email.

Q2: How can I unblock an IP address?
The answer: First, find the jail where the IP address is currently banned: sudo fail2ban-client status , then unban the IP address by running the following: sudo fail2ban-client set unsanip .

Q3: How can I view the fail2ban log file?
The answer: You may want to view the fail2ban log file to troubleshoot problems or to determine why an IP address was blocked. The file "/var/log/fail2ban.log" contains all logs generated by fail2ban. Use the cat command to view the fail2ban log file: cat /var/log/fail2ban.log.

Q4: I have enabled multiple filter files. Why are they all not working?
The answer: This problem is most likely caused by a filter overwriting a different file that preceded it. One way to fix this is to create your own filter file by combining multiple filters together.

Q5: Will Fail2ban protect my server from a DoS attack?
The answer: Yes and no. For the most part, Fail2ban will be able to block any IP address sending invalid requests to your server. However, one of the biggest limitations of this program is that it cannot operate on events that do not produce a log entry. As such, it is still important to secure your Linux server with other tools for these types of attacks.

Go to top button