How to Install Fail2ban with Firewalld on Fedora Linux

Installing Fail2ban with Firewalld on Fedora creates a powerful defense against brute-force attacks and unauthorized access attempts. Fail2ban blocks hostile IP addresses after repeated login failures, while Firewalld enforces flexible network policies. When you combine them, you get responsive protection that adapts to real threats without constant manual intervention. This guide shows how to install both tools and configure Fail2ban to work seamlessly with Firewalld’s zones and ipsets.

Along the way, you’ll verify services, create custom jails, and learn how to monitor and test bans in real time. Whether you’re hardening a personal VPS or maintaining production hosts, these steps keep intruders out while preserving the workflows you rely on. Together, Fail2ban and Firewalld deliver automated jail enforcement, native ipset integration that respects Fedora zones, broad service coverage, and optional alerts so you always know when bans trigger.

Update Fedora Linux System Packages

First, start by updating your system’s package list to ensure compatibility and avoid conflicts during installation. Open your terminal (press Ctrl+Alt+T, the Linux equivalent of launching Command Prompt or PowerShell) and run:

sudo dnf upgrade --refresh

This command refreshes and updates all your system packages. If updates include kernel changes, however, you should reboot your system to ensure all changes take effect.

Install Firewalld

Next, install Firewalld, a dynamic firewall manager for Linux systems. With your system updated, run the following command:

sudo dnf install firewalld

Firewalld manages network traffic through customizable rules and is a critical component of system security.

Enable and Start Firewalld

After installation, set Firewalld to start automatically at boot and launch it immediately using:

sudo systemctl enable firewalld --now

This ensures Firewalld runs automatically after system restarts, maintaining continuous network protection.

Verify Firewalld Status

Now, confirm Firewalld is running correctly with:

sudo firewall-cmd --state

If so, a “running” response confirms Firewalld is active and functioning correctly.

Review Current Firewalld Rules

Before integrating Fail2ban, it’s important to review the current Firewalld rules to understand your existing security configuration:

sudo firewall-cmd --list-all

This lets you ensure your current firewall rules allow Fail2ban to integrate smoothly with your existing security configuration. With Firewalld now operational, you can proceed to install Fail2ban.

Install Fail2ban with Firewalld Integration

Now that Firewalld is configured, install Fail2ban and its Firewalld integration package. Both are available in the Fedora repository:

sudo dnf install fail2ban fail2ban-firewalld

This installs Fail2ban and the components needed for seamless Firewalld integration.

Verify Fail2ban Installation

After installation, confirm Fail2ban installed correctly by checking its version:

fail2ban-client --version

This confirms the installed version is current and ready to use.

Enable and Start Fail2ban Service

Next, activate Fail2ban to start automatically at boot and begin monitoring immediately:

sudo systemctl enable fail2ban --now

This ensures Fail2ban continuously monitors your server for suspicious activity.

Verify Fail2ban Service Status

At this point, confirm Fail2ban is running correctly:

systemctl status fail2ban

When successful, an active status indicates Fail2ban is monitoring your server successfully.

With both services active, Fail2ban is now successfully installed and running. The following sections cover configuration basics and how to customize the setup for your specific needs.

Configure Fail2ban

Understand Fail2ban Configuration Files

To start, Fail2ban uses a hierarchy of configuration files. When you install the fail2ban-firewalld package, it automatically creates /etc/fail2ban/jail.d/00-firewalld.conf with the correct Firewalld integration settings. This file sets banaction = firewallcmd-ipset, which uses efficient ipset-based blocking.

Instead of modifying the default configuration files (which get overwritten during updates), create a local configuration file for your custom settings:

sudo nano /etc/fail2ban/jail.d/local.conf

This approach is cleaner than copying jail.conf to jail.local because it only contains your customizations rather than the entire default configuration. Settings in jail.d/local.conf override defaults while preserving the Firewalld integration already configured by 00-firewalld.conf.

Configure Basic Settings

Next, add your custom settings to /etc/fail2ban/jail.d/local.conf. Here’s a basic configuration example:

[DEFAULT]
# Backend for log parsing - systemd is used on modern Fedora
backend = systemd

# IP addresses to never ban
ignoreip = 127.0.0.1/8 ::1

# Ban duration in seconds (3600 = 1 hour)
bantime = 3600

# Time window to count failures (600 = 10 minutes)
findtime = 600

# Number of failures before ban
maxretry = 5

# Uncomment and adjust these when you enable email alerts
# destemail = root@localhost
# sender = fail2ban@localhost

