Security

Firewalld Cheat Sheet: Install and Use on Ubuntu 26.04 / 24.04

A rule added with firewall-cmd --add-service=http works the moment you type it and vanishes on the next reload. Firewalld keeps two configurations, and that command only touched one of them. Most firewalld surprises come from that split, from zones that match traffic by source before interface, and from Docker publishing ports that never pass your zone rules at all. This firewalld cheat sheet puts the firewall-cmd commands you’ll actually type into tables first, then shows each one against real traffic.

Original content from computingforgeeks.com - post 3520

You’ll install firewalld on Ubuntu, retire UFW so the two don’t fight over nftables, and then work through zones, services and ports, rich rules, port forwarding with masquerade, ipsets, policies, panic mode and the Docker fix. Every command below was run on Ubuntu 26.04 (firewalld 2.3.1), Ubuntu 24.04 (2.1.1) and Rocky Linux 10 (2.4.3) in November 2026, with a second VM sending the test traffic. Debian 13 users have a matching walkthrough in the guide to install and configure firewalld on Debian, which covers zones, services, rich rules, forwarding and panic mode.

Firewalld cheat sheet: firewall-cmd commands

Nearly every row below ran verbatim on the lab VMs. Without --permanent a command changes only the running firewall, and with it only the saved one, so the runtime column you see in --list-all and the files under /etc/firewalld/ can disagree until you reload or save. Each command acts on the default zone unless you add --zone=NAME.

Install, status and reload

CommandWhat it does
sudo apt install -y firewalldInstall on Ubuntu (enables and starts it)
sudo systemctl disable --now ufwStop UFW so only one tool owns the rules
sudo firewall-cmd --statePrints running when the daemon is up
sudo firewall-cmd --versionfirewalld version (needs root on Ubuntu)
sudo firewall-cmd --reloadLoad the permanent config, drop runtime-only rules
sudo firewall-cmd --complete-reloadFull reset; can drop active connections
sudo firewall-cmd --check-configValidate the XML files under /etc/firewalld
sudo nft list tablesShows table inet firewalld on the nftables backend

Zones

CommandWhat it does
sudo firewall-cmd --get-default-zoneZone for traffic no other zone claims
sudo firewall-cmd --set-default-zone=publicChange the default zone
sudo firewall-cmd --get-active-zonesZones bound to an interface or source
sudo firewall-cmd --list-allEverything allowed in the default zone
sudo firewall-cmd --list-all-zonesThe same for every zone
sudo firewall-cmd --zone=public --change-interface=ens4 --permanentBind an interface to a zone
sudo firewall-cmd --zone=internal --add-source=10.0.1.0/24Put a subnet in a zone (wins over interface)
sudo firewall-cmd --get-zone-of-source=10.0.1.20/32Which zone a source lands in
sudo firewall-cmd --new-zone=appnet --permanentCreate a custom zone

Services and ports

CommandWhat it does
sudo firewall-cmd --get-servicesPredefined service names
sudo firewall-cmd --info-service=httpPorts and modules behind a service
sudo firewall-cmd --add-service=httpOpen a service in the runtime config
sudo firewall-cmd --add-service={http,https} --permanentOpen several services on disk
sudo firewall-cmd --add-port=8080/tcpOpen a port
sudo firewall-cmd --add-port=60000-61000/udpOpen a port range
sudo firewall-cmd --remove-port=8080/tcpClose a port
sudo firewall-cmd --query-service=httpsyes or no, exit code 0 or 1
sudo firewall-cmd --new-service=myapp --permanentDefine your own service
sudo firewall-cmd --service=myapp --add-port=9000/tcp --permanentGive it a port

Runtime vs permanent

CommandWhat it does
sudo firewall-cmd --runtime-to-permanentSave the running rules to disk
sudo firewall-cmd --permanent --list-allWhat survives the next reload or boot
sudo firewall-cmd --list-services --permanentSaved services only
sudo firewall-cmd --add-service=http --timeout=60sTemporary rule that removes itself

Rich rules

CommandWhat it does
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" service name="postgresql" accept'Service for one subnet
sudo firewall-cmd --add-rich-rule='rule service name="ssh" accept limit value="10/m"'Rate-limit new SSH connections
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" reject type="icmp-port-unreachable"'Reject a network
sudo firewall-cmd --list-rich-rulesRich rules in the zone
sudo firewall-cmd --remove-rich-rule='RULE'Remove one, quoted exactly as listed

Forwarding and NAT

