Raspberry Pi

How to Install Fail2Ban on Raspberry Pi

Fail2Ban is a security solution that help users blocks those who try to enter your system multiple times using the wrong combination of password. It helps you set up the limit and if a person exceeds this limit and enters the wrong password multiple times, he/she will be blocked from the system. Installing this application on your Raspberry Pi system helps you secure your device from the hackers that wants to invade your privacy.

In this article, we will show you how to install Fail2Ban on Raspberry Pi and set up the limit for wring password guessing to secure your system.

Install Fail2Ban on Raspberry Pi

Step 1: The Fail2Ban software can be installed using the official repository of Raspberry Pi. However, before the installation, it’s better to update the repository from the following command:

$ sudo apt update

Step 2: To install Fail2Ban through apt repository, follow the below-given command:

$ sudo apt install fail2ban

Step 3: During the installation process of Fail2Ban, a “jail.conf” file is generated which we need to update according to our choice. However, before performing any configuration, it’s better to create a local copy of this file into the Fail2ban folder so that you can utilize this file if an error occurs during the configuration.

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

Step 4: Now we will open up our jail.local file through nano text editor from the following command:

$ sudo nano /etc/fail2ban/jail.local

Step 5: Within the file, search for “sshd” section using “Ctrl+W” keys.

Step 6: Now below the [sshd] section, we will add two more lines to enable Fail2Ban and filter the sshd connection using the SSH port.

enabled = true
filter = sshd

You can also change the number of max tries and ban time according to your choice but remember that ban will always be in seconds so you can use this below formula to calculate seconds for your required ban time:

No. of Minutes x 60

Like for 10 minutes ban time you can use it as 10 x 60 = 600 seconds, so you will use 600 in front of ban time:

After making the changes, press the keys “Ctrl + X” then Y” to save the file and finally press “Enter” to go back to the terminal.

Step 8: Now to load up the changes into the Raspberry Pi system, type the below-mentioned command in the terminal:

$ sudo service fail2ban restart

Remove Fail2Ban on Raspberry Pi

To remove Fail2Ban from Raspberry Pi, type below-mentioned command:

$ sudo apt remove fail2ban

Conclusion

Fail2Ban is a useful software for securing your Raspberry Pi system. The good thing about Fail2Ban is that it can directly be installed from the official repository of Raspberry Pi. The only thing you have to do after the installation is to configure the settings withing the generated jail.conf file that you must edit to make the changes to your system. The file can easily be opened through nano text editor and within this file, you have to choose the number of passwords tries and bantime for securing your Raspberry Pi system.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.