firewall-cmd (firewalld) (1/2)
firewalld is a background firewall daemon and firewall-cmd is its client, talking over D-Bus. You describe rules in terms of zones and services and firewalld turns them into nftables rules in the kernel. It is the default firewall on RHEL, Fedora, Rocky and openSUSE.
107.1 Why is it used?
- Zones are trust levels (block ... trusted): bind an interface or source to a zone instead of writing per-rule logic.
- Services (https, ssh) are named port bundles, easier than raw ports.
- Runtime changes apply instantly without dropping connections; permanent config survives reboot.
- Built-in port forwarding, masquerade (NAT), rich rules and zone-to-zone policies.
- Linux only (on Debian/Ubuntu: apt install firewalld); not on Windows/macOS.
107.2 How a packet's zone is chosen
- Step 1
1.Packet arrives at the firewalld server (192.168.127.10)
- Step 2
2.1. Source binding? admin PC 192.168.127.5 -> zone trusted (everything allowed)
- Step 3
3.2. Else interface: eth0 -> zone public
- Step 4
4.3. Else the default zone (usually public)
- Step 5
5.In public: https allowed -> web app; port 3306 not allowed -> rejected
Inside the zone: services, ports, rich rules allow; the target handles the rest
107.3 Runtime vs permanent
| Point | Runtime (no --permanent) | Permanent (--permanent) |
|---|---|---|
| Active | Immediately | Only after --reload |
| Stored in | Running daemon memory | /etc/firewalld/zones/*.xml |
| Survives reload/reboot | No | Yes |
| --timeout | Supported | Not supported |
- Safe way: run the command both ways, or test in runtime and then --runtime-to-permanent.
- --reload turns permanent into runtime and keeps state; --complete-reload also reloads modules and can break connections.
107.4 Lab: status, reload and log denied
firewalld needs conntrack, reject and fib modules that are not loaded in the Docker VM, so it was started without NET_ADMIN and exited. The rest of the lab uses firewall-offline-cmd, which takes the same options and writes the permanent XML files without a daemon.
firewalld --nofork --nopidgrep -o 'cache initialization.*' \ /var/log/firewalld | tail -1grep -o 'All hope is lost.*' \ /var/log/firewalld | tail -1firewall-cmd --state; echo $?firewall-cmd -V; echo $?firewall-offline-cmd -Vfirewall-offline-cmd --check-config; echo $?cache initialization failed: Operation not permittedAll hope is lost. Exiting.$ firewall-cmd --state; echo $?not running252$ firewall-cmd -V; echo $?FirewallD is not running252$ firewall-offline-cmd -V1.3.3$ firewall-offline-cmd --check-config; echo $?0firewall-offline-cmd --get-log-deniedfirewall-offline-cmd --set-log-denied=unicastfirewall-offline-cmd --get-log-deniedgrep LogDenied /etc/firewalld/firewalld.conf$ firewall-offline-cmd --get-log-deniedoff$ firewall-offline-cmd --set-log-denied=unicastsuccess$ firewall-offline-cmd --get-log-deniedunicast$ grep LogDenied /etc/firewalld/firewalld.confLogDenied=unicast- --state: exit 0 if running, else 252 (useful in scripts). -q hides messages like success.
- log-denied values: all, unicast, broadcast, multicast, off (default).
107.5 Zones
firewall-offline-cmd --get-default-zonefirewall-offline-cmd --get-zonesfirewall-offline-cmd --new-zone=labfirewall-offline-cmd --path-zone=labfirewall-offline-cmd --zone=public --list-allfirewall-offline-cmd --get-active-zones$ firewall-offline-cmd --get-default-zonepublic$ firewall-offline-cmd --get-zonesblock dmz drop external home internal publictrusted work$ firewall-offline-cmd --new-zone=labsuccess$ firewall-offline-cmd --path-zone=lab/etc/firewalld/zones/lab.xml$ firewall-offline-cmd --zone=public --list-allpublic target: default interfaces: services: dhcpv6-client ssh ports: forward: yes masquerade: no$ firewall-offline-cmd --get-active-zonesusage: see firewall-offline-cmd man pagefirewall-offline-cmd: error: unrecognized arguments: --get-active-zones- Other zone options: --set-default-zone, --get-zone-of-interface, --get-zone-of-source, --info-zone, --list-all-zones, --delete-zone, --load-zone-defaults.
- --get-active-zones is runtime only, so the offline tool rejects it.
107.6 Services, ports, protocols and ICMP blocks
firewall-offline-cmd --zone=public \ --add-service=httpsfirewall-offline-cmd --zone=public \ --add-port=8080/tcpfirewall-offline-cmd --zone=public \ --query-service=https; echo $?firewall-offline-cmd --zone=public \ --query-port=9090/tcp; echo $?firewall-offline-cmd -q --zone=public \ --add-service=http; echo $?firewall-offline-cmd --zone=public \ --add-service=http --timeout=20m$ ... --add-service=httpssuccess$ ... --add-port=8080/tcpsuccess$ ... --query-service=https; echo $?yes0$ ... --query-port=9090/tcp; echo $?no1$ ... -q ... --add-service=http; echo $?0$ ... --add-service=http --timeout=20musage: see firewall-offline-cmd man pagefirewall-offline-cmd: error: unrecognized arguments: --timeout=20mfirewall-offline-cmd --zone=public \ --add-protocol=grefirewall-offline-cmd --zone=public \ --add-source-port=53/udpfirewall-offline-cmd --zone=public \ --add-icmp-block=echo-requestfirewall-offline-cmd --zone=public \ --add-icmp-block-inversionfirewall-offline-cmd --zone=public \ --query-icmp-block-inversion; echo $?| Item | Add example | Notes |
|---|---|---|
| Service | --add-service=https | Named bundle of ports |
| Port | --add-port=8080/tcp, 5000-5100/udp | Ranges allowed |
| Protocol | --add-protocol=gre | Whole protocol, no port |
| Source port | --add-source-port=53/udp | Matches the packet's source port |
| ICMP block | --add-icmp-block=echo-request | Blocks ping; --add-icmp-block-inversion flips the list |
- Every item has --list-, --remove- and --query- forms; query returns exit 0 (yes) or 1 (no).
- --timeout=20m is runtime only: the rule removes itself.
107.7 Port forwarding, masquerade and rich rules
- Step 1
1.Internet client connects to port 80 of firewalld (192.168.127.10)
- Step 2
2.forward-port changes the destination to 192.168.127.20 and forwards it
- Step 3
3.App server answers through firewalld
- Step 4
4.LAN PC 192.168.127.30 sends to the Internet
- Step 5
5.masquerade replaces the source IP with firewalld's own IP (NAT)
firewall-offline-cmd --zone=public \ --add-forward-port \ port=80:proto=tcp:toaddr=192.168.127.20firewall-offline-cmd --zone=public \ --add-forward-port \ port=8080:proto=tcp:toport=80firewall-offline-cmd --zone=public \ --list-forward-portsfirewall-offline-cmd --zone=public \ --add-masqueradefirewall-offline-cmd --zone=public \ --query-masquerade; echo $?$ ... --list-forward-portsport=80:proto=tcp:toport=:toaddr=192.168.127.20port=8080:proto=tcp:toport=80:toaddr=$ ... --add-masqueradesuccess$ ... --query-masquerade; echo $?yes0- forward-port and masquerade are IPv4 only; for IPv6 use rich rules.
- Forms: :toport=8080 (local port), :toaddr=IP (another host), both together, or ranges port=5000-5010 toport=6000-6010.
107.8 Rich rules and bindings
firewall-offline-cmd --zone=public \ --add-rich-rule 'rule family=ipv4 source address=192.168.127.66 drop'firewall-offline-cmd --zone=public \ --add-interface=eth0firewall-offline-cmd --zone=trusted \ --add-source=192.168.127.5firewall-offline-cmd \ --get-zone-of-interface=eth0firewall-offline-cmd \ --get-zone-of-source=192.168.127.5firewall-offline-cmd --zone=public --list-all$ firewall-offline-cmd --get-zone-of-interface=eth0public$ firewall-offline-cmd --get-zone-of-source=192.168.127.5trusted$ firewall-offline-cmd --zone=public --list-allpublic (active) target: default icmp-block-inversion: yes interfaces: eth0 services: dhcpv6-client http https ssh ports: 8080/tcp protocols: gre masquerade: yes source-ports: 53/udp icmp-blocks: echo-request rich rules: rule family="ipv4" source address="192.168.127.66" drop- A source can be an IP, a network, a MAC or ipset:NAME, never a hostname; --change-interface moves an interface to another zone.
- Zone target: default (rejects the rest), ACCEPT, DROP (no reply), REJECT; CONTINUE is for policies only.
- --set-target=DROP worked; --get-priority errored because zone priorities arrived in firewalld 2.0 (lab has 1.3.3).
107.9 Policies: traffic between zones
- Step 1
1.LAN PC is in zone internal
- Step 2
2.Packet is forwarded towards the Internet (zone public)
- Step 3
3.Policy lan-to-wan: ingress internal, egress public
- Step 4
4.Target ACCEPT, priority -100 (checked before zones)
- Step 5
5.Packet reaches the Internet
firewall-offline-cmd --get-policiesfirewall-offline-cmd --new-policy=lan-to-wanfirewall-offline-cmd --policy=lan-to-wan \ --add-ingress-zone=internalfirewall-offline-cmd --policy=lan-to-wan \ --add-egress-zone=publicfirewall-offline-cmd --policy=lan-to-wan \ --set-target=ACCEPTfirewall-offline-cmd --policy=lan-to-wan \ --set-priority=-100firewall-offline-cmd --info-policy=lan-to-wan$ firewall-offline-cmd --get-policiesallow-host-ipv6$ firewall-offline-cmd --info-policy=lan-to-wanlan-to-wan (active) priority: -100 target: ACCEPT ingress-zones: internal egress-zones: public masquerade: no- Ingress/egress can be HOST (this machine) or ANY (any zone).
- Priority -32768..32767, default -1: below 0 runs before zones, above 0 after.
107.10 Common mistakes
- Adding a service without --permanent: works now, gone after reboot. Fix with --runtime-to-permanent.
- Thinking --permanent is active at once: it only writes XML; run --reload.
- Using --timeout with --permanent: timeout is runtime only.
- Expecting forward-port/masquerade for IPv6: use rich rules.
107.11 Interview tips
- Zone selection: 1) source binding, 2) interface, 3) default zone; then services/ports/rich rules, rest by target.
- Runtime = now, lost on reload; permanent = XML, active after reload.
- firewall-cmd -> D-Bus -> firewalld -> nftables in the kernel.
- Zones decide where traffic came from; policies control traffic between zones.