How-to

How to Enable and Use firewalld on CentOS Stream and Rocky

firewalld is the stock firewall on the RHEL family. It groups rules into zones, names common port sets as services and can change rules live without dropping connections.

Is firewalld already installed?

On CentOS Stream 9, AlmaLinux 9 and Rocky Linux 9 it nearly always is. Check and start it:

rpm -q firewalld || sudo dnf install -y firewalld
sudo systemctl enable firewalld --now
sudo firewall-cmd --state

The default zone allows SSH and DHCPv6 client traffic and a cockpit entry on some images, so turning firewalld on does not cut an SSH session on port 22. If you moved SSH to another port, add that port before enabling, or you lock yourself out.

What are zones and which one am I in?

A zone is a trust level with its own list of allowed services and ports. Each network interface belongs to one zone, and traffic from a source address can be routed into another zone. The common ones:

ZoneDefault behavior
publicDefault on servers; only selected services allowed
dropDrops everything incoming with no reply
blockRejects incoming with an ICMP message
trustedAccepts everything
internal, home, workLooser presets meant for private networks
firewall-cmd --get-default-zone
ip -br link; sudo firewall-cmd --get-active-zones     # interface to zone map
sudo firewall-cmd --zone=public --list-all

On a server with one public interface you will mostly work in public. Leave the default alone unless you have a reason.

How do runtime and permanent rules differ?

firewalld keeps two configurations. Runtime is what the kernel enforces right now. Permanent is what loads at the next reload or boot. A command without --permanent changes runtime only. That split is handy: try a rule live, check it works, then save it.

sudo firewall-cmd --add-service=http                  # live only
sudo firewall-cmd --runtime-to-permanent              # save the live set
sudo firewall-cmd --permanent --add-service=smtp-submission   # saved only
sudo firewall-cmd --reload                            # load saved set

A reload throws away runtime changes that were never saved. If a rule disappears after a reload, it was never made permanent.

How do I allow services and ports?

sudo firewall-cmd --get-services | tr ' ' '\n' | grep -E 'sql|http'
sudo firewall-cmd --permanent --zone=public --add-service={http,https} --add-port=8080/tcp
sudo firewall-cmd --permanent --add-port=25565/tcp
sudo firewall-cmd --permanent --zone=public --remove-service=cockpit --remove-service=dhcpv6-client
sudo firewall-cmd --reload && sudo firewall-cmd --list-services

Prefer services where one exists, since the name documents the rule. The brace syntax is the shell expanding two flags, not a firewalld feature. Remove services you do not use; cockpit is a web admin panel on port 9090 that many people never touch.

How do I allow a port only from trusted addresses?

Two clean ways. A rich rule:

sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.0.2.44" service name="mysql" accept'

Or a source binding to a dedicated zone, which scales better when several ports are involved:

sudo firewall-cmd --permanent --new-zone=office
sudo firewall-cmd --permanent --zone=office --add-source=192.0.2.0/27
sudo firewall-cmd --permanent --zone=office --add-service={ssh,mysql,postgresql}
sudo firewall-cmd --reload; sudo firewall-cmd --zone=office --list-all

Traffic from that range now uses the office zone, while everyone else stays in public. You can then remove SSH from public entirely if your source addresses are fixed.

Can firewalld forward a port?

sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080
sudo firewall-cmd --permanent --add-masquerade
sudo firewall-cmd --reload; sudo firewall-cmd --list-forward-ports

The first line redirects port 80 to a local app on 8080. Masquerade is only needed when you forward to another host or to containers and VMs behind NAT, such as KVM guests on the box.

How do I block one abusive address?

sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.77" drop'
sudo firewall-cmd --permanent --new-ipset=blocklist --type=hash:net
sudo firewall-cmd --permanent --ipset=blocklist --add-entry=198.51.100.0/24
sudo firewall-cmd --permanent --zone=drop --add-source=ipset:blocklist

For more than a handful of addresses use an ipset; it stays fast with thousands of entries. For automatic bans after failed logins, pair firewalld with Set up fail2ban, which supports it out of the box.

Does firewalld work with Docker?

Docker writes its own rules and, on current releases, creates a docker zone in firewalld. Ports you publish with -p are reachable even if your public zone does not list them. Bind containers to 127.0.0.1 when they should stay private. If you need to stop the firewall to debug something, Stop firewalld on CentOS Stream covers stopping it safely and turning it back on.

Budget Machines for RHEL Family Servers

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E2Intel Xeon E3-1270 v34 / 832 GB DDR31x 480 GB SSD2x 1 Gbps$38.32$114.96 (3 months)
S2Intel Xeon E-2356G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$58.39$58.39 (1 month)
S4Intel Xeon E-2388G8 / 1664 GB DDR41x 1 TB NVMe2x 10 Gbps$87.52$87.52 (1 month)

Keep Reading

Stop firewalld on CentOS Stream

Stop, disable or mask firewalld on CentOS Stream 9, Rocky Linux and AlmaLinux, and what to run instead so the box stays protected.

Open

Open a port in Linux

Open a TCP or UDP port on Ubuntu, Debian, AlmaLinux or Rocky with ufw, firewalld or nftables, then prove it works from outside.

Open

Set up fail2ban

Install fail2ban, write a jail.local for SSH and nginx, read the ban list and unban an address you locked out by mistake.

Open

Rocky Linux

Rocky Linux 9, a free Red Hat compatible rebuild focused on staying bug for bug identical to its upstream.

Open

FAQ

Quick Answers

Not on port 22, since the public zone allows SSH. Add a custom SSH port before enabling.

It was a runtime rule. Add it with --permanent or save live rules with --runtime-to-permanent.

nftables, on all current RHEL family releases.

Run sudo firewall-cmd --list-all-zones, or sudo nft list ruleset for the kernel view.

Yes, it is in the Ubuntu archive, but ufw is the usual choice there. Do not run both.

Not in any way you will notice. Rule matching in nftables is very cheap even on 10 or 25 Gbps ports.