Sub topic 3
Checking What Is Currently Allowed
Before you open or block anything, find out what the firewall already does. This chapter shows how to read the state of firewalld with firewall-cmd: whether it runs, which zone applies to your traffic, and which services, ports, and rules that zone holds. The commands only read state, so you can run them on a production server without changing anything.
What this chapter assumes
- You can use the Linux command line and
sudo. - You know what a TCP or UDP port is, and that a service listens on one.
- You can manage services with
systemctl. - You have read the previous chapter on zones. In short, a zone is a named rule set. Firewalld matches each incoming packet to one zone by its source address, then by the interface it arrived on, and otherwise uses the default zone.
The inspection workflow
Inspection always follows the same order. Check that the daemon runs. Find out which zones are in use. Then read the rules of the zone that matters.
Step 1: Confirm firewalld is running
The first command group from this tutorial starts the daemon and inspects it:
sudo systemctl enable --now firewalld
sudo firewall-cmd --state
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-allenable --now does two things. It starts firewalld immediately and also makes it start at every boot. If the daemon is already running, the command changes nothing.
--state prints running and exits with status 0 when the daemon is up. When it is down, it prints not running and exits with a non-zero status (252). Scripts can test that exit status instead of parsing the text. Every other firewall-cmd call fails with a connection error while the daemon is stopped, so this is the first thing to check when a command behaves strangely.
Step 2: Find the zone that applies
Two commands answer two different questions.
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zones--get-default-zone prints the zone used for any traffic that no interface or source assignment claims. On many installations this is public.
--get-active-zones prints only the zones that currently have an interface or a source attached. Typical output looks like this:
public
interfaces: eth0
drop
sources: 203.0.113.0/24Read it as follows:
- A zone with
interfaces:handles all traffic that arrives on those network interfaces. - A zone with
sources:handles traffic from those addresses or networks, whatever interface it comes in on. - A zone that is not listed has no traffic assigned to it. Its rules exist but are not in effect.
The default zone is not always the zone that serves your traffic. If eth0 is assigned to internal while the default is public, then internal governs traffic arriving on eth0. In that case the public rules do nothing for that interface. Always compare the two outputs before you decide which zone to read or change.
You can also ask directly:
sudo firewall-cmd --get-zone-of-interface=eth0
sudo firewall-cmd --get-zone-of-source=203.0.113.10Firewalld prints the zone name. If nothing is assigned, the interface query prints no zone. The source query reports no zone unless the address falls inside a source entry that you added to a zone.
Step 3: Read all rules of one zone
--list-all prints everything configured for one zone. Without --zone, it shows the default zone. Name the zone explicitly when it is not the default:
sudo firewall-cmd --zone=public --list-allExample output (the exact set of lines varies with the firewalld version):
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: cockpit dhcpv6-client ssh
ports: 8080/tcp
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
rule family="ipv4" source address="203.0.113.10" port port="22" protocol="tcp" rejectThe fields that matter most for this tutorial:
| Field | Meaning |
|---|---|
| `(active)` | The zone has an interface or source attached. |
| `target` | What happens to traffic that no rule in the zone matches. `default` rejects it, with some ICMP exceptions. `DROP` discards it silently. `ACCEPT` allows it. |
| `interfaces` | Network interfaces assigned to the zone. |
| `sources` | Addresses or networks assigned to the zone. |
| `services` | Named services allowed in, such as `ssh` or `http`. |
| `ports` | Individual port and protocol pairs allowed in, such as `8080/tcp`. |
| `rich rules` | Extra rules with conditions, such as a specific source address. |
A service is a name for a stored group of ports and protocols. http stands for 80/tcp, and the firewall treats it as that port. The same port can therefore be open through the services line or the ports line, so check both when you ask whether something is allowed.
Two edge cases come up often:
- Empty fields are normal. An empty
sources:line just means no source assignment exists. - A zone only filters traffic that reaches it. A packet from an address assigned to
dropnever reaches thepublicrules, even ifpublicallows the port.
Step 4: Ask narrow questions
For a script or a quick check, use the query commands. They print yes or no, and they set the exit status to 0 for yes and 1 for no.
sudo firewall-cmd --query-service=ssh
sudo firewall-cmd --query-port=8080/tcp
sudo firewall-cmd --zone=public --query-service=httpThe list commands print only one field:
sudo firewall-cmd --list-services
sudo firewall-cmd --list-ports
sudo firewall-cmd --list-rich-rules
sudo firewall-cmd --list-sourcesAll of these default to the default zone unless you pass --zone=NAME. The --query-port form needs both the number and the protocol. --query-port=8080 fails because firewalld cannot tell whether you mean TCP or UDP.
Look at more than one zone
To see every zone in one pass, including inactive ones:
sudo firewall-cmd --list-all-zones
sudo firewall-cmd --get-zones--get-zones lists only the names. --list-all-zones prints a full block for each zone, which is long but useful when you inherit a server and want to know whether someone left a stray rule in a zone that is not active today. A rule in an inactive zone has no effect now, but it takes effect the moment an interface or source is assigned to that zone.
To see which services firewalld knows about, and what one of them means:
sudo firewall-cmd --get-services
sudo firewall-cmd --info-service=http--info-service prints the ports, protocols, and description behind a name. Use it to confirm exactly which ports a service opens before you allow it.
Runtime and permanent views can differ
Firewalld keeps two configurations: the runtime configuration, which is what filters packets right now, and the permanent configuration, which is stored on disk and loaded at start or reload. The next chapter covers them in full. For inspection, remember one rule: a list command reads the runtime configuration unless you add --permanent.
sudo firewall-cmd --zone=public --list-services
sudo firewall-cmd --permanent --zone=public --list-servicesIf the two outputs differ, someone made a change in only one place. A service that appears in the runtime list but not the permanent list will disappear at the next reload or reboot. A service that appears only in the permanent list is not active yet. To see the full difference in one command, compare the two views with diff:
diff <(sudo firewall-cmd --zone=public --list-all) \
<(sudo firewall-cmd --permanent --zone=public --list-all)This command uses process substitution, so run it in bash. No output means the two configurations match for that zone. A few lines can differ even when the rules match, such as the (active) marker, because the permanent view has no notion of active state.
Common errors when inspecting
FirewallD is not running. The daemon is stopped. Runsudo systemctl status firewalldto see why, and start it withsudo systemctl enable --now firewalld.command not found: firewall-cmd. The package is not installed. Install thefirewalldpackage with your distribution's package manager.- A service you opened is missing from the list. You probably looked at the wrong zone or the wrong configuration. Check
--get-active-zonesfirst, then compare the runtime and permanent lists. --list-allshowspublic, but your interface is in another zone. The default zone is not the zone in use. Pass--zone=with the name from--get-active-zones.--query-port=8080reports an error. Add the protocol, as in8080/tcp.- Permission denied. Without root privileges,
firewall-cmdmay refuse to answer or ask for authentication. Usesudo.
Exercise
You run sudo firewall-cmd --get-default-zone and get public. Then sudo firewall-cmd --get-active-zones prints internal with interfaces: eth0, and public does not appear. You run sudo firewall-cmd --list-all and see ssh but not http. Is the http port closed for clients arriving on eth0? What command do you run next?
Show answer
You cannot tell yet. --list-all without --zone shows the default zone, public, but eth0 belongs to internal, so the public rules do not apply to that traffic. Run sudo firewall-cmd --zone=internal --list-all (or --zone=internal --query-service=http) to read the zone that does apply.
Exercise
sudo firewall-cmd --zone=public --list-services shows http, but sudo firewall-cmd --permanent --zone=public --list-services does not. What does this mean for the server after the next reboot, and how can you list the difference in one command?
Show answer
The http service was added to the runtime configuration only. After a reboot, or after firewall-cmd --reload, the permanent configuration is loaded and http will no longer be allowed. Compare the two views with diff <(sudo firewall-cmd --zone=public --list-all) <(sudo firewall-cmd --permanent --zone=public --list-all).
Exercise
Write a command that prints yes or no for whether TCP port 443 is allowed in the zone dmz, and say how a script can use the result without reading the text.
Show answer
sudo firewall-cmd --zone=dmz --query-port=443/tcp. The command exits with status 0 for yes and 1 for no, so a script can test $? or put the command directly in an if statement. Note that this checks only the ports list, so if 443 is open through the https service, query that with --query-service=https as well.