How to install and configure Fail2ban on Ubuntu Cloud Servers

2022-03-01 By Nathan 1607 Views linux ubuntu fail2ban security
6 reviews

Fail2ban is a tool to be used in Linux systems to counteract attacks like Brute force attacks and other automated attacks. This is done by monitoring the services logs such as (/var/log/auth.log, /var/log/apache/access.log) and it searches for malicious activity.


It is an intrusion prevention tool. After detecting an abusive IP address, Fail2Ban can perform multiple actions such as updating iptable firewall rules, adding IP addresses in TCP Wrapper's hosts.deny table, send email notification and any other user-defined action.


In this tutorial, we are going to cover the installation and configuration of Fail2ban in Ubuntu Cloud Servers.


Fail2ban Installation


  1. Log in to the Cloud Servers as the root user.

  2. Refresh and update the repositories – Execute the following command.

    # apt update && upgrade
    

    fail2banubuntu1

  3. Install Fail2ban in the Cloud Servers.

    # apt install fail2ban
    

    fail2banubuntu2


    Press Y and eventually complete the installation.

  4. Once the installation is completed, the service will be running in Ubuntu by default, you would be able to check the status.

    # systemctl status fail2ban
    

    fail2banubuntu3


    For the service to get activated when the server boots up, the service is to be enabled.

    # systemctl enable fail2ban
    

    fail2banubuntu4


Fail2ban Configuration


The default configuration is in /etc/fail2ban/jail.conf.

The screenshot describes the jail.conf:

fail2banubuntu5


For each services, there are parameters set according to which Fail2ban behaves.

  1. bantime – Time duration to which fail2ban imposes ban on the IP

  2. maxretry – The maximum amount of attempts user can try to login to the machine

  3. ignoreip – This is a specification to describe trusted networks so that fail2ban won’t act on it

  4. enabled – A Boolean variable to check the corresponding parameter status if true or false

  5. port – Specify the port of the jail


It is advised not to alter the configuration file, instead, we create a new configuration file named jail.local which by default is read before the jail.conf. Another method is to create a corresponding file inside jail.d folder.

For example, we create jail.local file and try to configure fail2ban on SSH. However, any number of services can be listed in the jail.local file as the fail2ban first checks out .local extension before .conf files.


Therefore it is better to specify the parameters of the services in jail.local file.

# touch /etc/fail2ban/jail.local

fail2banubuntu6


Here, the SSHD parameter is enabled and the port is specified as ssh.

The ban time will be 60 minutes, i.e 1 hour. The maximum number of attempts allowed will be 5.

The 6th attempt will make the user into the banned list. Ignore IP is to ignore the fail2ban action on the host as well as localhost network.


Once the changes are done, save the file and restart the fail2ban process:

fail2banubuntu7

What do you think about this article?

Rate this article
LayerStack Promotion
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.