[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 5
findtime = 600
bantime = 3600

Notably, the backend = systemd setting is important for Fedora because modern versions use systemd’s journal instead of traditional log files. The commented destemail and sender entries act as placeholders so you can replace them later without duplicating values when you enable alerts. The fail2ban-firewalld package includes /etc/fail2ban/jail.d/00-systemd.conf that may already set the backend, but explicitly defining it ensures consistency.

Advanced Configuration Options

Additionally, beyond the basic settings, you can fine-tune Fail2ban’s behavior with additional parameters.

Ban Time Increment

To increase ban duration for repeat offenders, add the following lines inside the existing [DEFAULT] block in your configuration:

# Enable incremental ban time for repeat offenders
bantime.increment = true

# Multiplier values for each subsequent ban
bantime.multipliers = 1 2 4 8 16 32 64

This configuration doubles the ban time with each offense, starting at the base bantime value and multiplying up to 64 times the original duration for persistent offenders.

Whitelist IP Addresses

Alternatively, prevent specific IP addresses or networks from being banned by extending the ignoreip setting in the same [DEFAULT] section:

# Whitelist local networks and trusted IPs
ignoreip = 127.0.0.1/8 ::1 192.168.1.0/24 203.0.113.50

For example, include your own IP address, trusted networks, and any monitoring systems that might trigger false positives. Use CIDR notation for network ranges.

Service-Specific Settings

Alternatively, configure ban policies for individual services. For example, set stricter rules for Apache:

[apache-auth]
enabled = true
port = http,https
logpath = %(apache_error_log)s
bantime = 86400
maxretry = 3
findtime = 600

This configuration enables protection for Apache authentication, banning IPs for 24 hours (86400 seconds) after 3 failed attempts within 10 minutes. If you’re running an Apache web server on Fedora, this jail provides essential protection against authentication attacks.

Email Notifications

Additionally, configure email alerts when Fail2ban takes action. First ensure you have a mail transfer agent installed (Postfix or Sendmail):

sudo dnf install -y postfix mailx
sudo systemctl enable --now postfix

Afterward, add the email settings to the [DEFAULT] block so every jail can reuse them:

destemail = admin@example.com
sender = fail2ban@example.com
# Send email with whois data and log lines
action = %(action_mwl)s

The action_mwl action relies on the mail binary provided by the mailx package, so install it alongside Postfix (or whichever mail relay you prefer). Send a quick test with mail to confirm outbound delivery before depending on automated alerts. If you use an external SMTP provider, adjust Postfix or swap in a different Fail2ban action that matches your mail setup.

Remember to remove or replace the commented placeholder email lines from the earlier configuration block instead of adding duplicate keys. Keeping each option defined only once prevents Fail2ban from ignoring values due to conflicting entries.

Common Jail Configurations

Fail2ban includes pre-configured jails for common services. Here are practical examples for the most frequently used services on Fedora.

SSH Protection (SSHD Jail)

To begin, protect SSH from brute-force attacks by expanding the [sshd] block you already created in jail.d/local.conf so it includes explicit limits and journal usage:

[sshd]
enabled = true
port = ssh
# Use systemd journal as log source
backend = systemd
# Ban for 1 hour after 5 failed attempts in 10 minutes
maxretry = 5
findtime = 600
bantime = 3600

Here, the backend = systemd setting tells Fail2ban to read from the systemd journal rather than log files, which is the standard approach on modern Fedora systems.

Nginx Jail

Similarly, protect Nginx web servers from various attacks:

# Protect against HTTP authentication failures
[nginx-http-auth]
enabled = true
port = http,https
logpath = %(nginx_error_log)s
maxretry = 3
bantime = 3600

# Block bots scanning for exploits
[nginx-botsearch]
enabled = true
port = http,https
logpath = %(nginx_access_log)s
maxretry = 2
bantime = 7200

Postfix Email Server Jail

Likewise, protect Postfix mail servers from spam and authentication attacks:

# Protect against SMTP authentication failures
[postfix-sasl]
enabled = true
port = smtp,submission,smtps
backend = systemd
maxretry = 3
bantime = 3600

# Block dictionary attacks on email addresses
[postfix-rbl]
enabled = true
port = smtp,submission,smtps
backend = systemd
maxretry = 1
bantime = 86400

Dovecot IMAP/POP3 Jail

Similarly, secure Dovecot IMAP and POP3 services from brute-force login attempts:

[dovecot]
enabled = true
port = imap,imaps,pop3,pop3s
backend = systemd
maxretry = 5
findtime = 600
bantime = 3600

Restart Fail2ban

After making configuration changes, restart Fail2ban to apply them:

sudo systemctl restart fail2ban

Custom Filters and Actions

Creating custom filters and actions in Fail2ban lets you tailor server security to specific needs on Fedora Linux.

Custom Filters

Fail2ban uses filters, defined by regular expressions, to identify unauthorized access attempts in log files. These filters are stored in the /etc/fail2ban/filter.d directory.

Creating a Custom Filter

First, to create a custom filter, open a new file in the filter directory:

sudo nano /etc/fail2ban/filter.d/mycustomfilter.conf

This step opens a new filter configuration file for editing.

Next, define your custom filter in this file. The filter definition typically includes a failregex, which is a regular expression matching log entries that indicate unauthorized access attempts:

[Definition]
failregex = ^.*unauthorized access attempt from <HOST>.*$
ignoreregex =

In this example, the failregex pattern matches any log entry containing an unauthorized access attempt from a specific IP address (represented by <HOST>).

Custom Actions

Meanwhile, actions in Fail2ban define how the system responds to detected offenses. They are stored in the /etc/fail2ban/action.d directory.

Creating a Custom Action

Then, create a new file in the action directory:

sudo nano /etc/fail2ban/action.d/mycustomaction.conf

This step opens a new file for editing your custom action configuration.

Afterward, add your action definition to this file:

[Definition]
actionstart = /usr/local/bin/mycustomaction start <name>
actionstop = /usr/local/bin/mycustomaction stop <name>
actioncheck = /usr/local/bin/mycustomaction status <name>
actionban = /usr/local/bin/mycustomaction ban <ip>
actionunban = /usr/local/bin/mycustomaction unban <ip>

At this stage, replace /usr/local/bin/mycustomaction with the path to your custom action script. Parameters like <name> and <ip> are dynamically replaced by Fail2ban when the action executes.

Integrating Custom Filters and Actions

Finally, after creating custom filters and actions, integrate them into your jail configuration:

[mycustomjail]
enabled = true
port = 8080
logpath = /var/log/mycustomservice.log
filter = mycustomfilter
action = mycustomaction
maxretry = 3
bantime = 1h

This jail configuration uses the custom filter and action you created, monitoring the specified log file and enforcing bans based on your settings.

Apply Configuration Changes

Restart Fail2ban to activate your new configurations:

sudo systemctl restart fail2ban

This loads all your custom settings and makes them operational.

Manage Bans with Fail2ban-Client

In addition, the fail2ban-client command manages Fail2ban configurations and jails, particularly for manual ban and unban operations.

Check Active Jails

To begin, view all currently active jails and their status:

sudo fail2ban-client status

Consequently, this displays a list of all enabled jails. Example output:

Status
|- Number of jail:      2
`- Jail list:   sshd, apache-auth

Ban an IP Address

When necessary, manually ban an IP address in a specific jail:

sudo fail2ban-client set <jail-name> banip <ip-address>

Replace <jail-name> with the relevant jail name and <ip-address> with the IP you want to ban. For example:

sudo fail2ban-client set apache-auth banip 192.168.1.1

This bans the IP address 192.168.1.1 in the apache-auth jail, preventing access from that IP to services protected by this jail.

Unban an IP Address

Conversely, to remove a ban from an IP address in a specific jail:

sudo fail2ban-client set <jail-name> unbanip <ip-address>

Again, replace <jail-name> and <ip-address> with the appropriate jail name and IP address. For example:

sudo fail2ban-client set apache-auth unbanip 192.168.1.1

This removes the ban on IP address 192.168.1.1 from the apache-auth jail.

View Banned IP Addresses

Additionally, to view currently banned IP addresses in a specific jail:

sudo fail2ban-client status <jail-name>

Replace <jail-name> with the jail you want to inspect. For example:

sudo fail2ban-client status apache-auth

This displays the status of the apache-auth jail, including all currently banned IP addresses.

Get Help with Fail2ban-Client

For additional information about available fail2ban-client commands, run:

sudo fail2ban-client -h

This displays a help menu with a comprehensive list of available options and commands.

Verify Firewalld and Fail2ban Integration

Thoroughly testing the integration between Firewalld and Fail2ban confirms they work together correctly for optimal server security.

Understanding Firewalld IPsets

Under the hood, when you install fail2ban-firewalld, it configures Fail2ban to use the firewallcmd-ipset ban action. This creates and manages ipsets (efficient IP address sets) within Firewalld rather than individual firewall rules. To view these ipsets:

sudo firewall-cmd --get-ipsets
sudo firewall-cmd --info-ipset f2b-sshd

Active Fail2ban jails create Firewalld ipsets named like f2b-jailname. For example, an active sshd jail creates f2b-sshd. This approach is more efficient than creating individual firewall rules for each banned IP, especially when managing hundreds or thousands of bans. If you still run the legacy iptables backend, sudo ipset list provides equivalent output.

Enable the SSHD Jail for Testing

If the SSHD jail is not already active, open your existing configuration file and confirm the [sshd] block keeps enabled set to true:

sudo nano /etc/fail2ban/jail.d/local.conf

Then verify the existing SSHD jail configuration includes the enabled flag:

[sshd]
enabled = true

Afterward, save the file and restart Fail2ban:

sudo systemctl restart fail2ban

Test Ban Functionality

To validate the setup, manually test the ban functionality by banning an IP address in the SSHD jail:

sudo fail2ban-client set sshd banip 203.0.113.7

This instructs Fail2ban to ban the specified IP address in the SSHD jail.

Next, verify that Firewalld applied the ban by checking for ipset entries:

sudo firewall-cmd --get-active-zones
sudo firewall-cmd --info-ipset f2b-sshd

At this point, you should see the banned IP in the Fail2ban-managed ipset. Alternatively, check the Fail2ban jail status:

sudo fail2ban-client status sshd

Example output showing a successful ban:

Status for the jail: sshd
|- Filter
|  |- Currently failed:	0
|  |- Total failed:	0
|  `- Journal matches:  _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned:	1
   |- Total banned:	1
   `- Banned IP list:	203.0.113.7

Test Real-World SSH Protection

For a more realistic test, intentionally trigger failed SSH login attempts from a test machine (not your main connection). From another computer or VM, attempt to SSH with incorrect passwords:

ssh testuser@your-server-ip

After exceeding the maxretry threshold (default 5 attempts), your IP will be banned. Check the Fail2ban log to confirm:

sudo tail -n 20 /var/log/fail2ban.log

Soon after, you should see entries indicating the ban action. The tail command displays the most recent log entries, making it perfect for monitoring real-time events. Additionally, you can check the system auth log for the failed attempts:

sudo journalctl -u sshd | grep "Failed password" | tail -n 10

To unban the test IP and restore access:

sudo fail2ban-client set sshd unbanip YOUR_TEST_IP

Revert Test Changes

After testing, if you want to disable the SSHD jail, edit your configuration file once more:

sudo nano /etc/fail2ban/jail.d/local.conf

If needed, set enabled to false or remove the sshd jail section entirely:

[sshd]
enabled = false

Restart Fail2ban:

sudo systemctl restart fail2ban

This deactivates the SSHD jail and returns the configuration to its original state.

Monitor Fail2ban Logs

Consistently monitoring Fail2ban logs helps ensure your jails operate correctly on Fedora Linux. The default log location is /var/log/fail2ban.log.

Watch Logs in Real-Time

To view the log file as events occur, use the tail -f command. This approach is useful for immediate troubleshooting or monitoring ongoing activities:

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

Running this command keeps the log file open and updates it in real-time with each new entry. For more advanced log monitoring techniques, see our comprehensive tail command guide.

Show a Specific Number of Log Entries

Alternatively, to review a specific number of recent log entries, use the tail command with the -n flag to specify how many lines to display:

sudo tail -n 30 /var/log/fail2ban.log

This displays the last 30 lines of the log file. Replace 30 with any number that fits your needs.

Search Logs for Specific Content

For targeted log analysis, such as finding entries related to a specific IP address or event, use the grep command to search for patterns:

sudo grep '192.168.1.1' /var/log/fail2ban.log

In this example, grep locates all occurrences of the IP address 192.168.1.1 in the Fail2ban log. This is effective for quickly identifying actions taken against a specific IP or finding patterns related to certain security incidents. Learn more about advanced text searching in our grep command guide.

Remove Fail2ban from Fedora

Uninstall Fail2ban

When you no longer need Fail2ban on your Fedora system, uninstall it to remove the software and its unused dependencies:

sudo dnf remove fail2ban fail2ban-firewalld

This removes Fail2ban, its Firewalld integration, and all unused dependencies.

Conclusion

You’ve successfully installed and configured Fail2ban with Firewalld on Fedora Linux, creating a robust defense against brute-force attacks and unauthorized intrusions. The combination of Fail2ban’s automated log monitoring and Firewalld’s dynamic rule management provides comprehensive server protection. Regular monitoring of Fail2ban logs, keeping your system updated with automatic updates, and adjusting jail configurations as your needs evolve will maintain strong security over time.

Leave a Comment