Installation and Configuration of Fail2ban on Amazon Linux 2023

|
| By Navneet Kashyap

It is important to note that Amazon Linux 2023 is pre-installed with Python 3.9 by default. Some of the old Python version features have been removed in Python 3.9, so you will need to use the latest Fail2ban source code in order to install it.

1. Installation dependency package 2to3.

  • yum install 2to3 -y

2. Download the latest fail2ban source code.

wget https://github.com/fail2ban/fail2ban/archive/refs/tags/1.0.2.tar.gz

3. Execute the following command to extract the source code.

  • tar -xvf 1.0.2.tar.gz
  • cd fail2ban-1.0.2/

4. Execute the below commands to initiate the installation process

  • ./fail2ban-2to3
  • python3.9 setup.py build
  • python3.9 setup.py install

5. The next step is to create the service file for this.

  • sudo cp ./build/fail2ban.service /etc/systemd/system/fail2ban.service
  • nano /etc/systemd/system/fail2ban.service and replace the Environment=”PYTHONPATH=/usr/local/lib/python3.9/site-packages”
  • systemctl daemon-reload

6. Now restart the service.

  • sudo systemctl enable fail2ban

7. Now modify the configuration according to the requirement.

  • nano /etc/fail2ban/jail.conf

# "bantime" is the number of seconds that a host is banned.
bantime = 60m
# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime = 10m
# "maxretry" is the number of failures before a host get banned.
maxretry = 10

8.Now start the service.

  • sudo service fail2ban start

9. Below command is to check the IPs blocked.

  • fail2ban-client status

10. If fail2ban not starting jails, then edit the following configurations

nano /etc/fail2ban/jail.conf
backend = auto
to
backend = polling

Leave a Reply

Your email address will not be published. Required fields are marked *