Install Fail2Ban on AlmaLinux 9 to detect repeated SSH authentication failures and add temporary blocks in firewalld. In about ten minutes, you will have a persistent SSH jail that reads the systemd journal and protects the management port.
Prerequisites
- An AlmaLinux 9 VPS 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 running
sshdservice and firewalld. AlmaLinux 9 uses DNF and firewalld, notaptand UFW. - No domain is required because this setup protects SSH directly.
- A VPSLake Linux VPS if you need an AlmaLinux server with root access.
The commands assume a sudo-enabled user. If you are already connected as root, remove sudo from the commands.
Step 1: Check AlmaLinux 9 and the active firewall zone
Confirm the release and network services first so Fail2Ban will work with the firewall and SSH daemon that are actually running.
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
The release must show version 9, while the service checks should return active. The zone and interface depend on your VPS:
PRETTY_NAME="AlmaLinux 9.x (Emerald Puma)"
VERSION_ID="9.x"
ID_LIKE="rhel centos fedora"
active
active
public
interfaces: eth0
If firewalld is stopped, do not start it blindly over SSH. Use the VPS control panel’s VNC console or confirm that SSH is allowed in the active zone first, then run sudo systemctl enable --now firewalld. A firewall change can otherwise cut off your current session.
Step 2: Enable CRB and EPEL
Fail2Ban is published for AlmaLinux 9 through EPEL, and EPEL dependencies may come from AlmaLinux’s CodeReady Builder repository, called CRB on this release.
Install the repository-management plugin, enable CRB, and add EPEL:
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --set-enabled crb
sudo dnf install -y epel-release
sudo dnf makecache
The cache refresh should finish without a repository error. AlmaLinux’s extra repositories documentation lists the EL9-specific crb command; do not substitute the EL8 powertools name here.
Step 3: Install Fail2Ban integrations
Install the daemon with its firewalld action definitions and systemd-journal support so the SSH filter can read AlmaLinux 9 authentication events.
sudo dnf install -y fail2ban fail2ban-firewalld fail2ban-systemd
fail2ban-client --version
The package version can change as EPEL publishes updates. A valid check looks like this:
Fail2Ban v1.1.0
The firewalld package supplies the action that creates temporary rich rules. The systemd package supplies the journal reader used by backend = systemd. EPEL’s Fail2Ban package page lists these EL9 package relationships.
Step 4: Create an SSH jail override
Keep local policy in jail.d so a future package update does not overwrite it; the vendor jail.conf is a defaults file, not the right place for custom settings. Fail2Ban’s jail configuration manual describes the override behavior.
Create a jail that bans an address for one hour after five failures within ten minutes:
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 entries are safe defaults. Add your fixed administrator address to ignoreip before enabling the service if you manage the VPS from a stable public IP, for example ignoreip = 127.0.0.1/8 ::1 203.0.113.25. Use a narrow address or CIDR, not a broad public range.
Leave port = ssh when the SSH service uses its normal service definition. If SSH listens on a custom port, replace it with that numeric port and make sure the same port is already allowed in the active firewalld zone.
Step 5: Validate and start the service
Parse the complete configuration before starting Fail2Ban, then enable it for future boots and start it immediately.
sudo fail2ban-client -t
sudo systemctl enable --now fail2ban
sudo systemctl is-active fail2ban
sudo systemctl status fail2ban --no-pager -l
The parser and service check should produce results similar to:
OK: configuration test is successful
active
The longer status output should include Active: active (running). If the parser reports an error, correct the jail file before restarting; a running daemon without the sshd jail does not protect SSH.
Verify the SSH jail
Check the loaded jail, its counters, the Fail2Ban journal, and the firewalld rules without deliberately generating failed logins:
sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo journalctl -u fail2ban --since "10 minutes ago" --no-pager
sudo firewall-cmd --list-rich-rules
sudo systemctl is-enabled fail2ban
On a new VPS, the counters can all be zero and the rich-rule list can be empty. That is expected until an address reaches maxretry; the important result is that one jail is loaded and its name is sshd:
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 final command should print enabled, confirming that Fail2Ban will return after a reboot. Avoid testing by guessing passwords from your workstation; whitelist its fixed address first or inspect the counters and service logs instead.
Troubleshooting
FirewallD is not running
The action cannot add or remove bans while firewalld is stopped. Confirm that you have console access and that SSH is allowed, then run sudo systemctl enable --now firewalld, check sudo firewall-cmd --state, and restart Fail2Ban.
Jail 'sshd' does not exist
The local file was not loaded, often because it was saved outside /etc/fail2ban/jail.d/ or the section header is misspelled. Inspect the file and run the parser again:
sudo sed -n '1,120p' /etc/fail2ban/jail.d/sshd.local
sudo fail2ban-client -t
The file needs an exact [sshd] section with enabled = true. Fix any syntax error, then run sudo systemctl restart fail2ban.
The jail loads but shows no SSH failures
The jail may be reading a file backend while AlmaLinux is storing SSH events in journald, or the journal integration package may be missing. Confirm the service name and journal entries, reinstall the integration, and reload the daemon:
sudo journalctl -u sshd -n 20 --no-pager
sudo dnf install -y fail2ban-systemd
sudo fail2ban-client -t
sudo systemctl restart fail2ban
The backend = systemd setting and the sshd filter must remain in sshd.local.
iptables action errors appear in the journal
An older local override may force banaction = iptables-multiport, even though this server uses firewalld. Remove that override and keep banaction = firewallcmd-rich-rules[actiontype=<multiport>], then verify that fail2ban-firewalld is installed and restart the service.
Hardening
- Use SSH keys and disable password authentication only after opening a second verified key-based session.
- Restrict SSH in firewalld to your office or VPN source when you have a stable administration network.
- Review
sudo fail2ban-client status sshdduring maintenance and keepignoreiplimited to trusted addresses. - The VPSLake SSH hardening generator can provide a starting policy to review before applying.
FAQ
Does Fail2Ban replace firewalld on AlmaLinux 9?
No. Firewalld remains the server’s general access-control layer, while Fail2Ban adds temporary source blocks after matching failed authentication events. You still need to allow only the services and ports your VPS requires.
Why are both CRB and EPEL enabled?
EPEL supplies Fail2Ban, while CRB supplies packages that EPEL software may need on AlmaLinux 9. Enabling CRB prevents dependency resolution from failing during installation.
Can I change the ban threshold later?
Yes. Edit maxretry to change the number of failures and findtime to change the counting window, then run sudo fail2ban-client -t and restart Fail2Ban. A lower threshold reacts sooner but increases the chance of banning an administrator who mistypes credentials.
How do I unban my own IP address?
First add the address to ignoreip so it is not banned again. Then replace ADMIN_IP below with the affected address:
sudo fail2ban-client set sshd unbanip ADMIN_IP
If SSH is unreachable, use the VPS control panel’s VNC console to update the jail and run the command locally.