Installing firewalld on Debian 13 turns it on immediately. The package enables and starts the service in the same apt run, and from that second the server only answers on SSH and DHCPv6 until you open anything else. That is the safe default, but a web server you could reach a minute ago stops answering, and on Ubuntu an active UFW keeps blocking ports firewalld says are open, because both rule sets load side by side.
This guide installs and configures firewalld on Debian 13 and Ubuntu 24.04: the default zone, services and ports, rich rules, port forwarding, masquerading, zones per interface, policies and panic mode. Each rule was tested from a second machine, so you see what the client gets, not only what firewall-cmd prints. Every command below was run on Debian 13.7 with firewalld 2.3.1 and Ubuntu 24.04.5 with firewalld 2.1.1 in November 2026.
Prerequisites
The lab used two small cloud VMs on the same private network: deb13-fw01 (Debian 13, 10.128.0.42) as the firewall host and u2404-fw02 (Ubuntu 24.04, 10.128.0.43) as the second firewall host and the test client. You need:
- Debian 13 or Ubuntu 24.04 with sudo access
- A console or out-of-band login in case a rule cuts off SSH
- A second host on the network to test rules from
You’ll want that console more than it looks. Every command here keeps SSH open, but a typo in a rich rule or a zone change on the wrong interface can drop your session, and the only way back in is a login that doesn’t go through the firewall.
Disable UFW before you start
Ubuntu 24.04 cloud images ship UFW installed but inactive, and Debian 13 does not install it at all. Nothing in the packages stops the two from running together: the firewalld unit on Ubuntu lists nftables.service, iptables.service and ipset.service as conflicts, but not UFW. If UFW is already enabled on the box, turn it off and keep it off across reboots:
sudo ufw --force disable
sudo systemctl disable --now ufw
To see why this step exists, we enabled UFW on the Ubuntu VM after firewalld had already opened HTTP. UFW loads its rules into separate ip filter tables next to firewalld’s inet firewalld table, and a packet has to be accepted by both. The client’s request to port 80 went from HTTP 200 to a connection failure the moment UFW came up, and back to 200 once it was disabled, even though firewall-cmd --list-services showed http the whole time.

The two filter tables stay in the kernel after ufw disable, but they no longer drop anything, so you can ignore them in nft list tables. If you’d rather stay on UFW, the UFW command reference covers the same jobs with its syntax. Pick one firewall per host.
Install firewalld
Firewalld is in Debian’s main repository and in Ubuntu’s universe component, which cloud images enable by default. Install it with apt:
sudo apt update
sudo apt install -y firewalld
On Debian 13 the install pulled in 26 packages, including ipset, libnftables1 and python3-nftables, but not the nft command. Install nftables as well with sudo apt install -y nftables, because several checks below read the generated ruleset with nft. The service is enabled and running as soon as apt finishes, so you do not need systemctl enable --now. Confirm the version, the state and the backend:
sudo firewall-cmd --version
systemctl status firewalld --no-pager
sudo grep -E "^(DefaultZone|FirewallBackend|StrictForwardPorts)" /etc/firewalld/firewalld.conf
Look for enabled on the Loaded line, active (running), and FirewallBackend=nftables. Use sudo for --version: run as a normal user it fails with a polkit authorization error rather than printing the version.

The two releases are not the same firewalld, and the differences decide which options exist on your box:
| Check | Debian 13 | Ubuntu 24.04 |
|---|---|---|
| firewalld package | 2.3.1-1+deb13u1 (main) | 2.1.1-1 (universe) |
State after apt install | enabled, active | enabled, active |
| Predefined services | 259 | 245 |
StrictForwardPorts option | present, set to no | not available |
IPv6_rpfilter default | strict | yes |
| Unit conflicts with UFW | no | no |
The StrictForwardPorts row is the one that bites people running containers. The firewalld.conf manual on Debian 13 says that with it set to no, the default, published container ports are allowed implicitly, so a port Docker publishes is reachable whatever your zone says. Debian 13 lets you set it to yes; Ubuntu 24.04’s 2.1.1 has no such option, so on Ubuntu you control container ports in the runtime itself. If the host runs containers, that difference alone is a reason to prefer Debian 13 or a newer Ubuntu.
Check the default zone
Every interface and source belongs to a zone, and anything not assigned explicitly falls into the default zone, which is public on both releases. Check what is active and what the zone allows:
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-all
Only ssh and dhcpv6-client are allowed. The interfaces: line is empty because neither VM runs NetworkManager, which is what normally assigns an interface to a zone; the default zone still covers ens4, which is why --get-active-zones prints public (default).