CommandWhat it does
sudo firewall-cmd --add-forward-port=port=8081:proto=tcp:toport=80Redirect a port on this host
sudo firewall-cmd --add-forward-port=port=8088:proto=tcp:toport=80:toaddr=10.0.1.10Forward to another host
sudo firewall-cmd --add-masqueradeSNAT forwarded traffic so replies return
sudo firewall-cmd --query-masqueradeIs masquerading on
sudo firewall-cmd --list-forward-portsActive port forwards

IP sets

CommandWhat it does
sudo firewall-cmd --new-ipset=blocklist --type=hash:net --permanentCreate a set of networks
sudo firewall-cmd --ipset=blocklist --add-entry=198.51.100.0/24Add one entry
sudo firewall-cmd --ipset=blocklist --add-entries-from-file=bad-ips.txtBulk load, one per line
sudo firewall-cmd --ipset=blocklist --get-entriesList entries
sudo firewall-cmd --zone=drop --add-source=ipset:blocklistSilently drop everything in the set
sudo firewall-cmd --delete-ipset=blocklist --permanentDelete the set

Policies

CommandWhat it does
sudo firewall-cmd --get-policiesList policy objects
sudo firewall-cmd --new-policy=host-egress --permanentCreate a policy
sudo firewall-cmd --policy=host-egress --add-ingress-zone=HOST --permanentTraffic leaving this host
sudo firewall-cmd --policy=host-egress --add-egress-zone=ANY --permanentTo any destination zone
sudo firewall-cmd --info-policy=host-egressShow a policy

ICMP, panic mode and logging

CommandWhat it does
sudo firewall-cmd --add-icmp-block=echo-requestStop answering ping
sudo firewall-cmd --panic-onDrop every packet in and out
sudo firewall-cmd --panic-offBack to normal
sudo firewall-cmd --query-panicyes while panic mode is on
sudo firewall-cmd --set-log-denied=unicastLog rejected and dropped unicast packets

The rest of the page is the long form: why each group behaves the way it does on Ubuntu and Rocky Linux, and the output that proves it. If you only need UFW, the UFW commands cheat sheet is the sibling to this page.

Install firewalld on Ubuntu 26.04 / 24.04

Ubuntu ships UFW, and the cloud images we tested had the ufw service enabled even though its status read inactive. Firewalld and UFW both write nftables rules, so running both leaves you with two rule sets that each have to allow a packet. Pick one. If UFW is already active on a server you reach over SSH, install firewalld first and make sure ssh is in its default zone (it is by default) before you turn UFW off, because the order decides whether your session survives the switch. Check what UFW is doing first. A server where UFW is enabled and active needs its allowed ports copied into firewalld before the switch, so write down what ufw status lists before you go further:

sudo ufw status
systemctl is-enabled ufw

On both Ubuntu releases the service was enabled while the firewall itself was off, which means nothing was filtering yet and nothing needs copying:

Status: inactive
enabled

The steps to install firewalld Ubuntu-side are the same on both LTS releases: one package from the archive. It enables and starts the daemon in the same step, so there’s no separate systemctl enable to run. A host you reach over SSH keeps its session through the install, because ssh is in the default zone from the first second the daemon runs:

sudo apt update
sudo apt install -y firewalld

The last lines of the install show systemd wiring the unit in. From this point the host is filtered by the public zone, which allows only SSH and DHCPv6 client traffic, so any web or database port you relied on is closed until you open it:

Created symlink '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service' → '/usr/lib/systemd/system/firewalld.service'.

Then disable UFW. The package install does not touch it, and an enabled ufw.service will load its own rules at the next boot even if you never ran ufw enable:

sudo systemctl disable --now ufw

Confirm firewalld is running and see which version your release carries. The version matters more than usual here, because Ubuntu 24.04 and 26.04 ship different major features, so note it before you copy a fix from anywhere, including this page:

sudo firewall-cmd --state
sudo firewall-cmd --version

Ubuntu 26.04 printed running and the version, so the daemon is answering on D-Bus:

running
2.3.1

Ubuntu 24.04 reported 2.1.1-1 from dpkg -l. That gap matters later: the StrictForwardPorts setting that fixes the Docker bypass is missing from the older firewalld Ubuntu 24.04 package. Run firewall-cmd without sudo and you get a polkit error instead of a version, which is covered in the errors section at the end.

firewalld installed and running on Ubuntu 26.04 with the nftables backend

The screenshot above is the same check on web01, plus the backend and zone state covered next. Run it on every new host before you add rules, because a daemon that is installed but stopped accepts every command and filters nothing, so the rules you type look fine while the host stays wide open.

Check the nftables backend

Firewalld on all three systems used nftables, not iptables. The setting lives in /etc/firewalld/firewalld.conf, which is readable only by root on Ubuntu:

sudo grep -E '^(FirewallBackend|DefaultZone|StrictForwardPorts)=' /etc/firewalld/firewalld.conf
sudo nft list tables

Ubuntu 26.04 returned the defaults below, and nft shows the single table firewalld owns:

DefaultZone=public
FirewallBackend=nftables
StrictForwardPorts=no
table inet firewalld

If nft list tables shows extra ip filter or ip nat tables, another tool is writing rules beside firewalld. Netfilter runs every table that hooks a packet, so a packet has to pass both sets of rules, and an accept in one table does not override a drop in another. On our box that extra writer was Docker, and it is the reason the Docker section exists.

Set up firewalld zones

A zone is a trust level with its own list of allowed services and ports. Every packet is matched to exactly one zone: first by its source address if a zone claims that source, then by the incoming interface, and otherwise by the default zone. That order is why adding a source to a zone can take services away from a host that had them a second ago.

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

A fresh Ubuntu 26.04 install answers with public, allowing only SSH and DHCPv6 client traffic. Everything else that arrives gets a reject, so a service that listened fine under an inactive UFW stops answering the moment firewalld starts, which is the first thing to check after the install:

public
public (default)
public (default, active)
  target: default
  ingress-priority: 0
  egress-priority: 0
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

Look at the empty interfaces: line. Ubuntu server uses netplan with systemd-networkd, not NetworkManager, so nothing tells firewalld which zone ens4 belongs to and --get-zone-of-interface=ens4 answers no zone. Traffic is still filtered, because unclaimed traffic falls into the default zone. Bind the interface explicitly anyway, so changing the default zone later doesn’t silently move your NIC:

sudo firewall-cmd --zone=public --change-interface=ens4 --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --get-active-zones

The interface now shows under the zone. The binding is stored in the zone file, so it survives a reboot even though netplan knows nothing about firewalld zones:

public (default)
  interfaces: ens4

Rocky Linux 10 runs NetworkManager, so eth0 was already listed under its zone. One thing caught us there: the Rocky 10 cloud image we used shipped with DefaultZone=trusted, which accepts everything. A stock Rocky install defaults to public, so check --get-default-zone on any cloud image before you trust it.

Built-in zones and what they do

Nine zones ship with firewalld. The three that differ most are the targets, not the service lists:

ZoneDefault behaviourUse it for
dropTarget DROP: no reply at allBlocklists, hostile sources
blockTarget %%REJECT%%: ICMP prohibited replyBlocking with a clear refusal
publicssh, dhcpv6-client (Rocky adds cockpit)Internet-facing interfaces
internalssh, mdns, samba-client, dhcpv6-clientTrusted LAN segments
trustedTarget ACCEPT: everything allowedLoopback-like links, VPN peers you own
externalssh, masquerade onThe outside leg of a router
dmzssh onlyHosts reachable from outside
homessh, mdns, samba-client, dhcpv6-clientHome LANs
workssh, dhcpv6-clientOffice LANs

Every row comes from --zone=NAME --list-all on web01 (Rocky adds cockpit to public). Use drop for sources you never want to answer, because a REJECT tells a scanner the host is alive.

Move a source into another zone

This is the source rule in action, and it is the zone behaviour that breaks working setups most often. Web01 allowed HTTP in public. Putting db01’s address into internal moved db01 into a zone without HTTP, so its curl failed immediately even though nothing about public had changed:

sudo firewall-cmd --zone=internal --add-source=10.0.1.20/32
sudo firewall-cmd --get-active-zones

Both zones are now active, one by source and one by interface. Db01 matches the source first, so for db01 the public rules no longer exist:

internal
  sources: 10.0.1.20/32
public (default)
  interfaces: ens4

The screenshot puts the whole sequence on one screen, with the client side marked. Read the active zones first, because a source line under any zone means some clients no longer see the default zone at all, and the rest of the troubleshooting follows from that. The --get-zone-of-source line confirms it for one address, which is quicker than reading every zone when a single client complains:

firewalld source binding moves a client into the internal zone and blocks HTTP on Ubuntu 26.04

The fix is to add the service to the zone the source now lives in (--zone=internal --add-service=http), after which db01 got 200 again. So whenever a host loses access right after you add a source binding, list that zone with --zone=NAME --list-all before you touch anything else. Remove the binding with --remove-source when you are done testing.

Create a custom zone

A custom zone is useful when a group of hosts needs one port nobody else gets. The zone is created on disk, so every step takes --permanent and needs a reload:

sudo firewall-cmd --new-zone=appnet --permanent
sudo firewall-cmd --zone=appnet --add-source=10.0.1.0/24 --permanent
sudo firewall-cmd --zone=appnet --add-port=5432/tcp --permanent
sudo firewall-cmd --reload

Firewalld writes the zone as a small XML file you can keep in Git or push with Ansible:

sudo cat /etc/firewalld/zones/appnet.xml

The file holds only what you added. Anything you leave out, such as a target, falls back to the default, so the zone rejects every other port from that subnet:

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <port port="5432" protocol="tcp"/>
  <source address="10.0.1.0/24"/>
</zone>

Delete it with --delete-zone=appnet --permanent followed by a reload. The delete only removes the file, so the zone keeps filtering in runtime until that reload, which is easy to miss when you are cleaning up a test.

Open services and ports with firewall-cmd

Prefer a service name over a raw port when one exists. Ubuntu 26.04 ships 259 predefined services, each an XML file that can carry several ports and helper modules, and --info-service tells you exactly what you’re opening:

sudo firewall-cmd --info-service=http

For http that is a single TCP port, so opening the service and opening 80/tcp are equivalent here:

http
  ports: 80/tcp
  protocols: 
  source-ports: 
  modules: 
  destination: 
  includes: 
  helpers: 

Before any change, curl from db01 to web01’s nginx was refused, since public doesn’t list http. One command opened it:

sudo firewall-cmd --add-service=http

Db01 then got the nginx page, with no restart of firewalld or nginx and no drop of existing connections:

200

Ports work the same way, with the protocol always required. A range uses a hyphen:

sudo firewall-cmd --add-port=8080/tcp
sudo firewall-cmd --add-port=60000-61000/udp
sudo firewall-cmd --list-ports

Both show up in the running zone. Neither is saved yet, so a reload right now would close them both, which is fine while you test and is exactly what the next section deals with:

8080/tcp 60000-61000/udp

When nothing fits a predefined service, define your own so the zone listing stays readable. A service named myapp reads better six months later than a bare 9000/tcp:

sudo firewall-cmd --new-service=myapp --permanent
sudo firewall-cmd --service=myapp --add-port=9000/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --info-service=myapp

The new service is ready to add to any zone, and changing its port later updates every zone that uses it in one place:

myapp
  ports: 9000/tcp
  protocols: 
  source-ports: 
  modules: 
  destination: 
  includes: 
  helpers: 

To stop answering ping without touching anything else, block the ICMP type instead of dropping ICMP wholesale, which also breaks path MTU discovery:

sudo firewall-cmd --add-icmp-block=echo-request

Ping from db01 then got an explicit filter reply rather than a timeout, so monitoring that pings this host will report it as filtered, not down:

PING 10.0.1.10 (10.0.1.10) 56(84) bytes of data.
From 10.0.1.10 icmp_seq=1 Packet filtered
From 10.0.1.10 icmp_seq=2 Packet filtered

--- 10.0.1.10 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1001ms

Remove it with --remove-icmp-block=echo-request. Block only echo-request, because types such as fragmentation-needed carry path MTU information, and dropping them produces connections that open fine and then hang on large transfers. --get-icmptypes lists the 49 types firewalld knows.

Make rules survive a reload

Firewalld holds a runtime configuration (what the kernel enforces now) and a permanent one (the XML under /etc/firewalld/). A command without --permanent changes only runtime and is lost on --reload, a service restart or a reboot. A command with --permanent changes only the files and does nothing until you reload. That second half trips people up more than the first. This is the whole cycle from web01, with db01’s curl in between. The two list commands read different copies of the configuration, so comparing them is the quickest way to see what a reload will throw away:

sudo firewall-cmd --list-services
sudo firewall-cmd --list-services --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-services

After the runtime-only --add-service=http, runtime had HTTP and disk didn’t. The reload threw it away:

dhcpv6-client http ssh
dhcpv6-client ssh
success
dhcpv6-client ssh

Side by side in the terminal, the reload is the line where the rule disappears. Nothing warns you at that point, so the first sign in production is usually a monitoring alert after someone else reloads the firewall for an unrelated change. That is why we save rules the moment a test passes instead of at the end of the session, when the list of runtime-only changes has grown and nobody remembers which ones mattered:

firewall-cmd runtime rule lost after reload while permanent rule waits for reload on Ubuntu 26.04

The same session showed the other direction too: --add-service={http,https} --permanent left the running list untouched until --reload applied it. So a permanent change you never reload looks saved, passes --list-all --permanent and still does nothing, which is the usual cause of an open port that answers nothing.

