How to Install Fail2Ban on Rocky Linux 8 (Step-by-Step)

Install Fail2Ban on Rocky Linux 8 with EPEL, firewalld, and a systemd SSH jail. Verify protection, avoid lockouts, and fix common errors on your VPS.
A polished 3D shield deflects glowing attack trails beside a Rocky Linux server with a subtle green accent on a dark navy background.

Install Fail2Ban on Rocky Linux 8 to watch failed SSH authentication and place temporary source blocks in firewalld. In roughly ten minutes, you will have an SSH jail that starts at boot, reads the systemd journal, and gives you clear ban counters to review.

Prerequisites

  • A Rocky Linux 8 VPS with at least 1 GB of RAM and 5 GB of free disk space.
  • SSH access as root, or a normal account that can run commands with sudo.
  • A running sshd service and firewalld. Rocky Linux 8 uses DNF and the EL8 powertools repository name.
  • No domain is needed because this guide protects SSH rather than a web application.
  • A VPSLake Linux VPS if you need a Rocky Linux server with root or sudo access.

The commands use sudo for a regular account. Remove sudo when you are logged in as root.

Step 1: Confirm Rocky Linux 8 and protect your SSH session

Check the release and firewall before installing the jail so you do not attach Fail2Ban to a stopped or unexpected firewall.

grep -E '^(PRETTY_NAME|VERSION_ID|ID_LIKE)=' /etc/os-release
sudo systemctl is-active sshd
sudo systemctl is-active firewalld
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-services

The release should report version 8, sshd and firewalld should be active, and the active zone should list ssh:

PRETTY_NAME="Rocky Linux 8.x (Green Obsidian)"
VERSION_ID="8.x"
ID_LIKE="rhel centos fedora"
active
active
public
  interfaces: eth0
ssh dhcpv6-client

If ssh is missing from the service list, allow it before continuing. Use the active zone shown by the previous command; public is only an example:

sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

If firewalld is stopped, use the VPS console or another confirmed recovery path before starting it over SSH. Then run sudo systemctl enable --now firewalld and confirm that the active zone still permits SSH.

Step 2: Enable Rocky Linux 8 repositories

Fail2Ban is delivered through EPEL, and some EPEL dependencies come from Rocky Linux 8’s disabled-by-default PowerTools repository. Rocky calls the equivalent repository CRB on newer major releases, but the EL8 command is powertools; see the Rocky Linux repository documentation.

Install the DNF repository plugin, enable PowerTools, and add EPEL:

sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --set-enabled powertools
sudo dnf install -y epel-release
sudo dnf makecache

The cache refresh should finish without a repository error. epel-release is available from Rocky’s Extras repository and installs the EPEL repository definition; do not download a random RPM from a third-party mirror.

Step 3: Install Fail2Ban integrations

Install the daemon with its firewalld action files and systemd journal support, which keeps this setup aligned with Rocky Linux 8’s service and logging stack.

sudo dnf install -y fail2ban fail2ban-firewalld fail2ban-systemd
fail2ban-client --version

The package version depends on the current EPEL 8 build. A successful version check looks similar to this:

Fail2Ban v1.0.2

The fail2ban-firewalld package provides the firewall action definitions, while fail2ban-systemd lets a jail read authentication events from journald. The EPEL Fail2Ban package listing shows the Rocky-compatible EL8 package and its companion components.

Step 4: Create a local SSH jail

Store your changes in jail.d so package updates do not overwrite your policy; Fail2Ban’s jail configuration reference recommends local overrides instead of editing the vendor defaults.

Create a policy that ignores loopback, counts five failures in ten minutes, and bans the source for one hour:

sudo install -d -m 0755 /etc/fail2ban/jail.d
sudo tee /etc/fail2ban/jail.d/sshd.local >/dev/null <<'EOF'
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1
bantime = 1h
findtime = 10m
maxretry = 5
backend = systemd

[sshd]
enabled = true
filter = sshd
port = ssh
EOF

The fail2ban-firewalld package supplies the firewalld action used by the installed configuration. If you administer the server from a fixed public address, add that address to ignoreip before starting the service, for example ignoreip = 127.0.0.1/8 ::1 203.0.113.25. Keep the allowlist narrow so it cannot become a back door.

Leave port = ssh when SSH uses the standard service definition. For a custom port, replace it with the numeric value SSH_PORT and make sure firewalld already allows that port:

sudo firewall-cmd --permanent --add-port=SSH_PORT/tcp
sudo firewall-cmd --reload

Replace SSH_PORT with the numeric port configured in /etc/ssh/sshd_config before running those commands.

The sshd filter still reads the journal; the port setting tells the ban action which traffic to block.

Step 5: Validate and enable the service

Parse the full configuration before starting Fail2Ban, then enable it immediately and at every future boot.

