Set up a firewall with firewalld (AlmaLinux, Rocky, Fedora)
GoGetSpace Engineering Last tested 6 min readTested onAlmaLinux 9Fedora 45
Short answer
firewalld is the standard firewall on AlmaLinux, Rocky Linux and Fedora. It isn't installed on our servers, so install and start it with sudo dnf install -y firewalld and sudo systemctl enable --now firewalld. It allows SSH from the start. Open a port with sudo firewall-cmd --permanent --add-port=3000/tcp, apply it with sudo firewall-cmd --reload, and check with sudo firewall-cmd --list-all.
We tested on AlmaLinux 9 and Fedora 45. Rocky Linux 9 is built the same way as AlmaLinux 9, so the steps and output are the same.
firewalld isn't installed on our AlmaLinux, Rocky or Fedora images, so any program that listens on a port can be reached from the internet until you turn it on.
The second command may print nothing. That's normal.
2
Check what's allowed:
Bash
$ sudofirewall-cmd--list-all
Output
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: cockpit dhcpv6-client ssh
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
ssh is in services, so SSH stays open. On Fedora the first line says public (default, active), there are two extra lines (ingress-priority: 0 and egress-priority: 0), and the services are dhcpv6-client mdns ssh.
3
Open a new terminal window and connect to your server again. If it connects, you're done. If it doesn't, run sudo systemctl disable --now firewalld in your first window, then see If you lock yourself out.
Permanent rules and reload
firewalld keeps two sets of rules: the ones running now, and the permanent ones it loads at boot. Add --permanent to save a rule, then run --reload to make it take effect:
Each command prints success. A --permanent change doesn't show in --list-all until you run --reload.
A rule without --permanent works straight away, but it disappears at the next reload or restart. That's the most common reason for "my port closed again".
Open the ports your app needs
firewalld knows common programs by name, called services. Use the name when there is one, and the port number when there isn't:
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: cockpit dhcpv6-client http https ssh
ports: 3000/tcp
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
To see the more than 200 service names firewalld knows, run sudo firewall-cmd --get-services.
Allow a port only from your own IP
Databases and admin panels shouldn't be open to everyone. A rich rule allows one address only. First find your IP: run echo $SSH_CLIENT on the server, and the first part is the IP your server sees you connecting from.
Besides ssh, firewalld allows a few services from the start:
cockpit (AlmaLinux and Rocky): port 9090, for the Cockpit web admin tool. Cockpit isn't installed on our images, so nothing answers there. Removing it is optional and keeps the list tidy: sudo firewall-cmd --permanent --remove-service=cockpit, then reload.
dhcpv6-client (all three) and mdns (Fedora only): used for network setup and local name lookups. Leave them.
Zones in short
firewalld groups rules into zones. Your server uses the public zone, and every command on this page changes it. You can check:
Bash
$ sudofirewall-cmd--get-active-zones
Output
public
interfaces: eth0
On Fedora the first line is public (default).
Docker and firewalld
When you publish a port with Docker (for example -p 8080:80), Docker adds its own rules that skip firewalld's public zone, and a new docker zone appears in --get-active-zones. The port is open to the internet even though firewalld doesn't allow it. We tested it: with no rule for port 8080, the container answered from outside.
To keep a container private, publish it on 127.0.0.1 only, then put a web server such as Nginx in front of it:
Bash
$ sudodocker run -d-p127.0.0.1:8080:80 nginx
If you lock yourself out
If firewalld no longer allows SSH, a window that's already connected keeps working, but a new connection fails straight away:
Output
ssh: connect to host 203.0.113.10 port 22: No route to host
It looks like the server is down, but it isn't: firewalld is refusing the connection. If you still have a connected window, add SSH back there. If not, use the browser console:
1
Open your server's page and start the browser console. Confirm with your account password or 2FA code. We email the account owner when a console session opens.
2
Click One-time password. It takes about a minute to be ready. Then sign in with it as your login user: almalinux, rocky or fedora.
If you're not sure which rule is the problem, sudo systemctl stop firewalld turns the firewall off until the next restart. Fix the rule, then turn it back on with sudo systemctl start firewalld and check SSH from a new window.
Not on our servers. It isn't installed on our AlmaLinux, Rocky or Fedora images. Until you turn on a firewall, any program that listens on a port can be reached from the internet.
Why did my rule disappear?
It was added without --permanent, so it only lasted until the next reload or restart. Add it again with --permanent, then run sudo firewall-cmd --reload.
Does firewalld stay on after a restart?
Yes, after sudo systemctl enable --now firewalld. Permanent rules load again at every boot.
Why does SSH say "No route to host"?
firewalld refuses blocked connections at once, so you get No route to host (SSH) or Couldn't connect to server (curl). The server is running. A port you allowed but where nothing is running yet waits and times out instead.
Can I use UFW on AlmaLinux instead?
It's possible, but we don't recommend it. firewalld is the standard on AlmaLinux, Rocky and Fedora. Use one firewall, not both.
Do I need a firewall if nothing else is running?
It's still worth it. Many programs start listening on a port when you install them, and a firewall keeps them private until you decide to open them.