Test in runtime and save what works. That order is safer over SSH, because a bad runtime rule disappears with a reload or a reboot, while a bad permanent rule comes back at every boot. The routine we use:

  1. Add the rule without --permanent
  2. Test it from a second machine
  3. Save it with sudo firewall-cmd --runtime-to-permanent

The save command copies the whole runtime configuration over the permanent one, so anything you added by hand for a quick test gets saved with it. Check --list-all before you run it:

sudo firewall-cmd --runtime-to-permanent

The other habit, writing --permanent rules and reloading once at the end, suits scripts and configuration management better, since the files are the source of truth there. A --timeout rule is the safest test of all, since it removes itself. It’s runtime only, and firewalld refuses to combine it with --permanent:

sudo firewall-cmd --add-port=8080/tcp --permanent --timeout=60s

The error is explicit and the rule is not added, so a script that relies on the timeout fails loudly instead of leaving a permanent hole:

usage: 'firewall-cmd --help' for usage information or see firewall-cmd(1) man page
Can't specify timeout for permanent action.

Use --add-service=http --timeout=60s on its own and the rule expires after a minute. It is the right tool for a remote change you are unsure about, because if the change cuts you off, waiting a minute brings the old state back without console access.

Write rich rules for source-specific access

Plain --add-service opens a port to everyone in the zone. Rich rules add the conditions: source or destination address, logging, rate limits, and accept, reject or drop. The full grammar is in the firewalld.richlanguage man page. Quote the whole rule in single quotes and each value in double quotes, the way --list-rich-rules prints them back. To prove a drop with logging, web01 served a test page on port 8080, which the zone already allowed. This rule drops db01 on that port and logs at most five hits a minute:

sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="10.0.1.20" port port="8080" protocol="tcp" log prefix="fw-8080-drop " level="info" limit value="5/m" drop'

Db01’s curl went from the page to a timeout, because drop sends nothing back. The kernel log named the rule by its prefix, which is the reason to give every logging rule its own prefix:

Nov 09 05:39:52 web01 kernel: fw-8080-drop IN=ens4 OUT= MAC=(redacted) SRC=10.0.1.20 DST=10.0.1.10 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=26101 DF PROTO=TCP SPT=37832 DPT=8080 WINDOW=65320 RES=0x00 SYN URGP=0 

The full run puts the rule, the timeout and the log line together. Keep the limit on any logging rule you leave in place, because a scanner hitting a logged port otherwise fills the journal in minutes. The prefix and the source address in the log line are enough to grep for a single rule later, so give each logging rule a prefix that names the port it guards:

firewalld rich rule logging and dropping one source on port 8080 on Ubuntu 26.04

That shows rich rule ordering too. Deny rules run before allow rules inside a zone, so the drop won even though 8080/tcp was open. A few more patterns from the same session:

sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" service name="postgresql" accept'
sudo firewall-cmd --add-rich-rule='rule service name="ssh" accept limit value="10/m"'
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" reject type="icmp-port-unreachable"'
sudo firewall-cmd --list-rich-rules

Firewalld lists them in its own order and quoting, and that printed text is exactly what --remove-rich-rule expects:

rule family="ipv4" source address="10.0.1.0/24" service name="postgresql" accept
rule family="ipv4" source address="10.0.1.20" port port="8080" protocol="tcp" log prefix="fw-8080-drop " level="info" limit value="5/m" drop
rule service name="ssh" accept limit value="10/m"
rule family="ipv4" source address="203.0.113.0/24" reject type="icmp-port-unreachable"

Copy a rule from this list when you remove it. A rule typed from memory with one value quoted differently is a different string, so --remove-rich-rule finds nothing to remove and the rule stays in place. With --permanent, list and remove with the flag on both commands, because the runtime and saved lists are separate.

Set up port forwarding and masquerade

A forward port on the same host is a redirect and needs nothing else. On web01 this sent port 8081 to nginx on port 80, and db01 got 200 on the new port:

sudo firewall-cmd --add-forward-port=port=8081:proto=tcp:toport=80

Forwarding to another host is different, and this is where most port-forward guides go wrong. A cross-host forward needs three pieces in place:

  • A forward port with toaddr=
  • Masquerade in the same zone
  • IP forwarding in the kernel (firewalld enables it with masquerade)

We used db01 (Rocky Linux 10) as a gateway, sending its port 8088 to nginx on web01, and tested from web02. The first attempt had only the forward port:

sysctl net.ipv4.ip_forward
sudo firewall-cmd --add-forward-port=port=8088:proto=tcp:toport=80:toaddr=10.0.1.10

IP forwarding was off and the curl from web02 timed out. Nothing was rejected, so there was no error to read, which is why a half-built forward looks like a network problem rather than a firewall one:

net.ipv4.ip_forward = 0
success
# curl from web02
curl: (28) Connection timed out after 5002 milliseconds
000

Two things were missing. The kernel was not forwarding at all, and even with forwarding on, the packet would still carry web02’s address as its source, so web01 would answer web02 directly and the reply would bypass db01 (many clouds drop such a packet outright). Masquerade fixes both here, because it rewrites the source to db01 and firewalld switches on IP forwarding for you when you enable it:

sudo firewall-cmd --add-masquerade
sysctl net.ipv4.ip_forward

Forwarding flipped to 1 and the same curl returned the nginx page. Nginx on web01 now logs db01 as the client, not web02, because masquerade replaced the source address on the way through:

success
net.ipv4.ip_forward = 1
# curl from web02
200

Both attempts on db01 are in one capture, before and after masquerade. The only change between the timeout and the 200 is one command, so when a forward fails, check --query-masquerade before anything else. The sysctl line matters too, because a host where something else forces ip_forward back to 0 will drop the forward again at the next boot:

firewalld forward-port to another host failing until masquerade is enabled on Rocky Linux 10

Add both with --permanent once they work. Masquerade is a per-zone setting, so it applies to every connection leaving through that zone, not only the forwarded port, and on a host with one interface that is all of its traffic. A VPN gateway uses the same pair on purpose, and a WireGuard VPN gateway that routes clients onto a LAN needs it for the same reason.

Use ipsets to block address lists

A rich rule per address gets slow to read and slow to load once the list grows. An ipset holds thousands of entries in one kernel set that a zone can use as a single source. Create the set on disk, reload, then fill it:

sudo firewall-cmd --new-ipset=blocklist --type=hash:net --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --ipset=blocklist --add-entry=10.0.1.20/32
sudo firewall-cmd --ipset=blocklist --add-entry=198.51.100.0/24
sudo firewall-cmd --ipset=blocklist --add-entries-from-file=/tmp/bad-ips.txt
sudo firewall-cmd --ipset=blocklist --get-entries

Four entries came back, two typed and two from the file. Nothing filters on them yet, because a set does nothing until a zone or rule uses it:

10.0.1.20/32
198.51.100.0/24
192.0.2.10
192.0.2.11

Attach the set to the drop zone and everything in it gets no reply at all. Because a source match beats the interface, every address in the set skips public entirely, SSH included:

sudo firewall-cmd --zone=drop --add-source=ipset:blocklist
sudo firewall-cmd --get-active-zones

The drop zone becomes active through the set, and db01’s curl timed out instead of being refused:

drop
  sources: ipset:blocklist
public (default)
  interfaces: ens4

The terminal view of the same run goes from the entries to the active zones. Checking --get-active-zones after attaching a set is worth the habit, because it is the only listing that shows the set is live. A set that exists but is attached to nothing blocks nobody, and --get-ipsets will still list it as if all were well:

firewalld ipset blocklist attached to the drop zone on Ubuntu 26.04

Watch the query syntax. The entry went in as 10.0.1.20/32, so --query-entry=10.0.1.20 answered no even though the address was blocked. On a hash:net set, query with the same prefix you added. The hash:ip set on Ubuntu 24.04 matched the bare address as expected. For fail2ban style automation, --add-entries-from-file is the command to script.

Use policies for outbound and forwarded traffic

Zones only filter traffic coming into the host. Policies filter traffic between zones, including traffic the host sends itself through the symbolic HOST zone, so they replace most of what people used the deprecated direct interface for. This one stops web01 from opening SSH to db01:

sudo firewall-cmd --new-policy=host-egress --permanent
sudo firewall-cmd --policy=host-egress --add-ingress-zone=HOST --permanent
sudo firewall-cmd --policy=host-egress --add-egress-zone=ANY --permanent
sudo firewall-cmd --policy=host-egress --add-rich-rule='rule family="ipv4" destination address="10.0.1.20" port port="22" protocol="tcp" reject' --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --info-policy=host-egress

The policy is active with its target left at CONTINUE, so anything the rich rule does not match falls through. Set the target to REJECT instead and the policy becomes an outbound allowlist, which also cuts the host off from package mirrors and DNS until you allow them, so start with CONTINUE:

host-egress (active)
  priority: -1
  target: CONTINUE
  ingress-zones: HOST
  egress-zones: ANY
  services: 
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	rule family="ipv4" destination address="10.0.1.20" port port="22" protocol="tcp" reject

The connection test from web01 went from open to refused. Other destinations were untouched, because the CONTINUE target hands unmatched traffic on:

bash: connect: Connection refused
bash: line 1: /dev/tcp/10.0.1.20/22: Connection refused