For a server that faces the internet, keep public as the default and add services to it. Changing the default zone with --set-default-zone takes effect at once and persists, so only do it when another zone’s starting set fits better, and check it first with sudo firewall-cmd --zone=internal --list-all.
Open services and ports
Nginx was listening on port 80 of the Debian VM, and the client got curl: (7) Failed to connect ... Couldn't connect to server straight after the install. A service name maps to its ports, so http reads better in an audit than 80/tcp. Check the mapping and the list of names before you add a raw port:
sudo firewall-cmd --info-service=http
sudo firewall-cmd --get-services
The catch with firewalld is that it keeps two configurations. A rule added without --permanent works at once but lives only in the running firewall, and the next reload or reboot throws it away. The capture shows it: https added at runtime is in the list, then gone after --reload. Opening HTTP at runtime gave the client a 200, and after the reload it was refused again.

So write permanent rules and reload to apply them. Add HTTP and HTTPS to the public zone:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-services
The list should now include both services, and a request from the second host returns 200:
dhcpv6-client http https ssh
Ports without a service definition go in as port/protocol, and a range uses a hyphen. These open TCP 8080, UDP 5353 and a TCP range for something like passive FTP:
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --permanent --add-port=5353/udp
sudo firewall-cmd --permanent --add-port=30000-31000/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --list-ports
All three entries appear on one line:
8080/tcp 30000-31000/tcp 5353/udp
Removing a rule is the same command with --remove. If you tested something at runtime and want to keep it, sudo firewall-cmd --runtime-to-permanent copies the whole running state into the permanent config. We used it to keep a runtime 8080/tcp, and it showed up in --permanent --list-ports straight away.
sudo firewall-cmd --permanent --remove-port=30000-31000/tcp
sudo firewall-cmd --permanent --remove-port=5353/udp
sudo firewall-cmd --reload
Check the zone once more with sudo firewall-cmd --list-all after any removal, because a port opened earlier under another name, such as 8080/tcp next to a service that also uses 8080, keeps the port open.
Restrict access with rich rules
Rich rules add a source address, logging, a rate limit or a reject action to a rule. Two behaviours decide whether a rich rule does what you meant, and both showed up in the lab:
- accept rules only add access to what the zone already allows
- drop and reject rules run before the zone’s services
So a rule that allows HTTP from one subnet changes nothing while http is still in the zone’s service list, because the service already lets everyone in. A drop for one address, on the other hand, wins even while the service is open to everyone. When you restrict a service, remove it from the zone first and then add the rich rule.
Allow a service from one subnet only
Remove the open service first, then allow it from the subnet that should reach it. Here it is the client’s own address:
sudo firewall-cmd --permanent --remove-service=http
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" service name="http" accept'
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.128.0.43/32" service name="http" accept'
sudo firewall-cmd --reload
sudo firewall-cmd --list-rich-rules
With a rule for 10.0.1.0/24 only, the client at 10.128.0.43 got no answer, because nothing else in the zone allowed port 80 any more. Once its own address was in a rule, it got 200. Both rules are listed:
rule family="ipv4" source address="10.128.0.43/32" service name="http" accept
rule family="ipv4" source address="10.0.1.0/24" service name="http" accept
Do the same for SSH only if you have that console login, because removing ssh from the zone before the rich rule matches your address locks you out.
Block an address: drop or reject
A drop rule makes the host silent, so the client waits for its own timeout and learns nothing about whether the host exists. A reject rule answers with an ICMP error and the client fails at once:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.128.0.43" drop'
sudo firewall-cmd --reload
With http added back to the zone, so HTTP was open to everyone, and this drop in place, the client’s curl hung for its full 6 seconds and ended with curl: (28) Connection timed out. The same rule with reject failed in 12 milliseconds with curl: (7) Failed to connect. Use drop for addresses you want to waste time on and reject for internal hosts, so their applications fail fast instead of hanging on a timeout.
Clear the test rules before the next step, because the drop and the source accept for the client would both hide the rate limit. Remove a rule by repeating it with --remove-rich-rule, and take http out of the zone again:
sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="10.128.0.43" drop'
sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="10.128.0.43/32" service name="http" accept'
sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="10.0.1.0/24" service name="http" accept'
sudo firewall-cmd --permanent --remove-service=http
sudo firewall-cmd --reload
sudo firewall-cmd --list-rich-rules should now print nothing.
Rate limit new connections
A limit on an accept rule caps how often the rule matches. With http out of the zone and no other rule for port 80, this allows three new connections a minute:
sudo firewall-cmd --permanent --add-rich-rule='rule service name="http" accept limit value="3/m"'
sudo firewall-cmd --reload
Ten quick requests from the client got five 200s and five failures, not three, because firewalld compiles the rule with a burst of 5 packets. So the first five connections in any quiet period always get through, and the 3 per minute only applies after that. The second line in the capture is the drop test from the previous step.

