Root-Server Tutorial

ROOT-SERVER TUTORIAL

Installing and Configuring Fail2Ban on a Debian Server

Learn how to install and configure Fail2Ban on a Debian server to block IP addresses when too many SSH login attempts fail in a short time.

Author:

Patrick Weber

Last updated:

06/11/2021

Introduction

This tutorial explains how to install and configure Fail2Ban on a Debian Linux system. If too many SSH login failures are registered for an IP address, Fail2Ban will block this IP address for a specified blocking time.

Requirements

You need the APT Package Manager and an editor, for example vi or nano, to create and change config files.

Step 1 - Install Fail2Ban with the APT Package Manager

All commands are executed with sudo because root permissions are required.

sudo apt-get update
sudo apt-get install fail2ban

Step 2 - Copy the default config file to a new location

Fail2Ban has two configuration files: /etc/fail2ban/jail.conf and /etc/fail2ban/jail.d/defaults-debian.conf. You should not update these files because they may be overwritten when the package is updated. The recommended way is to create a .local file, which overrides the settings of the .conf file:

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

Step 3 - Change the [DEFAULT] section and add configuration

Open the configuration file jail.local with a text editor of your choice:

vi /etc/fail2ban/jail.local

Change the section [DEFAULT] to this configuration:

[DEFAULT]
## "ignoreip" can be a list of IP addresses, CIDR masks or DNS hosts. Fail2Ban
## will not ban a host that matches an address in this list. Several addresses
## can be defined using space (and/or comma) separator.
ignoreip = 127.0.0.1/8 ::1
## "bantime" is the number of seconds that a host is banned.
bantime  = 3600
## A host is banned if it has generated "maxretry" during the last "findtime"
## seconds.
findtime = 600
## "maxretry" is the number of failures before a host gets banned.
maxretry = 5

Save the configuration file.

Step 4 - Enable SSHD in the [sshd] section of the configuration file

Open the configuration file with a text editor:

vi /etc/fail2ban/jail.local

Search for the [sshd] section:

#
### SSH servers
#

[sshd]

Add 'enabled' after the [sshd]:

#
# SSH servers
#

[sshd]
enabled = true

With this configuration we enable SSHD for Fail2Ban to watch the log path for authentication errors. Save the configuration file.

Step 5 - Optional: Configure email notifications

You can optionally configure email notifications in the file /etc/fail2ban/jail.local. To do so, you have to change the [DEFAULT]-section:

[DEFAULT]
## ...
action = %(action_mw)s
destemail = admin@your-special-netcup-domain.net
sender = root@your-special-netcup-domain.net

Step 6 - Restart the Fail2Ban service

sudo service fail2ban restart

Step 7 - Check the status of the Fail2Ban service

sudo service fail2ban status

The logs should look like this:

Nov 06 23:13:44 v22039013573286090 systemd[1]: Starting Fail2Ban Service...
Nov 06 23:13:44 v22039013573286090 systemd[1]: Started Fail2Ban Service.
Nov 06 23:13:44 v22039013573286090 fail2ban-server[2352904]: Server ready

Conclusion

By reading this tutorial you learned how to install and configure Fail2Ban. Fail2Ban supports not only SSHD, but also databases, mail servers and webservers.

License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contributor's Certificate of Origin

By making a contribution to this project, I certify that:

  1. The contribution was created in whole or in part by me and I have the right to submit it under the license indicated in the file; or

  2. The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same license (unless I am permitted to submit under a different license), as indicated in the file; or

  3. The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

  4. I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the license(s) involved.

Published 06/11/2021 by Patrick Weber

Submit your tutorial

Get 60€ netcup vouchers for every published tutorial.