Read the existing policies before you add your own, because a policy with a lower priority number runs first. Policies also explain what other software does to your firewall. On Rocky Linux 10, --get-policies listed more built-in policies than Ubuntu did:

allow-host-ipv6 gateway-dmz-to-HOST gateway-lan-to-HOST gateway-lan-to-work gateway-lan-to-world gateway-world-to-HOST

On Ubuntu web01 the list grew a docker-forwarding entry the moment Docker started, which leads into the next section.

Use panic mode without locking yourself out

Panic mode drops every packet in both directions, established connections included. It’s the right call when a host is actively being abused, and the wrong call over SSH unless something will turn it off again. Schedule the off switch first:

sudo systemd-run --on-active=45s --timer-property=AccuracySec=1s /usr/bin/firewall-cmd --panic-off
sudo firewall-cmd --panic-on

The AccuracySec=1s matters. Without it a systemd timer may fire up to a minute late, and our first attempt’s leftover timer switched panic mode off in the middle of the next test. While panic mode was on, firewalld had added a second table with drop policies at the raw hooks:

yes
table inet firewalld_policy_drop {
	chain raw_prerouting {
		type filter hook prerouting priority raw + 9; policy drop;
	}

	chain raw_output {
		type filter hook output priority raw + 9; policy drop;
	}
}

From db01, ping lost every packet, and a new SSH session to web01 timed out. The open SSH session that ran the command froze as well, so without the timer the only way back would have been the console:

PING 10.0.1.10 (10.0.1.10) 56(84) bytes of data.

--- 10.0.1.10 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2044ms

The whole panic test fits in one capture, timer first. The order is the point: run the timer command before --panic-on, because once panic mode is active the terminal you would type the fix into stops responding. Count the seconds you need for the test and add a margin, since the timer is the only thing that brings the host back without console access:

firewalld panic mode dropping all traffic on Ubuntu 26.04 with a timer to turn it off

After 45 seconds the timer ran --panic-off and --query-panic answered no. Our first attempt used a timer with no accuracy set and a 25 second delay, and it fired about a minute late, so plan the delay with that slack if you leave the default. If you have console access (a hypervisor, IPMI, a cloud serial console), you can skip the timer.

Stop Docker from bypassing firewalld

Docker publishes container ports with its own DNAT rules, and firewalld accepts DNAT’d traffic by default, so a published port is reachable no matter what your zone says. We reproduced it on Ubuntu 26.04 with the distribution’s docker.io package:

sudo docker run -d --name web8090 -p 8090:80 nginx:alpine
sudo firewall-cmd --query-port=8090/tcp

Port 8090 was not open in public, yet db01 got the nginx page. So --list-all is not an inventory of what the host exposes once Docker runs, and ss -tlnp or a scan from another machine is the only honest check:

no
# curl from db01
200

Docker also registers itself with firewalld, which you can see in the zones. The docker zone has target ACCEPT, so container traffic is trusted once it is inside:

docker
  interfaces: docker0
public (default)
  interfaces: ens4

Our first fix attempt, a policy from public to docker with a REJECT target, changed nothing: db01 still got 200. Firewalld’s forward chain accepts any packet with DNAT status before it reaches the policies. The setting that removes that shortcut is StrictForwardPorts, added in the firewalld version Ubuntu 26.04 and Rocky Linux 10 ship. Ubuntu 24.04’s package doesn’t have it: grep -c StrictForwardPorts on its config returned 0.

sudo sed -i 's/^StrictForwardPorts=no/StrictForwardPorts=yes/' /etc/firewalld/firewalld.conf
sudo firewall-cmd --reload

After the reload, port 8090 was refused from both other VMs, and it stayed refused after restarting Docker. To publish one container on purpose, give firewalld its own forward port to the container address:

sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web8090
sudo firewall-cmd --add-forward-port=port=8090:proto=tcp:toport=80:toaddr=172.17.0.2 --permanent
sudo firewall-cmd --reload

The DNAT chain then allows only that original destination port, and the curl returned 200 again. Because the forward port lives in the permanent zone, it survives reloads and reboots and shows up in --list-all, so the exposure is visible in the firewall again:

table inet firewalld {
	chain filter_FORWARD_dnat {
		ct original proto-dst 8090 accept
		reject with icmpx admin-prohibited
	}
}

The capture shows the port before and after the strict setting, from db01’s point of view. Run the same test from a second machine on your own hosts, since a curl from the Docker host itself never crosses the forward chain and will succeed either way. A port scan from outside the host is the check that matters, because it sees what the internet sees, whichever tool wrote the rule:

