How to Install and Configure Fail2ban for ssh Protection on Debian 12

Fail2ban is an intrusion prevention software framework witten in the Python programming language that is designed to prevent against brute-force attacks. It is able to run on POSIX systems that have an interface to a packet-control system or firewall installed locally, such as iptables or TCP Wrapper.

Fail2ban works together with a packet-control system or firewall installed on your server, and is commonly used to block connection attempts after a number of failed tries.

It operates by monitoring log files for certain types of entries and runs predetermined actions based on its findings.

Ensure that your system is up to date

Ensure that your OS packages are up to date:

1
2
sudo apt update
sudo apt upgrade

Installing Fail2ban

To install Fail2ban, use this command:

1
sudo apt install fail2ban

Type y to confirm installation.

Once installed, copy the default jail.conf file to make a local configuration with this command

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

Then open the new local configuration file for edit with your favourite text editor, for example

1
sudo vim /etc/fail2ban/jail.local

Scroll down to go through some of the settings available in the configuration file.

First up are the basic defaults for ignoreip, which allows you to exclude certain IP addresses from being banned, for example if your own computer has a fixed IP you can enter it here. Next set the bantime which determines how long an offending host will remain blocked until automatically unblocked. Lastly check the findtime and maxretry counts, of which the find time sets the time window for the max retry attempts before the host IP attempting to connect is blocked.

1
2
3
4
5
[DEFAULT]
ignoreip = 127.0.0.1
bantime  = 3600
findtime = 600
maxretry = 3

If you have a sendmail service configured on your cloud server, you can enable the email notifications from Fail2ban by entering your email address into the parameter destemail and changing the action = %(action_)s to action = %(action_mw)s.

Once you’ve done the basic configurations, check the different jails available in the configuration options. Jails are the rules which fail2ban applies to any given application or log file. SSH jail settings, which you can find at the top of the jails list, are enabled by default.

If you have changed the default ssh port make sure to update as well.

1
2
3
[sshd]
enabled = true
port    = 3922

You can enable any other jail modules in the same fashion by editing the enabled parameter to true.

When you’ve enabled all the jails you wish, save the configuration file and exit the editor. Then you’ll need to restart the monitor with the following command

1
sudo systemctl restart fail2ban

With that done, you should now check your iptable rules for the newly added jail sections on each of the application modules you enabled.

1
sudo iptables -L

Any banned IP addresses will appear in the specific chains that the failed login attempts occurred at. You can also manually ban and unban IP addresses from the services you defined jails for with the following commands.

1
2
3
sudo fail2ban-client set <jail> banip/unbanip <ip address>
# For example
sudo fail2ban-client set sshd unbanip 83.136.253.43

Fail2ban is a handy addition to the iptables and firewall access control in general.

Last updated on Oct 14, 2024 11:46 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy