Sub topic 2
Zones: how traffic is matched to a rule set
This chapter assumes you can use sudo on the command line and know what a listening TCP or UDP port is. It also assumes you can read an IPv4 address in CIDR notation, such as 203.0.113.0/24, and that firewalld is installed and running. The previous chapter covered how firewall-cmd talks to the firewalld daemon. Here you learn how firewalld decides which rules apply to a given packet.
Why zones exist
A server rarely treats all traffic the same way. Traffic from the office network may be allowed to reach a database port, while the same port should be closed to the internet. firewalld handles this with zones. A zone is a named set of rules. It holds a list of allowed services, ports, ICMP types, rich rules, and a default action for everything not listed.
Every incoming packet is assigned to exactly one zone. After that, only that zone's rules decide whether the packet is accepted. Rules in other zones are not consulted. This means a service you opened in the internal zone does nothing for a packet that was matched to public.
How a packet is matched to a zone
firewalld checks three things in a fixed order and uses the first that matches:
- Source address. If the packet's source address or network is bound to a zone, that zone is used.
- Incoming interface. If no source matched, the zone bound to the network interface the packet arrived on is used.
- Default zone. If neither matched, the packet goes to the default zone.
Source matching comes first. This is why a source assigned to the drop zone is discarded even when the packet arrives on an interface in the public zone. It is also why source-based zones are the tool for blocking or trusting specific networks.
An interface or source can belong to only one zone at a time. The default zone is different. It is a fallback, not a binding, so it also covers any interface that is not assigned to a zone.
The predefined zones
firewalld ships with nine zones. Their names suggest a level of trust, but the real behavior is defined by their rules. The table summarizes the typical differences. Exact contents can vary between distributions and versions, so check your own system with firewall-cmd --info-zone=NAME.
| Zone | Intended use | Behavior for unlisted incoming traffic |
|---|---|---|
| `drop` | Hostile networks | Dropped silently, with no reply. Only outgoing connections work. |
| `block` | Networks you refuse explicitly | Rejected with an ICMP prohibited message. |
| `public` | Untrusted networks such as the internet | Rejected. Only selected services are allowed, typically `ssh`. |
| `external` | Outward-facing side of a router doing masquerading | Rejected. Only selected services are allowed. |
| `dmz` | Servers reachable from outside but isolated from the inside network | Rejected. Only selected services are allowed. |
| `work` | Office network | Rejected. A few services such as `ssh` are allowed. |
| `home` | Home network | Rejected. A few more services are allowed. |
| `internal` | Internal network you mostly trust | Similar to `home`. |
| `trusted` | Fully trusted | All traffic accepted. |
On most server installs the default zone is public. It is a sensible default for a machine on an untrusted network, and you will do most of your work in it.
Targets: what happens to traffic no rule matches
Each zone has a target that sets the action for packets that match none of its rules.
defaultrejects the packet with an ICMP error, except for traffic that firewalld always permits, such as replies to connections you started and some ICMP types. Most zones use this.ACCEPTaccepts everything. Thetrustedzone uses it.REJECT(shown as%%REJECT%%) rejects the packet and sends an error back.DROPdiscards the packet with no reply. Thedropzone uses it.
The difference between reject and drop matters when you test. A rejected connection fails immediately with "connection refused" or "no route to host". A dropped connection hangs until it times out. A hang therefore suggests a drop rule, or a firewall elsewhere on the path, while an instant refusal suggests a reject or a closed port.
Inspecting zones
These commands show the zone configuration without changing anything.
# Zone used when nothing else matches
sudo firewall-cmd --get-default-zone
# Zones that currently have an interface or source bound to them
sudo firewall-cmd --get-active-zones
# Which zone an interface or source belongs to
sudo firewall-cmd --get-zone-of-interface=eth0
sudo firewall-cmd --get-zone-of-source=203.0.113.0/24
# Everything configured in one zone, including its target
sudo firewall-cmd --info-zone=public
# All zones that exist, with all of their rules
sudo firewall-cmd --list-all-zonesA typical --get-active-zones result looks like this:
drop
sources: 203.0.113.0/24
public (default)
interfaces: eth0Read this as follows. Traffic from 203.0.113.0/24 goes to drop, because source matching comes first. Other traffic arriving on eth0 goes to public. The (default) marker shows which zone is the fallback.
A zone with no interface and no source does not appear in this list. It still exists, but nothing is matched to it, so its rules have no effect. That is a common reason a freshly opened service seems to be ignored.
The zone you did not name
When you run a firewall-cmd command without --zone=, it acts on the default zone. This is correct only if your interface is actually in the default zone.
Suppose eth0 is bound to internal while public is the default zone. Running sudo firewall-cmd --add-service=https opens HTTPS in public. Traffic arriving on eth0 is matched to internal, so it is still blocked. The command succeeded, but the rule is in the wrong place.
The safe habit is to look up the zone first, then name it explicitly:
zone=$(sudo firewall-cmd --get-zone-of-interface=eth0)
echo "eth0 is in zone: $zone"
sudo firewall-cmd --zone="$zone" --list-allThe rest of this tutorial usually omits --zone for brevity, as the spine examples do. On a server with more than one zone in use, add it.
Changing which zone an interface or source uses
Three operations change zone membership:
# Move an interface to another zone (runtime only)
sudo firewall-cmd --zone=internal --change-interface=eth1
# Bind a source network to a zone, saved permanently
sudo firewall-cmd --permanent --zone=internal --add-source=192.168.10.0/24
# Change the fallback zone (takes effect now and is saved)
sudo firewall-cmd --set-default-zone=publicSome details to keep in mind:
--change-interfacemoves the interface if it is already in a different zone.--add-interfacefails with a conflict error in that case, so use--change-interfacewhen moving.--add-sourcealso refuses to bind a source that is already in another zone. Remove it from the old zone with--remove-sourcefirst. Avoid overlapping source networks in different zones. Mixed overlaps are hard to reason about, and you should verify them rather than assume which zone wins.- Changing the default zone moves every interface that has no explicit zone. Do this over SSH only after checking that the new zone allows
ssh. - On systems where NetworkManager manages the interface, it can reapply its own zone setting when the connection restarts. A change made with
firewall-cmdcan then appear to revert. Set the zone on the connection instead, for examplesudo nmcli connection modify "Wired connection 1" connection.zone internal, then bring the connection up again.
Runtime and permanent configuration, and the --permanent flag, are covered in the next chapter. For now, note that --change-interface without --permanent lasts only until the next reload or reboot.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Service opened, but clients still cannot connect | Rule added to the default zone, but the interface is in another zone | Check `--get-zone-of-interface` and repeat the rule with `--zone=` |
| `ZONE_CONFLICT` when adding an interface or source | It is already bound to a different zone | Use `--change-interface`, or remove the source first |
| Blocked network can still connect | Source bound to a zone that is not `drop` or `block`, or a different, more permissive zone matches first | Check `--get-zone-of-source` |
| Zone rules appear to do nothing | The zone has no interface or source and is not the default | Bind an interface or source, or choose another zone |
| Interface zone reverts after a network restart | NetworkManager reapplies its own setting | Set `connection.zone` with `nmcli` |
Decision guide
- Use the interface to separate networks by where traffic arrives, for example a public NIC and a private NIC.
- Use a source binding to treat specific addresses differently from the rest of an interface's traffic, for example trusting an office subnet or dropping an abusive one.
- Keep the default zone conservative. Anything not explicitly placed lands there.
Exercise
A server has eth0 bound to the zone internal, and the default zone is public. You run sudo firewall-cmd --add-service=https and clients on eth0 still cannot reach port 443. Why, and what should you run instead? Also say what a client sees if a source is bound to the drop zone, compared with the block zone.
Show answer
Without --zone, the command changed the default zone, public. Traffic arriving on eth0 is matched to internal, so internal's rules apply and HTTPS is still closed there. Run sudo firewall-cmd --zone=internal --add-service=https (add --permanent, or use --runtime-to-permanent, to keep it). A client in a source bound to drop gets no reply and the connection hangs until it times out. A client in a source bound to block gets an immediate ICMP rejection.