Docker published port bypassing firewalld until StrictForwardPorts is enabled on Ubuntu 26.04

That chain matches on the destination port only, so a source-limited rich rule forward port opened 8090 to db01 as well in our test. Container IPs also change when a container is recreated. If you need per-source control, pick one of these instead:

  • Publish on one address with -p 10.0.1.10:8090:80
  • Publish on 127.0.0.1 behind a reverse proxy on the host
  • Put the clients in their own zone and forward from there

The reverse proxy route is the one we run, because the proxy port then sits in a normal zone where rich rules work. The Docker cheat sheet covers the publish flags, and Podman users can check the same behaviour with the Podman cheat sheet.

Compare firewalld on Ubuntu and Rocky Linux 10

The commands are identical across the three systems. What changes is the package, the network stack under it and two defaults worth checking, so a script written on one runs on the others once those are handled:

ItemUbuntu 26.04Ubuntu 24.04Rocky Linux 10
firewalld package2.3.1-32.1.1-12.4.3-4.el10_2
Installed by defaultNo (UFW is)No (UFW is)Yes, running
Backendnftablesnftablesnftables
Interface in a zoneOnly after –change-interfaceOnly after –change-interfaceYes, via NetworkManager
public zone servicesssh, dhcpv6-clientssh, dhcpv6-clientssh, dhcpv6-client, cockpit
StrictForwardPortsPresent, noNot presentPresent
firewall-cmd as non-rootAuthorization failedAuthorization failed–version works without sudo

For a RHEL family box, the firewalld guide for RHEL walks the same commands from the Rocky side, and Debian, Fedora and openSUSE have their own pages: firewalld on Debian, firewalld on Fedora and firewalld on openSUSE Leap. On Rocky, binding an interface through NetworkManager is also possible, which the nmcli cheat sheet shows.

Fix common firewall-cmd errors

These are the errors the lab actually printed, with the exact text you’d search for.

Authorization failed. Make sure polkit agent is running

Running firewall-cmd as a normal user on Ubuntu, even for --version, returns this and exit code 253. Rocky Linux 10 answered the same --version call without sudo, so scripts written there break on Ubuntu:

Authorization failed.
    Make sure polkit agent is running or run the application as superuser.

Firewalld answers over D-Bus and Ubuntu’s polkit policy allows only root. Prefix sudo. The same applies to reading /etc/firewalld/firewalld.conf, which stat showed as mode 600, owned by root, on Ubuntu 26.04.

Error: INVALID_PROTOCOL

A rich rule with a port element but no protocol is rejected as a whole, so nothing from it is applied:

Error: INVALID_PROTOCOL

Add protocol="tcp" or protocol="udp" next to the port. Firewalld checks the whole rule before it writes anything, so a failed --add-rich-rule leaves the zone exactly as it was and you can retry without cleanup.

Can’t specify timeout for permanent action

As shown earlier, --timeout only works on runtime rules. Drop --permanent, or drop the timeout and remove the rule yourself.

Error: INVALID_ZONE

A typo in --zone= fails with exit code 112 and the zone name it could not find. The exit code is stable, so scripts can test for it instead of parsing the message:

Error: INVALID_ZONE: nosuchzone

Check the spelling against --get-zones. A new zone created with --permanent doesn’t exist in runtime until you reload, which produces the same error.

Warning: ZONE_ALREADY_SET

Setting the default zone to the zone that’s already default prints a warning and still returns success, so it’s harmless in scripts:

Warning: ZONE_ALREADY_SET: public
success

Firewalld treats the request as already satisfied, which keeps idempotent tooling such as Ansible quiet. A script that checks the exit code still passes, so you can run --set-default-zone on every provisioning pass without a guard around it.

Keep reading

Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step) Ubuntu Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step) UFW Firewall Commands with Examples on Ubuntu 26.04 / 24.04 Security UFW Firewall Commands with Examples on Ubuntu 26.04 / 24.04 PHP2Shell (CVE-2026-109618, CVE-2026-109619): Check and Patch PHP Php PHP2Shell (CVE-2026-109618, CVE-2026-109619): Check and Patch PHP Git CVE-2026-111760: Check and Patch the Recursive Clone RCE Git Git CVE-2026-111760: Check and Patch the Recursive Clone RCE Deploy Collabora Online Office on Ubuntu with Let’s Encrypt SSL Cloud Deploy Collabora Online Office on Ubuntu with Let’s Encrypt SSL Install SuiteCRM on Ubuntu 24.04 with Nginx Ubuntu Install SuiteCRM on Ubuntu 24.04 with Nginx

Leave a Comment

Press ESC to close