The nftables rule behind it shows the part that matters for production: there is no per-source meter. The budget is shared by every client, so one noisy address uses it up for everybody.
sudo nft list chain inet firewalld filter_IN_public_allow | grep -E "limit|dport 80"
The limit sits directly on the port 80 accept, with the 3/minute rate and the burst of 5:

That is why we wouldn’t put a rich rule limit on SSH. An attacker spending the shared budget locks you and your admins out too, and with ssh still in the zone the limit does nothing at all, because the plain service accepts what the limited rule refuses. For brute force on SSH, Fail2ban bans per address and works with firewalld.
The limit rule is now the only thing letting port 80 in, so remove it and put the plain service back before you continue, or every later HTTP test runs into the same five-connection budget:
sudo firewall-cmd --permanent --remove-rich-rule='rule service name="http" accept limit value="3/m"'
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
The MySQL logging rule in the next step can stay, since nothing on this host should accept 3306 from outside anyway.
Log what a rule drops
Add log with a prefix to a rich rule and every match lands in the kernel log. Give the log its own limit so a scan can’t flood the journal. This logs and drops MySQL connections:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" port port="3306" protocol="tcp" log prefix="mysql-blocked: " level="warning" limit value="5/m" drop'
sudo firewall-cmd --reload
After two connection attempts with nc from the client, both timed out, and the kernel log names the source and port. MAC addresses are redacted in the capture.
sudo journalctl -k --no-pager -g mysql-blocked | tail -2
Each line carries the prefix, SRC=10.128.0.43 and DPT=3306:

To log everything the zones refuse, not one port, switch on denied logging. --set-log-denied writes the setting to firewalld.conf and reloads, so it survives a reboot:
sudo firewall-cmd --set-log-denied=unicast
sudo firewall-cmd --get-log-denied
A refused connection to port 5432 then logged as filter_IN_public_REJECT: in journalctl -k, and the client saw No route to host, which is how firewalld’s reject message looks to most tools. Set it back with --set-log-denied=off when you’re done debugging.
Forward ports
A forward port rewrites the destination of incoming traffic, to another port on the same host or to another machine. You’d use the local form when an app listens on one port and clients expect another. Forward 8080 to the local Nginx on 80:
sudo firewall-cmd --permanent --add-forward-port=port=8080:proto=tcp:toport=80
sudo firewall-cmd --reload
The client then got the “Welcome to nginx!” page on port 8080. A request to 127.0.0.1:8080 on the server itself still failed, because the rule only rewrites traffic coming in on an interface. So if you test a forward with curl on the server and it fails, that doesn’t mean the rule is wrong: test it from another host.
Sending traffic to another machine needs masquerading as well, so the backend replies through the gateway instead of straight to the client. We forwarded port 9000 on the Debian host to a web server on port 80 of the Ubuntu VM:
sudo firewall-cmd --permanent --add-port=9000/tcp
sudo firewall-cmd --permanent --add-forward-port=port=9000:proto=tcp:toport=80:toaddr=10.128.0.43
sudo firewall-cmd --reload
In the lab the request came from the Ubuntu VM and was forwarded back to a web server on that same VM, so without masquerading the reply never went back through the gateway and curl timed out after 5 seconds. Turn masquerading on:
sudo firewall-cmd --permanent --add-masquerade
sudo firewall-cmd --reload
The next request returned the backend’s page. Firewalld also turned on IP forwarding by itself. Our Google Cloud image sets net.ipv4.ip_forward=0 in /etc/sysctl.d/60-gce-network-security.conf, and the Ubuntu VM without forward rules still read 0, yet the Debian host read 1 once the forward and masquerading were in place. Check the result on your host rather than assuming it:
sudo firewall-cmd --list-forward-ports
sudo firewall-cmd --query-masquerade
cat /proc/sys/net/ipv4/ip_forward
Both forwards are listed, masquerading reads yes, and the nftables ruleset carries the dnat and masquerade statements firewalld generated:

Masquerading in the public zone rewrites every connection leaving through that interface, so on a single-NIC server keep it only as long as the forward needs it. The backend’s logs also lose the real client address, because every request now arrives from the gateway’s IP, which matters if the backend rate limits or bans by source. On a real gateway it belongs in the external zone on the uplink, which comes with masquerading already on. When you no longer need the test forwards, remove them the same way they went in:
sudo firewall-cmd --permanent --remove-forward-port=port=9000:proto=tcp:toport=80:toaddr=10.128.0.43
sudo firewall-cmd --permanent --remove-forward-port=port=8080:proto=tcp:toport=80
sudo firewall-cmd --permanent --remove-port=9000/tcp
sudo firewall-cmd --permanent --remove-masquerade
sudo firewall-cmd --reload
After that reload --list-forward-ports printed nothing and --query-masquerade said no, but ip_forward still read 1. Firewalld switches forwarding on and leaves it on, so reboot or set it back with sysctl if the host must not route.
Split interfaces across zones
A host with two networks gets one zone per interface, and a trusted subnet can get its own zone by source address. Our VMs have one NIC, so we added a dummy interface lan0 with 192.168.50.1/24 to stand in for the internal network. Skip this on a host with a real second interface, and note that a dummy interface made this way is gone after a reboot:
sudo ip link add lan0 type dummy
sudo ip addr add 192.168.50.1/24 dev lan0
sudo ip link set lan0 up
Then bind each interface to its zone, open MySQL on the internal side only, and send a management subnet to trusted:
sudo firewall-cmd --permanent --zone=external --change-interface=ens4
sudo firewall-cmd --permanent --zone=internal --change-interface=lan0
sudo firewall-cmd --permanent --zone=internal --add-service=mysql
sudo firewall-cmd --permanent --zone=trusted --add-source=10.0.1.0/24
sudo firewall-cmd --reload
Moving ens4 out of public cost us HTTP. Services belong to zones, and external starts with ssh only, so the client that had been getting 200 got nothing until ens4 went back to public. Before you move an interface, check these in the old zone and recreate the ones you need in the new one:
- services and ports from
--list-all - rich rules from
--list-rich-rules - forward ports and masquerading
Do it in the permanent config and reload once, so you never sit in a state where SSH is open but your application is not.
sudo firewall-cmd --get-active-zones
Each interface and the source subnet now sit in their own zones, and public stays the default for anything else:

Without NetworkManager, firewalld stores the binding in the zone file itself, as <interface name="ens4"/> in /etc/firewalld/zones/external.xml, which is how it survives a reboot on a plain Debian or netplan server. If an interface lands in the wrong zone after a reboot, read these files with sudo before anything else, because the binding lives there and not in the network configuration. A source in trusted accepts everything from that subnet, so keep sources there to addresses you control.
For the rest of this guide we put ens4 back in public, where the services from earlier live, and kept lan0 in internal:
sudo firewall-cmd --permanent --zone=external --remove-interface=ens4
sudo firewall-cmd --permanent --zone=public --add-interface=ens4
sudo firewall-cmd --reload
After the reload the client got 200 on port 80 again, and --get-active-zones listed ens4 under public.
Filter traffic between zones with policies
Zones filter traffic to the host. Traffic routed between zones, such as LAN clients going out through the gateway, is handled by policies. This one lets the internal zone out through public:
sudo firewall-cmd --permanent --new-policy=int-to-ext
sudo firewall-cmd --permanent --policy=int-to-ext --add-ingress-zone=internal
sudo firewall-cmd --permanent --policy=int-to-ext --add-egress-zone=public
sudo firewall-cmd --permanent --policy=int-to-ext --set-target=ACCEPT
sudo firewall-cmd --reload
sudo firewall-cmd --info-policy=int-to-ext
The policy shows as active with its ingress and egress zones:
int-to-ext (active)
priority: -1
target: ACCEPT
ingress-zones: internal
egress-zones: public
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
Policies replace direct rules, which older guides still recommend. We added the classic ping-blocking direct rule, --direct --add-rule ipv4 filter INPUT 0 -p icmp --icmp-type echo-request -j DROP, and it still worked on firewalld 2.3.1. It went through iptables-nft into a separate ip filter table outside the zones, though, so --list-all never shows it, and the firewall-cmd manual marks the whole direct interface deprecated and due for removal. If you find direct rules on a server you inherit, rewrite them as rich rules or policies before an upgrade drops them.
Create a custom service
An application on its own ports is easier to audit as a named service than as loose port numbers. Define it once and add it to any zone:
sudo firewall-cmd --permanent --new-service=myapp
sudo firewall-cmd --permanent --service=myapp --set-description="My Custom Application"
sudo firewall-cmd --permanent --service=myapp --add-port=9090/tcp
sudo firewall-cmd --permanent --service=myapp --add-port=9091/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --zone=public --add-service=myapp
sudo firewall-cmd --reload
Firewalld writes the definition to /etc/firewalld/services/myapp.xml, which you can copy to other hosts:
<?xml version="1.0" encoding="utf-8"?>
<service>
<description>My Custom Application</description>
<port port="9090" protocol="tcp"/>
<port port="9091" protocol="tcp"/>
</service>
Run sudo firewall-cmd --list-services afterwards and myapp sits next to the built-in names, and --info-service=myapp shows both ports.
Test panic mode with a timer
Panic mode drops every packet in and out, including your SSH session, so you cannot type the command that turns it off. Never run it on a remote host without a way back. Schedule the panic-off first:
sudo systemd-run --on-active=30 --timer-property=AccuracySec=1s /usr/bin/firewall-cmd --panic-off
sudo firewall-cmd --panic-on
Our SSH session froze and came back 44 seconds later once the timer had run, and a ping from the other VM during that window lost 10 of 10 packets. sudo firewall-cmd --query-panic printed no afterwards. Without AccuracySec=1s, systemd can run the timer up to a minute late.
Choose the right zone
These are the predefined zones as firewalld 2.3.1 ships them on Debian 13, read with --get-target, --list-services and --query-masquerade for each zone. A target of default rejects anything not listed:
| Zone | Target | Services allowed | Use it for |
|---|---|---|---|
| drop | DROP | none | silently ignoring a network |
| block | REJECT | none | refusing with an ICMP error |
| public | default | ssh, dhcpv6-client | internet-facing servers (default zone) |
| external | default | ssh, masquerade on | the uplink of a NAT gateway |
| dmz | default | ssh | hosts in a DMZ |
| work | default | ssh, dhcpv6-client | office networks |
| home | default | ssh, mdns, samba-client, dhcpv6-client | home LANs |
| internal | default | ssh, mdns, samba-client, dhcpv6-client | backend and LAN interfaces |
| trusted | ACCEPT | everything | management subnets you control |
home and internal start identical, and work matches public, so the choice between them is about naming your networks clearly, not about different defaults. The target is different: it only decides what happens to traffic nothing in the zone matched. We put the client’s address in drop and added http to that zone, and the client still got 200 on port 80 while port 8080 timed out. A service you add to drop or block is open, and a zone with trusted‘s ACCEPT target has no firewall at all. For a printable list of every firewall-cmd command, including rich rules, ipsets, policies and the StrictForwardPorts fix for Docker, see the firewalld cheat sheet.
Fix common firewalld errors
These are the messages the lab produced, with what each one means.
Authorization failed. Make sure polkit agent is running
firewall-cmd run as a normal user asks polkit for permission over D-Bus, and on a server with no polkit agent the request fails, even for --version. Run it with sudo, or add your admin user to a polkit rule if you really need unprivileged access.
Warning: ALREADY_ENABLED: ‘http’ already in ‘public’
The service is already in that zone’s runtime or permanent set, so nothing changed. It is harmless, and scripts can ignore it because the exit code is still 0, but if you expected the rule to be new, check whether you’re looking at the runtime or the permanent config with and without --permanent.
curl: (7) Failed to connect versus curl: (28) Connection timed out
From the client, error 7 within milliseconds means something answered with a refusal: firewalld rejected it because the port isn’t open in the zone or a reject rule matched, or the port is open and nothing is listening, so the host sent a TCP reset. Check sudo ss -ltn on the server to tell those apart. A timeout means a drop rule matched, a rate limit ran out, or a forward is missing masquerading. Check sudo firewall-cmd --list-all for the first case and --list-rich-rules for the others.
Harden the setup before production
Firewalld on Debian 13 and Ubuntu 24.04 is a sound default once you know the two traps the lab hit: runtime rules that vanish on reload, and zone moves that silently close services. Before you rely on it:
- UFW disabled and its unit disabled
- every rule written with
--permanent, then reloaded - databases allowed by source rich rule, not by service
- SSH brute force handled by Fail2ban, not a rich rule limit
- no direct rules left from older guides
Pair it with SSH key-only logins and you have a server that exposes only what you opened. On Rocky Linux or Fedora the same commands apply with a different starting zone; see firewalld on Rocky Linux 10 and firewalld on Fedora, and the firewalld documentation has the full rich rule and policy syntax.