...
Skip to main content
Print

How to Install Fail2ban Against Brute-Force Attacks

Fail2ban protects your server against brute-force attacks. It reads the log files and blocks IP addresses that try a wrong password too often. Every server that is online receives thousands of automated login attempts every day. With Fail2ban you stop them within a few minutes.

Step 1: install Fail2ban

apt update
apt install fail2ban -y

On AlmaLinux you first install the EPEL repository:

dnf install epel-release -y
dnf install fail2ban -y

Step 2: create your own configuration

Never edit the jail.conf file, because it is overwritten during updates. Create the jail.local file instead:

nano /etc/fail2ban/jail.local

Put these settings in the file:

[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5
ignoreip = 127.0.0.1/8 JOUW-EIGEN-IP

[sshd]
enabled = true
  • bantime: how long an IP address is blocked.
  • findtime and maxretry: after 5 failed attempts within 10 minutes a block follows.
  • ignoreip: IP addresses that are never blocked, such as your own IP address.

Step 3: start Fail2ban

systemctl enable --now fail2ban
fail2ban-client status sshd

Managing blocked IP addresses

fail2ban-clientXITENS
fail2ban-client status sshdshows the blocked IP addresses.
fail2ban-client set sshd unbanip IP-ADRESlifts a block.

Tip: combine with an SSH key

Fail2ban works best together with logging in with an SSH key and a firewall such as UFW or CSF.

Related articles

Table of Contents