Install Fail2Ban on AlmaLinux 8 to detect repeated SSH authentication failures and ask firewalld to block abusive addresses. In about ten minutes, you will have a boot-persistent SSH jail that reads the systemd journal and reports its ban activity.
Prerequisites
- An AlmaLinux 8 server with at least 1 GB of RAM and 5 GB of free disk space.
- SSH access as
root, or a regular account with workingsudoprivileges. - A reachable
sshdservice and firewalld; this guide uses AlmaLinux’s DNF and firewalld stack. - No domain is necessary because the protected service is SSH.
- A VPSLake Linux VPS with root access if you need a server for this setup.
The commands below use sudo. Remove it when you are already connected as root.
Step 1: Confirm the release and firewall zone
Check the operating system and active firewall before installing anything, because Fail2Ban must attach its bans to the zone handling your network interface.
grep -E '^(PRETTY_NAME|VERSION_ID|ID_LIKE)=' /etc/os-release
sudo systemctl is-active sshd
sudo firewall-cmd --state
sudo firewall-cmd --get-active-zones
You should see AlmaLinux 8, an active SSH service, running firewalld, and at least one active zone. The zone name and interface vary by VPS:
PRETTY_NAME="AlmaLinux 8.x (Sapphire Caracal)"
VERSION_ID="8.x"
ID_LIKE="rhel centos fedora"
active
running
public
interfaces: eth0
If firewalld is missing or stopped, do not blindly enable it over a remote session. Confirm that your current provider firewall or console gives you a recovery path, then install and start it with sudo dnf install -y firewalld and sudo systemctl enable --now firewalld. Make sure the active zone still allows SSH before continuing.
Step 2: Enable PowerTools and EPEL
AlmaLinux 8 calls its supplemental build-dependency repository powertools; EPEL supplies Fail2Ban itself. The AlmaLinux repository guide lists this EL8-specific repository name.
Install the DNF repository-management 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 complete without a repository error. If powertools is already enabled, DNF will simply leave it unchanged.
Step 3: Install Fail2Ban and its AlmaLinux integrations
Install the daemon along with the firewalld action definitions and systemd-journal support so the SSH filter can read the log source used by this release.
sudo dnf install -y fail2ban fail2ban-firewalld fail2ban-systemd
fail2ban-client --version
The version is supplied by EPEL and may change as updates are published. A successful check looks like this:
Fail2Ban v1.1.x
The fail2ban-firewalld package provides actions that add and remove temporary firewalld rules. The systemd package provides the Python integration needed by backend = systemd; without it, the jail may start but fail to inspect the journal.
Step 4: Define an SSH jail in a local override
Keep your policy in /etc/fail2ban/jail.d/ instead of changing the vendor jail.conf, so package upgrades do not overwrite your SSH settings. Fail2Ban documents this override order in its jail configuration reference.
Create a jail that allows five failures in ten minutes and blocks 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
banaction = firewallcmd-rich-rules[actiontype=<multiport>]
[sshd]
enabled = true
filter = sshd
port = ssh
EOF
The loopback addresses are safe defaults. If you administer the VPS from a stable public address, append that address or a narrow CIDR to ignoreip, for example 203.0.113.25, before starting the service. Do not add a broad range: a mistaken whitelist can make SSH protection ineffective. If SSH listens on a custom port, replace port = ssh with its numeric port and ensure that port is allowed in the active firewalld zone.
Step 5: Test and start the service
Validate the complete configuration first, then enable Fail2Ban at boot and start it now.
sudo fail2ban-client -t
sudo systemctl enable --now fail2ban
sudo systemctl is-active fail2ban
sudo systemctl status fail2ban --no-pager -l
The first and third commands should produce output similar to:
OK: configuration test is successful
active
The status view should include Active: active (running). If the configuration test fails, fix that error before restarting; a running service is not useful if the SSH jail was rejected during startup.
Verify the SSH jail and firewalld action
Confirm that the jail loaded, inspect its counters, and review recent daemon messages:
sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo journalctl -u fail2ban -n 20 --no-pager
sudo firewall-cmd --list-rich-rules
On a new server, no address may have been banned yet. The important checks are one loaded jail, sshd in the jail list, and zero or more counters rather than 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 rich-rule list can be empty until an address reaches maxretry; that is normal. Do not create a test ban by guessing passwords from your own workstation, especially before adding its fixed address to ignoreip.
Troubleshooting
Failed to start firewalld.service or FirewallD is not running
The ban action cannot change firewall rules while firewalld is stopped. Use a provider console or a second session if necessary, confirm that SSH is allowed, then run sudo systemctl enable --now firewalld and restart Fail2Ban with sudo systemctl restart fail2ban.
No journal files were found or the SSH jail sees no failures
The systemd backend needs the integration package and a journal containing sshd entries. Install the missing package, check the log source, and reload the jail:
sudo dnf install -y fail2ban-systemd
sudo journalctl -u sshd -n 20 --no-pager
sudo fail2ban-client -t
sudo systemctl restart fail2ban
If journalctl -u sshd is empty, verify that SSH is actually managed by sshd.service and that the service is running before investigating Fail2Ban filters.
Jail 'sshd' does not exist
Fail2Ban did not load the section, usually because the file path or header is wrong. Display the local file and rerun the parser:
sudo sed -n '1,120p' /etc/fail2ban/jail.d/sshd.local
sudo fail2ban-client -t
The file must contain an exact [sshd] section with enabled = true. Correct the file, then run sudo systemctl restart fail2ban.
Your administrator address is banned
Add the trusted address to ignoreip, restart the service, and remove the existing ban. Replace ADMIN_IP with the affected address:
sudo fail2ban-client set sshd unbanip ADMIN_IP
sudo systemctl restart fail2ban
If you cannot reconnect, use the VPS control panel’s VNC console to make the ignoreip edit first.
Hardening
- Use SSH keys and disable password authentication only after testing a second SSH session with the key.
- Limit the SSH service in firewalld to your office or VPN source when you have a stable administration network.
- Keep the
ignoreiplist short and reviewsudo fail2ban-client status sshdduring routine maintenance. - Use the VPSLake SSH hardening generator for a starting configuration, then review every proposed setting before applying it.
FAQ
Does Fail2Ban replace the AlmaLinux firewall?
No. Firewalld still defines the server’s general network policy, while Fail2Ban adds temporary source blocks after matching failed logins. You should continue to allow only the services the VPS actually needs.
Will the SSH jail survive a reboot?
Yes. systemctl enable --now fail2ban starts the daemon immediately and registers it for future boots. The jail is loaded again from /etc/fail2ban/jail.d/sshd.local when the service starts.
How can I change the number of login attempts?
Edit maxretry for the threshold and findtime for the window, then run sudo fail2ban-client -t and restart the service. Lower values react faster but can ban a user who repeatedly mistypes a password, so whitelist only trusted fixed addresses.
Can I protect SSH on a nonstandard port?
Yes. Set the jail’s port to the numeric SSH port and allow that same port in the active firewalld zone. The sshd filter still reads authentication events from the journal; the port setting tells the ban action which traffic to block.