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.
You need the APT Package Manager and an editor, for example vi or nano, to create and change config files.
All commands are executed with sudo because root permissions are required.
sudo apt-get update sudo apt-get install fail2ban
Fail2Ban has two configuration files:
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
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Open the configuration file
jail.local with a text editor of your choice:
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.
Open the configuration file with a text editor:
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.
You can optionally configure email notifications in the file
To do so, you have to change the [DEFAULT]-section:
[DEFAULT] ## ... action = %(action_mw)s destemail = firstname.lastname@example.org sender = email@example.com
sudo service fail2ban restart
sudo service fail2ban status
The logs should look like this:
Nov 06 23:13:44 v22039013573286090 systemd: Starting Fail2Ban Service... Nov 06 23:13:44 v22039013573286090 systemd: Started Fail2Ban Service. Nov 06 23:13:44 v22039013573286090 fail2ban-server: Server ready
By reading this tutorial you learned how to install and configure Fail2Ban. Fail2Ban supports not only SSHD, but also databases, mail servers and webservers.
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.
By making a contribution to this project, I certify that:
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
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
The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.
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.