Skip to main content

firewalld Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for managing firewall rules with firewalld on Linux

firewalld is a dynamic firewall manager used on Fedora, RHEL, CentOS, and their derivatives. It organizes rules into zones and supports both runtime and permanent configuration. This cheatsheet covers service management, zones, ports, rich rules, and masquerading.

Basic Commands

Start, stop, and reload the firewalld service.

CommandDescription
firewall-cmd --stateCheck if firewalld is running
sudo systemctl start firewalldStart the service
sudo systemctl stop firewalldStop the service
sudo systemctl enable firewalldEnable at boot
sudo systemctl disable firewalldDisable at boot
sudo firewall-cmd --reloadReload rules without dropping connections
sudo firewall-cmd --complete-reloadFull reload, resets all connections

Runtime vs Permanent

By default, firewall-cmd changes apply at runtime only and are lost on reload. Add --permanent to persist a rule, then reload to activate it.

CommandDescription
sudo firewall-cmd --add-service=httpAllow HTTP (runtime only)
sudo firewall-cmd --add-service=http --permanentAllow HTTP (survives reload)
sudo firewall-cmd --reloadActivate permanent rules
sudo firewall-cmd --runtime-to-permanentSave all runtime rules as permanent

Zones

Zones define trust levels for network connections. Each interface belongs to one zone.

CommandDescription
firewall-cmd --get-zonesList all available zones
firewall-cmd --get-default-zoneShow the default zone
sudo firewall-cmd --set-default-zone=publicSet the default zone
firewall-cmd --get-active-zonesShow active zones and their interfaces
firewall-cmd --zone=public --list-allList all settings for a zone
sudo firewall-cmd --zone=public --change-interface=eth0Assign interface to zone (runtime)
sudo firewall-cmd --zone=public --add-interface=eth0 --permanentAssign interface permanently
sudo firewall-cmd --zone=public --remove-interface=eth0Remove interface from zone

Services

Allow or block named services defined in /usr/lib/firewalld/services/.

CommandDescription
firewall-cmd --get-servicesList all predefined services
firewall-cmd --zone=public --list-servicesList services allowed in zone
firewall-cmd --info-service=httpShow ports and protocols for a service
sudo firewall-cmd --zone=public --add-service=http --permanentAllow service permanently
sudo firewall-cmd --zone=public --remove-service=http --permanentRemove service

Ports

Open or close individual ports when no predefined service exists.

CommandDescription
firewall-cmd --zone=public --list-portsList open ports in zone
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanentOpen a TCP port
sudo firewall-cmd --zone=public --add-port=4000-4500/tcp --permanentOpen a port range
sudo firewall-cmd --zone=public --remove-port=8080/tcp --permanentClose a port

Rich Rules

Rich rules allow fine-grained control over source, destination, port, and action.

CommandDescription
firewall-cmd --zone=public --list-rich-rulesList rich rules in zone
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" accept' --permanentAllow traffic from subnet
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="203.0.113.10" reject' --permanentReject traffic from IP
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port port="22" protocol="tcp" accept' --permanentAllow SSH from subnet
sudo firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="203.0.113.10" reject' --permanentRemove a rich rule

Masquerade (NAT)

Masquerading lets machines on a private network reach the internet through the firewall host.

CommandDescription
firewall-cmd --zone=public --query-masqueradeCheck if masquerading is enabled
sudo firewall-cmd --zone=public --add-masquerade --permanentEnable masquerading
sudo firewall-cmd --zone=public --remove-masquerade --permanentDisable masquerading

Logging

Control which denied packets are logged to help with debugging.

CommandDescription
firewall-cmd --get-log-deniedShow current log-denied setting
sudo firewall-cmd --set-log-denied=allLog all denied packets
sudo firewall-cmd --set-log-denied=unicastLog denied unicast only
sudo firewall-cmd --set-log-denied=offDisable denied-packet logging

Common Server Setup

Baseline rules for a web server using firewalld.

CommandDescription
sudo firewall-cmd --set-default-zone=publicSet zone to public
sudo firewall-cmd --zone=public --add-service=ssh --permanentKeep SSH access
sudo firewall-cmd --zone=public --add-service=http --permanentAllow HTTP
sudo firewall-cmd --zone=public --add-service=https --permanentAllow HTTPS
sudo firewall-cmd --reloadActivate all permanent rules
firewall-cmd --zone=public --list-allVerify active rules