How to Configure firewalld on CentOS, Rocky Linux and AlmaLinux
firewalld is the firewall of CentOS, Rocky Linux, AlmaLinux and RHEL: zones, services and ports, permanent rules, SSH for one address, common errors.
3 min readBy Smita
Reviewed by the AmazingRDP technical team

A server to secure your way. A Linux VPS gives you root access. See Linux VPS from $6.99
firewalld is the firewall manager on the Red Hat family of systems: RHEL, CentOS Stream, Rocky Linux, AlmaLinux and Fedora. It is controlled with one command, firewall-cmd, and built around two ideas: zones and the difference between runtime and permanent rules. Once those are clear, the rest is short.
A note on the name in the title: CentOS Linux 7 and 8 are no longer maintained. Everything here applies unchanged to their successors: see Rocky Linux vs CentOS.
Is it running?
sudo firewall-cmd --state
sudo systemctl enable --now firewalld
If it is not installed: sudo dnf install firewalld. Over SSH there is no risk in starting it; the default zone allows SSH.
See what is allowed
sudo firewall-cmd --list-all
This shows the active zone, the interfaces in it, and the services and ports it lets in. Everything not listed is refused.
Runtime and permanent
This is the part that catches everyone once.
- A command without
--permanenttakes effect at once and is forgotten at the next reload or restart. - A command with
--permanentis saved and does nothing until you reload.
So the normal pattern is two lines:
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
The other way round also works: try rules without --permanent, and when they are right, keep them all with sudo firewall-cmd --runtime-to-permanent. That is the safer habit on a remote server, since a mistake disappears with a restart.
Services and ports
sudo firewall-cmd --get-services
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --permanent --add-port=5000-5010/udp
sudo firewall-cmd --permanent --remove-service=cockpit
sudo firewall-cmd --reload
A service is a name for one or more ports (http is 80, https is 443). Use the name where one exists and the port number otherwise.
Zones
A zone is a set of rules for a level of trust. Every network interface belongs to one zone.
| Zone | Lets in by default |
|---|---|
public (the default) | SSH and little else |
home, internal, work | A few more services for trusted networks |
trusted | Everything |
drop | Nothing; no answer is sent |
block | Nothing; a refusal is sent |
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --zone=internal --list-all
A server with one interface on the internet needs only public. Zones earn their keep when a server has a second, private interface.
Allow something for one address only
The cleanest way is to put the trusted address into a zone of its own:
sudo firewall-cmd --permanent --zone=internal --add-source=203.0.113.25
sudo firewall-cmd --permanent --zone=internal --add-service=ssh
sudo firewall-cmd --permanent --zone=public --remove-service=ssh
sudo firewall-cmd --reload
SSH now answers only that address. Before the last two lines, be certain the address is yours and does not change, and keep the provider's console within reach.
The same can be written as a "rich rule":
sudo firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=203.0.113.25 port port=3306 protocol=tcp accept'
Block an address
sudo firewall-cmd --permanent --zone=drop --add-source=198.51.100.7
sudo firewall-cmd --reload
If you lock yourself out
Open the server's console in the provider's control panel and run sudo firewall-cmd --add-service=ssh. If the bad rule was not permanent, a restart of the server also clears it.
Problems
| Sign | Cause |
|---|---|
| The rule vanished after a restart | --permanent was missing |
| The rule was added and nothing changed | --reload was missing |
| The port is open and still not reachable | No program is listening on it, or it listens on 127.0.0.1 only: see listing open ports on Linux. A provider firewall may also sit in front |
| A service on an unusual port is refused although the firewall allows it | SELinux; the audit log names it |
| ALREADY_ENABLED | A warning only: the rule exists |
Related
The Ubuntu counterpart is ufw: open or block a port in the Ubuntu firewall. A web server behind this firewall: HTTPS for Nginx with Let's Encrypt. Moving SSH to another port is covered for Ubuntu in changing the SSH port. For a server of your own, see our Linux VPS plans.
Questions
How do I open a port in firewalld?
Run sudo firewall-cmd --permanent --add-port=8080/tcp and then sudo firewall-cmd --reload.
What is the difference between runtime and permanent rules?
Runtime rules apply at once and are lost on reload or restart. Permanent rules are saved and apply after a reload.
What is a zone in firewalld?
A named set of rules for a level of trust. Each network interface or source address belongs to one zone.
How do I see the current firewalld rules?
Run sudo firewall-cmd --list-all.