firewalld commands cheat sheet
firewalld status and startup
-
sudo systemctl status firewalldShows whether the firewalld service is running and whether it's set to start automatically at boot.
-
sudo systemctl enable --now firewalldStarts firewalld now and sets it to start automatically at boot. Caution: on a remote server, first check that the default zone allows SSH, as explained below.
-
firewall-cmd --stateChecks whether the firewalld service is running. When it is, the output is running.
Example:
running -
sudo firewall-cmd --reloadReloads the firewall and keeps active connections. The permanent settings become the active settings, and runtime changes that weren't saved as permanent are lost.
-
sudo firewall-cmd --complete-reloadFully reloads the firewall, including the kernel modules. Caution: according to the documentation, this is likely to drop active connections, so use it only for serious firewall problems.
-
sudo firewall-cmd --check-configChecks that the permanent settings are valid, including the structure of the XML files, before you reload.
-
firewall-cmd --versionShows the installed firewalld version.
Runtime and permanent settings
-
sudo firewall-cmd --add-service=httpWithout --permanent, the change is runtime only: it takes effect right away but is lost when the firewall reloads, the service restarts, or the server reboots.
-
sudo firewall-cmd --permanent --add-service=httpWith --permanent, the change is saved to the permanent settings but doesn't take effect until the next reload, so run sudo firewall-cmd --reload after it.
-
sudo firewall-cmd --runtime-to-permanentSaves all the active runtime settings as permanent settings, replacing the existing permanent settings. A convenient way to work: make runtime changes, check that everything works, and only then save.
-
sudo firewall-cmd --permanent --list-allShows the permanent settings of the default zone. Comparing it with the output of sudo firewall-cmd --list-all shows whether there are runtime changes that haven't been saved yet.
-
sudo firewall-cmd --add-port=8080/tcp --timeout=10mOpens a port for a limited time, here 10 minutes, and the rule is removed automatically when the time is up. The units are s for seconds, m for minutes, and h for hours. --timeout can't be combined with --permanent.
Zones
-
sudo firewall-cmd --get-default-zoneShows the default zone, which applies to interfaces and connections that aren't assigned to another zone. On a standard installation, this is the public zone. A command without --zone applies to the default zone.
Example:
public -
sudo firewall-cmd --get-active-zonesShows the active zones and the interfaces and source addresses assigned to each one.
-
sudo firewall-cmd --get-zonesLists the zones defined on the server.
-
sudo firewall-cmd --list-allShows everything set in the default zone: interfaces, source addresses, services, ports, port forwarding, and rich rules.
-
sudo firewall-cmd --zone=internal --list-allShows the settings of a specific zone, here internal.
-
sudo firewall-cmd --list-all-zonesShows the settings of all the zones on the server.
-
sudo firewall-cmd --get-zone-of-interface=eth0Shows which zone the eth0 network interface is assigned to. The ip addr command shows the interface names on the server.
-
sudo firewall-cmd --set-default-zone=publicSets the default zone. Caution: interfaces without an explicit zone move to the new zone, so on a remote server first check with sudo firewall-cmd --zone=public --list-all, using the new zone's name, that it allows SSH.
-
sudo firewall-cmd --permanent --zone=internal --change-interface=eth1Moves the eth1 interface to the internal zone. When NetworkManager manages the interface, the zone is changed in the connection settings. Caution: don't move the interface you're connected through over SSH to a zone that doesn't allow SSH.
-
sudo firewall-cmd --permanent --zone=trusted --add-source=203.0.113.10Assigns a source address to a zone, so traffic from it is handled by that zone's rules. In the trusted zone, all traffic from the address is allowed, so assign only known addresses to it, such as your office's static address. You can also give a network in CIDR notation, but not a hostname.
-
sudo firewall-cmd --permanent --zone=trusted --remove-source=203.0.113.10Removes the source address from the zone.
-
sudo firewall-cmd --get-zone-of-source=203.0.113.10Shows which zone a source address is assigned to.
Predefined zones
-
publicFor public networks, where you don't trust the other computers on the network. Only selected incoming connections are accepted, and this is the default zone. On most installations, the ssh service is already enabled in it.
-
dropEvery incoming packet is dropped without a reply, and only outgoing connections from the server are possible.
-
blockEvery incoming connection is rejected with an icmp-host-prohibited message on IPv4, and only connections the server started are possible.
-
trustedAll connections are accepted. Suitable only for fully known addresses or interfaces, such as an isolated internal network.
-
internalFor internal networks, where you partly trust the other computers. Only selected incoming connections are accepted. The home and work zones are similar.
-
externalFor external networks when the server acts as a router, with masquerading (address translation) enabled. Only selected incoming connections are accepted.
-
sudo firewall-cmd --permanent --zone=public --set-target=DROPSets what happens to traffic that matches no rule in the zone: default, ACCEPT, DROP, or REJECT. This setting is permanent only and takes effect after a reload. Caution: first make sure SSH is allowed in the zone.
Services
-
sudo firewall-cmd --get-servicesLists all the predefined services, such as ssh, http, https, and mysql. A service is a name for a group of ports and protocols.
-
sudo firewall-cmd --list-servicesLists the services allowed in the default zone.
-
sudo firewall-cmd --permanent --add-service=httpsAllows the service in the zone, here HTTPS on port 443. After the command, reload with sudo firewall-cmd --reload. You can repeat --add-service several times in one command.
-
sudo firewall-cmd --permanent --remove-service=httpRemoves a service from the zone. Caution: don't remove the ssh service on a remote server before another way to connect is in place, such as a rule for a specific IP address.
-
sudo firewall-cmd --query-service=sshChecks whether the service is allowed in the zone. The exit code is 0 when it's allowed and 1 when it isn't.
-
sudo firewall-cmd --info-service=httpsShows a service's details, including the ports and protocols it opens.
-
/etc/firewalld/servicesThe built-in service definitions are in the /usr/lib/firewalld/services directory. Don't edit them, because a package update replaces them. To change a service, copy its file to the /etc/firewalld/services directory and edit the copy, which takes precedence over the original file.
-
sudo firewall-cmd --permanent --new-service=myappCreates a new, empty service named myapp in the permanent settings.
-
sudo firewall-cmd --permanent --service=myapp --add-port=9000/tcpAdds a port to the new service. After a reload, you can allow it like any other service, with --add-service=myapp.
Ports
-
sudo firewall-cmd --permanent --add-port=8080/tcpOpens TCP port 8080 in the default zone. The protocol is required: tcp, udp, sctp, or dccp. Reload after the command.
-
sudo firewall-cmd --permanent --add-port=6000-6007/tcpOpens a port range, including the first and last ports. The range is written with a hyphen.
-
sudo firewall-cmd --permanent --add-port=2222/tcpOpens a port for an SSH service set to a different port, here 2222. Add the rule and reload before you change the port in the SSH service settings.
-
sudo firewall-cmd --permanent --remove-port=8080/tcpCloses a port opened with --add-port.
-
sudo firewall-cmd --list-portsLists the ports opened in the zone with --add-port. Allowed services aren't shown here; they appear in the output of --list-services.
-
sudo firewall-cmd --query-port=8080/tcpChecks whether the port is open in the zone. The exit code is 0 when it's open and 1 when it isn't.
Rich rules
-
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="22" protocol="tcp" accept'Allows SSH from one IP address. A rule with a source or destination address must include family, ipv4 or ipv6.
-
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="mysql" accept'Allows access to the mysql service (port 3306) only from an internal network in CIDR notation.
-
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.7" reject'Rejects all connections from one IP address and tells the sender the connection was refused.
-
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.0/24" drop'Silently drops all traffic from an entire network, with no reply to the sender.
-
sudo firewall-cmd --permanent --add-rich-rule='rule priority="-10" family="ipv4" source address="198.51.100.7" drop'Priority sets the order in which rules are checked: a lower value is checked first, and a negative value is checked before the zone's services and ports. This way, the block also applies to ports that are open to everyone.
-
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv6" source address="2001:db8::/32" service name="ssh" reject'Rejects SSH from an IPv6 address range. The syntax is the same as for IPv4, with family="ipv6".
-
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" service name="ssh" log prefix="ssh-office " level="info" limit value="1/m" accept'Allows SSH from a specific network and logs the connections, with a prefix to identify them. limit restricts logging to one entry per minute, and the units are s, m, h, and d.
-
sudo firewall-cmd --list-rich-rulesLists the rich rules in the zone, one rule per line.
-
sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="198.51.100.7" reject'Removes a rich rule. The wording must match the rule exactly, so it's easiest to copy it from the output of --list-rich-rules.
Masquerading and port forwarding
-
sudo firewall-cmd --permanent --zone=public --add-masqueradeEnables IPv4 masquerading in the zone, so traffic from an internal network goes out with the server's address. Packet forwarding (IP forwarding) is enabled automatically.
-
sudo firewall-cmd --zone=public --query-masqueradeChecks whether masquerading is enabled in the zone. In the external zone, it's already enabled in the zone settings.
-
sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080Forwards incoming traffic from port 80 to port 8080 on the same server.
-
sudo firewall-cmd --permanent --add-forward-port=port=2222:proto=tcp:toport=22:toaddr=192.168.1.20Forwards traffic from port 2222 to port 22 on another server on the internal network. With toaddr, packet forwarding is enabled automatically.
-
sudo firewall-cmd --list-forward-portsLists the port forwards in the zone. Port forwarding and masquerading with these commands are for IPv4; for IPv6, you set them up with rich rules.
Logging blocked traffic
-
sudo firewall-cmd --get-log-deniedShows the logging setting for blocked packets. The default is off, meaning nothing is logged.
Example:
off -
sudo firewall-cmd --set-log-denied=allLogs packets that were rejected or dropped. The possible values are all, unicast, broadcast, multicast, and off. Caution: on a server that gets a lot of scans, the log grows fast.
Panic mode
-
sudo firewall-cmd --panic-onBlocks all incoming and outgoing traffic, and active connections time out. Caution: on a remote server, the SSH connection drops too, and you can get back to the server only through its console.
-
sudo firewall-cmd --panic-offTurns panic mode off. If it was on only briefly, existing connections may resume.
-
sudo firewall-cmd --query-panicChecks whether panic mode is on.
nftables and service settings
-
FirewallBackend=nftablesThe setting in /etc/firewalld/firewalld.conf that sets which kernel mechanism holds the rules. The default is nftables, and the iptables option is deprecated and will be removed in a future version.
-
DefaultZone=publicThe setting in the same file that sets the default zone. It's easier to change it with the firewall-cmd --set-default-zone command.
-
sudo nft list rulesetShows all the rules at the nftables level, including the rules firewalld created. Use it for checking only, and make changes with firewall-cmd.
No matching commands.
Before changing the firewall on a remote server
- Test first, then save: a change without --permanent takes effect right away and is lost on a reload or reboot. If a runtime change blocked your connection, rebooting the server restores the permanent settings. Save with --runtime-to-permanent only after testing.
- Permanent changes need a reload: a command with --permanent doesn't change the active firewall until you run sudo firewall-cmd --reload.
- SSH in the right zone: before you change the default zone, move an interface to another zone, change the target, or remove a service, check with --list-all that the zone you're connected through still allows SSH, with the ssh service or the matching port.
- Keep a session open: after changing rules, keep your current SSH session open and test logging in from a new window. This way, you can fix a mistake without losing access to the server.
Direct rules and policies
The firewalld direct interface, where you write iptables rules as they are, is deprecated and will be removed in a future version. Instead, you define policies, which set rules for traffic between zones.
The IP addresses on this page, such as 203.0.113.10 and 198.51.100.7, belong to ranges reserved for documentation; replace them with your real addresses. Network notation such as 192.168.1.0/24 is explained in the CIDR table, and the private and reserved ranges are listed on the reserved IP ranges page. On Ubuntu and Debian servers, the firewall is usually managed with UFW, and its commands are on the UFW commands page.
On a Linux VPS, you connect over SSH with full root access and can choose an RHEL-family distribution such as AlmaLinux or Rocky Linux. You configure the server's firewall with every command on this page: zones, services, ports, rules by IP address, and port forwarding.
firewalld FAQ
How do I open a port in firewalld?
Run sudo firewall-cmd --permanent --add-port with the port number and protocol, for example sudo firewall-cmd --permanent --add-port=8080/tcp, and then reload with sudo firewall-cmd --reload. For a well-known service, it's easier to use its name, for example sudo firewall-cmd --permanent --add-service=https. When you're done, check the result with sudo firewall-cmd --list-all.
What's the difference between runtime and permanent settings in firewalld?
A runtime setting takes effect right away but is lost on a reload or reboot. A permanent setting is saved to the configuration files and takes effect only after sudo firewall-cmd --reload. A safe approach is to make a runtime change, check that the server is reachable and the services work, and only then save all the changes with sudo firewall-cmd --runtime-to-permanent.
What is a zone in firewalld?
A zone sets the level of trust for traffic that comes from a network interface or a source address, and which services and ports are allowed in it. On a server with one network interface, the default public zone is usually enough, with only the services you need allowed in it. You can assign known addresses, such as your office's static address, to a zone with broader permissions.
How do I allow SSH only from a specific IP address in firewalld?
Add a rich rule for the address, for example sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" service name="ssh" accept', and reload. After you test logging in from that address in a new window, remove the general ssh service with sudo firewall-cmd --permanent --remove-service=ssh and reload again. First make sure the address you connect from is static, so access to the server stays available.
What's the difference between firewalld and UFW?
Both tools manage the Linux firewall with simple commands. firewalld is common on RHEL-family distributions, such as AlmaLinux and Rocky Linux, and works with zones and with runtime and permanent settings. UFW is common on Ubuntu and Debian and works with a simple list of rules. The UFW commands are on the UFW commands page.