AlmaLinux and Rocky Linux use the firewall firewalld, not ufw as Ubuntu does. Its standard zone public lets in only SSH, the Cockpit web interface (port 9090) and DHCPv6, and blocks everything else that comes in. Fail2ban blocks addresses that keep trying wrong logins; unlike on Ubuntu, you have to switch it on for SSH yourself. The commands were tested on a dedicated server with AlmaLinux 9 and in test machines with AlmaLinux 10 and Rocky Linux 9; run them as root.
1. Switch On the Firewall
dnf install firewalld systemctl enable --now firewalld firewall-cmd --state
After an installation of AlmaLinux 9 from the Client Area, firewalld is installed but switched off; dnf then only says that it is already installed. firewall-cmd --state answers running, and the firewall stays on after a restart. See what it lets in:
firewall-cmd --list-all
The line services: shows cockpit dhcpv6-client ssh: SSH keeps working when you switch the firewall on.
Use IPMI or noVNC for a dedicated server, or Console Access under Information → Console for a VPS, and run systemctl stop firewalld. Then correct the rule and start the firewall again.
2. Open a Port for a Program
For example a web server on port 8080:
firewall-cmd --permanent --add-port=8080/tcp firewall-cmd --reload
--permanent saves the rule, and --reload puts the saved rules into effect. A rule added without --permanent works at once but is gone after the next reload or restart. Use /udp for UDP. Well-known programs also have names: --add-service=http --add-service=https opens ports 80 and 443. To remove a rule, use the same words with remove:
firewall-cmd --permanent --remove-port=8080/tcp firewall-cmd --reload
If you do not use Cockpit, close its port too: firewall-cmd --permanent --remove-service=cockpit and firewall-cmd --reload.
Ports that Docker or Podman publish with -p are not controlled by firewalld. A container published on port 8080 can be reached from another server although the firewall has no rule for it. See Docker on AlmaLinux and Rocky Linux.
3. Allow SSH Only From Your Addresses
If you always connect from the same addresses (home, office, a VPN), allow SSH only from them. Add your addresses first, then remove the general rule (replace the address with yours; a whole range is written like 198.51.100.0/24):
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" service name="ssh" accept' firewall-cmd --permanent --remove-service=ssh firewall-cmd --reload
Keep your SSH window open and log in from a second window to check that you still get in; the login from the allowed address works at once. firewall-cmd --list-all shows the rule under rich rules:. Many home connections get a new address from time to time; you can check yours at myip.com. To allow everyone again: firewall-cmd --permanent --add-service=ssh and firewall-cmd --reload.
4. Block Password Guessing With Fail2ban
Fail2ban comes from EPEL, an extra package source of the Fedora project (see Install and Update Software on AlmaLinux and Rocky Linux):
dnf install epel-release dnf install fail2ban
After the installation, Fail2ban watches nothing: fail2ban-client status shows Number of jail: 0. Switch on the SSH jail in the file /etc/fail2ban/jail.local (create it with nano; your own address after ::1, so that you can never block yourself):
[DEFAULT] bantime = 1h ignoreip = 127.0.0.1/8 ::1 203.0.113.10 [sshd] enabled = true
Start Fail2ban, also after every restart, and check the SSH jail:
systemctl enable --now fail2ban fail2ban-client status sshd
Fail2ban reads the failed logins from the system journal. An address that fails 5 times within 10 minutes is blocked for the bantime, here one hour. Total failed counts the failed logins and Banned IP list shows the blocked addresses. Fail2ban blocks them with firewalld rules: a blocked address shows up in firewall-cmd --list-rich-rules as rule family="ipv4" source address="…" port port="ssh" protocol="tcp" reject, and the rule is gone after the unblocking.
If you blocked yourself, connect from another address or through the console and unblock your address:
fail2ban-client set sshd unbanip 203.0.113.10
With password login switched off, guessing passwords has no chance at all: Log In to Your Linux Server With an SSH Key. On AlmaLinux and Rocky Linux the command that applies the change is systemctl reload sshd.