sudo fail2ban-client -t
sudo systemctl enable --now fail2ban
sudo systemctl is-active fail2ban
sudo systemctl status fail2ban --no-pager -l

The configuration test and service check should look like this:

OK: configuration test is successful
active

The longer status output should contain Active: active (running). If the parser reports an error, correct /etc/fail2ban/jail.d/sshd.local before restarting. A running daemon without a loaded sshd jail does not protect the SSH service.

Verify the SSH jail

Confirm the loaded jail, inspect its counters, and check the journal without intentionally generating failed logins from your own workstation.

sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo journalctl -u fail2ban -n 20 --no-pager
sudo firewall-cmd --list-rich-rules
sudo systemctl is-enabled fail2ban

On a new VPS, zero failures and an empty rich-rule list are normal. The useful result is one loaded jail named sshd, a running service, and counters that can be read without an error:

Status
|- Number of jail: 1
`- Jail list: sshd

Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  `- Total failed: 0
`- Actions
   |- Currently banned: 0
   `- Total banned: 0

The firewall rule appears only after an address reaches maxretry, while enabled confirms the daemon will return after a reboot.

Troubleshooting

No match for argument: fail2ban

DNF cannot see EPEL, or PowerTools was not enabled before dependency resolution. Check the repository state and rebuild the metadata cache:

sudo dnf repolist --enabled
sudo dnf config-manager --set-enabled powertools
sudo dnf install -y epel-release
sudo dnf clean all
sudo dnf makecache
sudo dnf install -y fail2ban fail2ban-firewalld fail2ban-systemd

The enabled list should contain both powertools and an EPEL repository. If the server is pinned to an old Rocky minor release, update the Rocky repository configuration before retrying.

FirewallD is not running

Fail2Ban cannot add or remove a ban while firewalld is stopped. Confirm SSH access through the provider console or a second session, then run sudo systemctl enable --now firewalld, verify sudo firewall-cmd --state, and restart Fail2Ban.

Jail 'sshd' does not exist

The override was saved under the wrong path, or its section header was misspelled. Display the file and run the parser again:

sudo sed -n '1,120p' /etc/fail2ban/jail.d/sshd.local
sudo fail2ban-client -t

The file must contain an exact [sshd] section and enabled = true. Fix the syntax, then run sudo systemctl restart fail2ban and check sudo fail2ban-client status again.

The jail is active but records no SSH failures

The systemd integration may be missing, or the SSH events may not be present in the journal. Check the service name and recent entries, reinstall the integration, and reload the jail:

sudo journalctl -u sshd -n 30 --no-pager
sudo dnf install -y fail2ban-systemd
sudo fail2ban-client -t
sudo systemctl restart fail2ban

Keep backend = systemd in the local file. If journalctl -u sshd has no entries, resolve the SSH service or logging problem before changing the Fail2Ban filter.

Hardening

  • Use SSH keys and turn off password authentication only after opening a second, verified key-based session.
  • Restrict the SSH service in firewalld to your office or VPN source when that network has a stable address.
  • Review sudo fail2ban-client status sshd during maintenance and keep ignoreip limited to addresses you control.
  • The VPSLake SSH hardening generator can create a starting policy to review before applying.

FAQ

Is Fail2Ban included in Rocky Linux 8’s default repositories?

No. Rocky Linux 8 provides the base system repositories, while Fail2Ban is available through EPEL. PowerTools supplies dependencies that some EPEL packages need, so enable it before installing the daemon.

Does this configuration use /var/log/secure?

No. The jail explicitly uses the systemd backend, which reads the journal associated with sshd.service. This avoids guessing a file path and matches the service-based logging used by a normal Rocky Linux 8 installation.

How do I change the ban duration or threshold?

Edit bantime, findtime, or maxretry in /etc/fail2ban/jail.d/sshd.local. Values such as 30m, 1h, and 1d are easier to review than raw seconds; run sudo fail2ban-client -t and restart Fail2Ban after editing.

How do I unban an administrator’s IP address?

First add the trusted address to ignoreip, otherwise the next matching failures can ban it again. Then replace ADMIN_IP with the affected address:

sudo fail2ban-client set sshd unbanip ADMIN_IP

If you cannot reconnect, use the VPS control panel’s VNC console to update the local jail and run the unban command from the server.

Samrat Ghosh
Written by

Samrat Ghosh

Founder & Infrastructure Engineer

Samrat Ghosh is the founder of VPSLake, where he builds and runs the remote desktop and VPS hosting infrastructure the company is built on. He writes hands-on guides about RDP, Windows Server, VPS management and secure remote access - the practical documentation he wishes had…

Previous Article

How to Install Fail2Ban on AlmaLinux 9 (Step-by-Step)

Next Article

5 Best AWS Lightsail Alternatives in 2026

Write a Comment

Leave a Comment

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

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