Operating System CentOS

Advanced firewalld on AlmaLinux 9 and Rocky Linux 9: Zones, Sources and Rich Rules

A deeper firewalld guide for the RHEL 9 family: how zones are chosen, source-based access, custom services, IP sets, rich rules and safe testing.

This guide goes beyond opening a port. It explains how firewalld decides which zone handles a connection, and shows how to restrict services to specific source addresses, define your own services and zones, block address lists with IP sets, and write rich rules for logging, rate limiting and port forwarding. It is written for AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9. If you only need to install firewalld and allow a few services, start with the basic firewalld setup guide on this site.

Older tutorials on this topic covered CentOS 7, which reached end of life in June 2024. The firewall-cmd commands you know from there still work, but on the 9 releases firewalld generates nftables rules instead of iptables rules, and the old "direct" iptables interface should be replaced with rich rules or policies.

Requirements

  • A server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 with firewalld running. Check with sudo firewall-cmd --state, which should print running.
  • A user with sudo rights.
  • Access to your provider's web console as a fallback. Several examples restrict SSH, and a mistake there can lock you out of the server.

Runtime and permanent configuration

firewalld keeps two separate configurations. The runtime configuration is what the kernel enforces right now; it is lost on a reload or reboot. The permanent configuration is stored in /etc/firewalld/ and is loaded at start. A command without --permanent changes only the runtime; a command with --permanent changes only the stored configuration and has no effect until you reload.

That gives you two workflows. The first writes the permanent configuration and then activates it:

sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

The second is the one the firewalld developers recommend for anything risky: change the runtime only, test, and save when it works.

sudo firewall-cmd --zone=public --add-service=https
# test from another machine, then:
sudo firewall-cmd --runtime-to-permanent

If the test fails, sudo firewall-cmd --reload throws the runtime changes away and restores the saved state. Note that --runtime-to-permanent saves the entire runtime configuration, not only your last command.

For extra safety, add a timeout. The rule removes itself after the given time (s, m or h), and it cannot be combined with --permanent:

sudo firewall-cmd --zone=public --add-port=8443/tcp --timeout=10m

To compare both states, list them side by side:

sudo firewall-cmd --zone=public --list-all
sudo firewall-cmd --permanent --zone=public --list-all

How firewalld picks a zone

A zone is a set of rules with a trust level. Every incoming connection is handled by exactly one zone, chosen in this order:

  1. a zone that has the packet's source address bound to it;
  2. otherwise the zone the incoming interface is bound to;
  3. otherwise the default zone.

A zone is only "active" when at least one interface or source is bound to it. See what is active on your server:

sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --get-zones

The predefined zones range from distrustful to open:

ZoneBehaviour for incoming traffic
dropDropped without any reply; only outgoing connections work
blockRejected with an ICMP "prohibited" message
publicUntrusted network; only selected services accepted (the usual default)
externalLike public, with IPv4 masquerading enabled, for routers
dmzPublicly reachable hosts with limited access to the internal network
work, home, internalMostly trusted networks; selected services accepted
trustedEverything accepted

Each zone also has a target that decides what happens to packets matching none of its rules: default (reject, but allow ICMP), ACCEPT, REJECT or DROP. To make the public zone silently drop unsolicited traffic instead of rejecting it:

sudo firewall-cmd --permanent --zone=public --set-target=DROP
sudo firewall-cmd --reload

If the server has a second, private network interface, move it to a more trusted zone so internal services do not have to be opened to the internet. When NetworkManager manages the interface, firewalld stores the zone in the connection profile:

sudo firewall-cmd --permanent --zone=internal --change-interface=eth1
sudo firewall-cmd --reload
sudo firewall-cmd --get-zone-of-interface=eth1

Source-based rules: restrict a service to known addresses

Because source bindings win over interface bindings, you can give selected addresses their own zone with more access, while everyone else stays in public. A typical use is limiting SSH to an office network. Create a zone, bind the trusted range to it and allow SSH there:

sudo firewall-cmd --permanent --new-zone=mgmt
sudo firewall-cmd --permanent --zone=mgmt --add-source=198.51.100.0/24
sudo firewall-cmd --permanent --zone=mgmt --add-service=ssh
sudo firewall-cmd --reload

New zones can only be created with --permanent, so the reload is required. Connections from 198.51.100.0/24 are now handled by mgmt. Open a second SSH session from that range to confirm it works, and keep the first one open. Only then remove SSH from the public zone, first in the runtime configuration:

sudo firewall-cmd --zone=public --remove-service=ssh

Test again from inside and outside the trusted range. If the result is what you want, save it; if you locked yourself out, a reload from the provider's console or a reboot brings SSH back.

sudo firewall-cmd --runtime-to-permanent

A connection from a bound source is evaluated by its source zone, so the mgmt zone must list every service those addresses need, including web ports if the same people browse the sites on this server:

sudo firewall-cmd --permanent --zone=mgmt --add-service=http --add-service=https
sudo firewall-cmd --reload

Useful commands for source bindings:

sudo firewall-cmd --zone=mgmt --list-sources
sudo firewall-cmd --get-zone-of-source=198.51.100.0/24
sudo firewall-cmd --permanent --zone=mgmt --remove-source=198.51.100.0/24

Services: use names instead of port numbers

A service is a named definition of ports and protocols. Predefined ones live in /usr/lib/firewalld/services/; list them and inspect one:

sudo firewall-cmd --get-services
sudo firewall-cmd --info-service=https

For your own applications, define a service once and reuse it in zones and rich rules. Custom definitions are saved in /etc/firewalld/services/:

sudo firewall-cmd --permanent --new-service=myapp
sudo firewall-cmd --permanent --service=myapp --set-description="Internal API"
sudo firewall-cmd --permanent --service=myapp --add-port=8443/tcp
sudo firewall-cmd --permanent --service=myapp --add-port=9000-9010/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --zone=public --add-service=myapp
sudo firewall-cmd --reload

If the application's ports change later, you update the service definition and every zone that uses it follows.

IP sets: block or allow many addresses at once

An IP set is a named list of addresses or networks that can be used as a source. To block a list of abusive networks, create a set and bind it to the drop zone:

sudo firewall-cmd --permanent --new-ipset=blocklist --type=hash:net
sudo firewall-cmd --permanent --ipset=blocklist --add-entry=203.0.113.0/24
sudo firewall-cmd --permanent --ipset=blocklist --add-entry=198.51.100.77
sudo firewall-cmd --permanent --zone=drop --add-source=ipset:blocklist
sudo firewall-cmd --reload

Larger lists can be loaded from a text file with one entry per line; lines starting with # or ; are ignored:

sudo firewall-cmd --permanent --ipset=blocklist --add-entries-from-file=/root/blocklist.txt
sudo firewall-cmd --reload

Inspect or shrink the set with --info-ipset=blocklist and --ipset=blocklist --remove-entry=....

Rich rules

Rich rules cover what plain service and port entries cannot express: a condition on source or destination combined with an action, logging and rate limits. The general form is:

rule [family="ipv4|ipv6"] [priority="N"]
  [source address="..."] [destination address="..."]
  service|port|protocol|forward-port|...
  [log ...] [accept|reject|drop]

The whole rule is passed as one quoted argument. Like every other setting, it can be added to the runtime first and saved later.

Allow a port from one address only

sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="198.51.100.25" port port="5432" protocol="tcp" accept'

Reject or drop a single address

sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="203.0.113.50" drop'
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="203.0.113.51" reject type="icmp-admin-prohibited"'

family is mandatory whenever you use an address or a reject type.

Log and rate-limit

This rule accepts SSH from one network and writes at most three log entries per minute with a recognisable prefix:

sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="198.51.100.0/24" service name="ssh" log prefix="ssh-office " level="info" limit value="3/m" accept'

A limit can also be placed on the action itself. The next rule accepts at most ten new connections per minute to the custom service; put the limit after accept, not after log:

sudo firewall-cmd --zone=public --add-rich-rule='rule service name="myapp" accept limit value="10/m"'

For this to have an effect, myapp must not also be allowed as a plain service in the same zone. Rates use s, m, h or d as the time unit. Log entries go to the kernel log; read them with sudo journalctl -k.

Forward a port

sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" forward-port port="8080" protocol="tcp" to-port="80"'

Order and priority

Rich rules are not processed in the order you add them. With the default priority 0, firewalld sorts them by action: log rules first, then reject and drop rules, then accept rules. A deny rule therefore beats an allow rule for the same traffic. To force a different order, set priority between -32768 and 32767; lower numbers run first, negative values run before the zone's ordinary service and port entries, and positive values after them:

sudo firewall-cmd --zone=public --add-rich-rule='rule priority="-10" family="ipv4" source address="198.51.100.25" accept'

List, remove and save

sudo firewall-cmd --zone=public --list-rich-rules
sudo firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="203.0.113.50" drop'
sudo firewall-cmd --runtime-to-permanent

To remove a rule, pass exactly the text shown by --list-rich-rules.

Verify and troubleshoot

  • See everything at once: sudo firewall-cmd --list-all-zones prints every zone; --get-active-zones shows which ones actually receive traffic.
  • Validate the saved files: sudo firewall-cmd --check-config checks the permanent configuration before you reload.
  • Find out what is being refused: turn on logging of denied packets, reproduce the problem, read the kernel log, and turn it off again.
sudo firewall-cmd --set-log-denied=unicast
sudo journalctl -k --since "5 minutes ago"
sudo firewall-cmd --set-log-denied=off
  • A rule has no effect: check that it is in the zone that handles the traffic. A source binding sends the connection to a different zone than the interface's zone.
  • A rule disappeared after reboot: it was only in the runtime configuration. Add it again and run --runtime-to-permanent.
  • Emergency stop: sudo firewall-cmd --panic-on drops all incoming and outgoing packets, including your SSH session. Use it only from the provider's console, and end it with --panic-off.

Frequently asked questions

Should I use a rich rule or a separate zone to restrict a service?

For one port and one or two addresses, a rich rule in the existing zone is the quickest solution. When a group of addresses needs a different set of services as a whole (administrators, a monitoring system, a private network), a zone with source bindings is easier to read and maintain, because the zone lists everything that group may reach.

Can I still add raw iptables rules with --direct?

The direct interface still exists but is deprecated, and on the 9 releases its rules live apart from the nftables rules firewalld generates for zones, which makes the result hard to reason about. Express the rule as a rich rule, or as a policy if it concerns traffic flowing between zones.

What are firewalld policies?

Zones describe traffic arriving at the server. Policies filter traffic that flows from one zone to another, for example from an internal network through the server to the internet, or traffic the host itself sends out. They are created with --permanent --new-policy and accept the same services, ports and rich rules as zones. On a single web server you rarely need them; on a router or VPN gateway they are the right tool.

Do Docker or Podman containers bypass these rules?

Container engines add their own packet filter rules and, in Docker's case, their own firewalld zone. A port published by a container can therefore be reachable even though you never opened it in public. Check published ports with the container tool and bind them to 127.0.0.1 when they should not be public.

Share story:

Continue reading other guides on How VPS.