cheat-sheets

firewalld

firewalld is the default firewall manager on Fedora, RHEL and derivatives. It groups interfaces and sources into zones, allows traffic by service or port, and applies changes without dropping existing connections. Use firewall-cmd to manage it; it drives nftables underneath (see nftables).

Documentation: firewalld documentation · firewall-cmd(1) Project: firewalld

Verification: options were checked against the firewall-cmd man page in Ubuntu 24.04 (firewalld). No rules were applied to a live firewall. Zone and service names (public, ssh, http) are firewalld defaults.

Runtime vs Permanent #

Changes without --permanent apply immediately and are lost on reload or reboot. With --permanent they are saved but only take effect after --reload.

COMMANDDESCRIPTION
sudo firewall-cmd --stateIs firewalld running
sudo firewall-cmd --reloadLoad permanent config; keeps connections, drops runtime-only changes
sudo firewall-cmd --runtime-to-permanentSave the current runtime config
sudo firewall-cmd --list-allActive default zone: services, ports, rich rules
sudo firewall-cmd --list-all-zonesEvery zone
sudo firewall-cmd --get-default-zoneDefault zone
sudo firewall-cmd --get-active-zonesZones with interfaces or sources

Zones #

COMMANDDESCRIPTION
sudo firewall-cmd --get-zonesAll zone names
sudo firewall-cmd --set-default-zone=publicChange the default zone
sudo firewall-cmd --zone=public --list-allOne zone
sudo firewall-cmd --zone=internal --change-interface=<IF> --permanentMove an interface to a zone
sudo firewall-cmd --zone=internal --add-source=192.0.2.0/24 --permanentTreat a network as part of a zone
sudo firewall-cmd --get-zone-of-interface=<IF>Which zone an interface is in
sudo firewall-cmd --new-zone=<ZONE> --permanentCreate a custom zone (needs --reload)

Services and Ports #

COMMANDDESCRIPTION
sudo firewall-cmd --get-servicesPredefined service names
sudo firewall-cmd --add-service=https --permanentAllow a service in the default zone
sudo firewall-cmd --zone=internal --add-service=ssh --permanentIn a chosen zone
sudo firewall-cmd --remove-service=http --permanentRemove a service
sudo firewall-cmd --add-port=8080/tcp --permanentAllow a port
sudo firewall-cmd --add-port=60000-60010/udp --permanentAllow a port range
sudo firewall-cmd --remove-port=8080/tcp --permanentRemove a port
sudo firewall-cmd --query-service=httpsIs a service allowed (prints yes or no)
sudo firewall-cmd --add-service=https --timeout=300Temporary rule that expires after 300 seconds

Rich Rules #

bash
# allow SSH only from one network
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.0.2.0/24" service name="ssh" accept'

# reject traffic from one address
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.20" reject'

sudo firewall-cmd --list-rich-rules
sudo firewall-cmd --reload

Forwarding and NAT #

COMMANDDESCRIPTION
sudo firewall-cmd --add-masquerade --permanentMasquerade (source NAT) outgoing traffic in the zone
sudo firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080 --permanentRedirect a local port
sudo firewall-cmd --add-forward-port=port=80:proto=tcp:toport=80:toaddr=192.0.2.20 --permanentForward to another host (needs masquerade)
sudo firewall-cmd --list-forward-portsShow forwards

Safety Net #

Before locking down a remote host, test with a timeout so a mistake reverts itself, and use panic mode only at the console:

COMMANDDESCRIPTION
sudo firewall-cmd --add-service=<SERVICE> --timeout=60Rule that disappears after 60 seconds
sudo firewall-cmd --panic-on / --panic-offDrop all traffic / restore (cuts your SSH session)

Docker #

Docker writes its own nftables or iptables rules and can bypass zone rules for published ports. See Docker Troubleshooting before relying on firewalld to close a published container port.

Troubleshooting #

SYMPTOMFIX
Rule gone after rebootIt was runtime-only: repeat with --permanent or --runtime-to-permanent
Permanent rule not activeRun sudo firewall-cmd --reload
Service reachable from the wrong networkCheck the interface’s zone: --get-active-zones
FirewallD is not runningsudo systemctl enable --now firewalld
Unsure what is blockedsudo firewall-cmd --list-all; logs: sudo firewall-cmd --set-log-denied=all then journalctl -k

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.