FedoraRHEL Based

How To Install Fail2Ban on Fedora 40

Install Fail2Ban on Fedora 40

In today’s digital landscape, server security is paramount. Fail2Ban stands as a formidable ally in the fight against malicious attacks, particularly brute-force attempts targeting Linux servers. This intrusion-prevention software has become an essential tool for system administrators and security-conscious users alike.

Fedora 40, known for its cutting-edge features and robust security measures, provides an excellent platform for implementing Fail2Ban. By integrating this powerful software, you can significantly enhance your server’s defenses against persistent threats.

This comprehensive guide will walk you through the process of installing and configuring Fail2Ban on Fedora 40. We’ll cover everything from the initial setup to advanced configuration options, ensuring you have the knowledge to fortify your server effectively.

Prerequisites

Before diving into the installation process, let’s ensure you have everything needed to proceed smoothly:

System Requirements

  • A Fedora 40 server or workstation
  • Root access or sudo privileges
  • A stable internet connection for package downloads

Software Requirements

  • Python (pre-installed on Fedora 40)
  • Up-to-date system packages

It’s crucial to start with an updated system to ensure compatibility and security. Let’s begin by updating your Fedora 40 installation:

sudo dnf update && sudo dnf upgrade -y

This command will fetch the latest package information and upgrade all installed packages to their newest versions.

Step 1: Installing Fail2Ban on Fedora 40

With our system primed and ready, let’s proceed with the installation of Fail2Ban:

Install Fail2Ban

Fedora’s repositories include Fail2Ban, making the installation process straightforward. Execute the following command:

sudo dnf install fail2ban

This command will download and install Fail2Ban along with its dependencies. The process should complete within a few minutes, depending on your internet speed.

Verify Installation

To ensure Fail2Ban has been installed correctly, we can check for its presence in the system:

find /var -name fail2ban

This command should return several paths, including configuration files and directories related to Fail2Ban. If you see the results, it confirms that the installation was successful.

Step 2: Configuring Fail2Ban

With Fail2Ban installed, it’s time to configure it to protect your Fedora 40 server effectively. The configuration process involves creating a local configuration file and setting up various parameters to customize Fail2Ban’s behavior.

Create a Local Configuration File

Fail2Ban uses two main configuration files: jail.conf and jail.local. It’s crucial to create and use jail.local for your custom settings, as updates to Fail2Ban might overwrite jail.conf.

Create the jail.local file using the following command:

sudo nano /etc/fail2ban/jail.local

This opens the nano text editor. Now, let’s add some basic configuration settings.

Basic Configuration Settings

Copy and paste the following content into your jail.local file:

[DEFAULT]
ignoreip = 127.0.0.1/8 ::1
bantime = 3600
findtime = 600
maxretry = 5

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = %(sshd_log)s
maxretry = 3

Let’s break down these settings:

  • ignoreip: IPs that Fail2Ban should ignore (localhost in this case)
  • bantime: Duration of the ban in seconds (1 hour)
  • findtime: Time frame in which Fail2Ban checks for repeated failures (10 minutes)
  • maxretry: Number of failures before a ban is imposed (5 attempts)

The [sshd] section specifically configures protection for SSH:

  • enabled: Activates the SSH jail
  • port: Specifies the SSH port (default is 22)
  • filter: Uses the built-in sshd filter
  • logpath: Path to the SSH log file
  • maxretry: Overrides the default maxretry for SSH (3 attempts)

Save the file and exit the editor (in nano, press Ctrl+X, then Y, then Enter).

Advanced Configuration Options

For more comprehensive protection, you can add additional jails and actions. Here are some examples:

Email Notifications

To receive email notifications when Fail2Ban takes action, add the following to your jail.local file:

[DEFAULT]
destemail = your_email@example.com
sender = fail2ban@example.com
action = %(action_mwl)s

This configuration will send detailed emails, including whois information and relevant log entries.

Protecting Apache

If you’re running an Apache web server, add this section:

[apache-auth]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/httpd/error_log
maxretry = 3

Integration with FirewallD

Fedora 40 uses FirewallD by default. To ensure Fail2Ban works seamlessly with it, add:

[DEFAULT]
banaction = firewallcmd-ipset

This setting tells Fail2Ban to use FirewallD’s ipset feature for banning IPs.

Step 3: Starting and Enabling Fail2Ban Service

With the configuration in place, it’s time to start the Fail2Ban service and ensure it runs on system boot.

Start Fail2Ban Service

To start the Fail2Ban service immediately, use this command:

sudo systemctl start fail2ban

Enable Fail2Ban on Boot

To ensure Fail2Ban starts automatically when your Fedora 40 system boots up, enable the service:

sudo systemctl enable fail2ban

Verify Service Status

Check if Fail2Ban is running correctly:

sudo systemctl status fail2ban

This command should display “active (running)” in the output, confirming that Fail2Ban is operational.

Step 4: Testing Fail2Ban Configuration

To ensure Fail2Ban is working as expected, we can simulate failed login attempts and observe the software’s response.

Simulate Failed SSH Login Attempts

From another machine or a different SSH session, attempt to log in with an incorrect password multiple times. For example:

ssh nonexistentuser@your_server_ip

Repeat this process several times, ensuring you exceed the maxretry limit set in your configuration.

Check Banned IPs

On your Fedora 40 server, check if Fail2Ban has banned the IP:

sudo fail2ban-client status sshd

This command will display information about the SSH jail, including any currently banned IP addresses.

Unbanning an IP Address

If you need to unban an IP address (for instance, if you accidentally banned yourself), use:

sudo fail2ban-client set sshd unbanip IP_ADDRESS

Replace IP_ADDRESS with the actual IP you want to unban.

Troubleshooting Common Issues

Even with careful configuration, you might encounter some issues. Here are solutions to common problems:

Fail2Ban Not Starting

If Fail2Ban doesn’t start, check the system logs:

sudo journalctl -u fail2ban

Look for error messages that might indicate configuration problems or conflicts with other services.

Incorrect Log Paths

Ensure the log paths in your jail.local file are correct. Fedora 40 might use different paths compared to other distributions. Verify with:

ls /var/log/

FirewallD Conflicts

If you’re using FirewallD and Fail2Ban doesn’t seem to be banning IPs, check if the banaction is set correctly:

grep banaction /etc/fail2ban/jail.local

It should be set to firewallcmd-ipset for proper FirewallD integration.

Log File Analysis

For deeper troubleshooting, examine the Fail2Ban log:

sudo tail -f /var/log/fail2ban.log

This command will display the log in real time, helping you identify any issues as they occur.

Congratulations! You have successfully installed Fail2Ban. Thanks for using this tutorial for installing the Fail2Ban on the Fedora 40 system. For additional help or useful information, we recommend you check the official Fail2Ban website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button