Sub topic 9
Blocking a source address or network with zones
This chapter assumes you can use sudo and systemctl, and that you read IPv4 addresses and CIDR notation such as /24. It also assumes you know the difference between runtime and permanent configuration in firewalld, covered in an earlier chapter. Here you will block traffic from a specific address or network by assigning it to a zone. The next chapter covers rich rules, which solve a related problem at finer granularity.
Why zones can block sources
Most firewalld examples attach a zone to a network interface. A zone can also be attached to a source, which is an IP address or a network range. When a packet arrives, firewalld picks the zone to apply in this order:
- If the packet's source address matches a source assigned to a zone, that zone is used.
- Otherwise, if the packet arrived on an interface assigned to a zone, that zone is used.
- Otherwise, the default zone is used.
The source match comes first. You can therefore leave your interface in the public zone and still send one hostile network to a different zone that discards everything.
Choosing a zone that blocks
firewalld ships with several zones that refuse traffic. They differ in how they refuse it.
| Zone | Behavior for incoming traffic | What the sender sees |
|---|---|---|
| `drop` | Discards every incoming packet with no reply | A timeout |
| `block` | Rejects every incoming packet | An ICMP "host prohibited" error |
| `public` | Accepts only the services and ports you listed, rejects the rest | Depends on the rule |
For hostile or unwanted sources, drop is usually the better choice. The sender gets no confirmation that a host exists, and scanners have to wait for timeouts. block is more polite and makes your own debugging easier, because a refusal arrives immediately instead of hanging.
Neither zone allows any services. Adding http to the drop zone has no practical use, because the point of the zone is to refuse everything. Outgoing connections that your server starts are still allowed. Replies to them are accepted as part of the established connection.
Assigning a source to the drop zone
This command sends all traffic from 203.0.113.0/24 to the drop zone. That range is reserved for documentation, so it is safe to practice with. Replace it with the real network you want to block.
sudo firewall-cmd --permanent --zone=drop --add-source=203.0.113.0/24
sudo firewall-cmd --reload
sudo firewall-cmd --get-active-zonesThe --permanent option writes the change to the saved configuration only, so --reload is needed to load it into the running firewall. Afterward, --get-active-zones should list drop with a sources: line. A zone appears as active only when it has an interface or a source assigned, so drop shows up for the first time at this point.
You can use a single address, a CIDR range, or an IPv6 prefix:
sudo firewall-cmd --permanent --zone=drop --add-source=198.51.100.7
sudo firewall-cmd --permanent --zone=drop --add-source=2001:db8:bad::/48
sudo firewall-cmd --reloadA bare address is treated as a single host. Note that firewalld keeps IPv4 and IPv6 sources in the same list, so one zone can hold both kinds.
Inspecting source assignments
Check what a zone holds and which zone owns an address:
sudo firewall-cmd --zone=drop --list-sources
sudo firewall-cmd --zone=drop --query-source=203.0.113.0/24
sudo firewall-cmd --get-zone-of-source=203.0.113.0/24--list-sources prints every source in the zone. --query-source prints yes or no and sets the exit status, which makes it usable in scripts. --get-zone-of-source tells you which zone claims an address or range, which is the quickest way to answer "why is this address behaving this way?"
Without --permanent, these commands read the runtime configuration. Add --permanent to read the saved configuration instead. If the two differ, you have either an unsaved runtime change or a saved change that has not been reloaded.
Rules and gotchas
A source belongs to only one zone
A given source can be assigned to only one zone at a time. If 203.0.113.0/24 is already in the trusted zone and you try to add it to drop, firewalld reports a zone conflict. Remove it from the old zone first, or move it in one step:
sudo firewall-cmd --permanent --zone=drop --change-source=203.0.113.0/24
sudo firewall-cmd --reload--change-source moves the source into the named zone, even if it currently lives in another one.
Overlapping networks
Different sources can overlap. For example, 203.0.113.0/24 can be in drop while 203.0.113.50 is in trusted. Packets from the single address use the more specific match, as long as both entries are actual sources in zones. Do not rely on this without testing it. A simpler design avoids overlap: keep one list of blocked networks and one list of trusted networks, and check them with --get-zone-of-source.
The zone replaces the interface zone for that source
When a source matches, the interface zone does not apply to that traffic at all. This also works in the opposite direction. You can place an office network in the trusted or internal zone, so it reaches services that public does not expose. This is the same mechanism used for blocking, applied for allowing.
Existing connections
Established connections are usually accepted by a rule that runs before zone matching. If a blocked host already has a session open, such as an SSH login, the session can keep working after you add the source. New connections are refused. To cut off an existing session, terminate it on the server, for example with ss -K or by killing the process, or restart the service that holds the connection. Check this on your own system before you assume that the block has taken effect.
Do not block yourself
Adding your own address, or a range that contains it, to the drop zone cuts off your SSH session and every new connection from you. Before you add a broad range such as a /16, confirm that your client address is outside it. You can find your address on the server with who or with echo $SSH_CLIENT.
To test a block without committing to it, run the command without --permanent. The change lives only in the running firewall, so sudo firewall-cmd --reload or a reboot removes it. Save it with sudo firewall-cmd --runtime-to-permanent once it behaves as you expect.
Removing a block
Take the source out of the zone, then reload:
sudo firewall-cmd --permanent --zone=drop --remove-source=203.0.113.0/24
sudo firewall-cmd --reload
sudo firewall-cmd --get-active-zonesIf drop has no interfaces and no sources left, it disappears from the active zone list.
Many addresses: use an ipset
Each source you add is its own entry, and firewalld turns each into a separate rule. For dozens or hundreds of addresses, an ipset is a better fit. It is a named set of addresses that firewalld handles as one match. You create the set, add entries, and attach it to a zone as ipset:name.
sudo firewall-cmd --permanent --new-ipset=blocklist --type=hash:net
sudo firewall-cmd --permanent --ipset=blocklist --add-entry=203.0.113.0/24
sudo firewall-cmd --permanent --ipset=blocklist --add-entry=198.51.100.7
sudo firewall-cmd --permanent --zone=drop --add-source=ipset:blocklist
sudo firewall-cmd --reloadLater you add or remove entries with --add-entry and --remove-entry without touching the zone.
Zones or rich rules?
| Need | Better tool |
|---|---|
| Refuse everything from a network | Source in the `drop` or `block` zone |
| Refuse only one port from one address | Rich rule |
| Allow a trusted network more services than the public | Source in `trusted` or `internal` |
| Maintain a long list of addresses | ipset attached to a zone |
A zone source is all-or-nothing for that address. If you only want to stop one address reaching SSH while it can still reach your website, use a rich rule, covered in the next chapter.
Exercise
Your server's interface is in the public zone, and you want to discard all traffic from 198.51.100.0/24. Write the commands that do this permanently and confirm it is active. Then explain why the public zone's open services do not let that network through.
Show answer
Run sudo firewall-cmd --permanent --zone=drop --add-source=198.51.100.0/24, then sudo firewall-cmd --reload, then sudo firewall-cmd --get-active-zones (or sudo firewall-cmd --zone=drop --list-sources) to confirm that drop lists the network. Zone selection checks the packet's source address first, so packets from that network match the drop zone before the interface zone is considered, and drop discards them without a reply.
Exercise
You run sudo firewall-cmd --permanent --zone=drop --add-source=192.0.2.0/24, and firewalld reports a zone conflict. Which command shows the cause, and which command fixes it in a single step?
Show answer
sudo firewall-cmd --get-zone-of-source=192.0.2.0/24 shows which zone already holds the source. sudo firewall-cmd --permanent --zone=drop --change-source=192.0.2.0/24 moves it into drop, and sudo firewall-cmd --reload applies the change. A source can belong to only one zone.
Exercise
After you drop a network, a host in it still has an SSH session open and keeps typing commands. Why can this happen, and how do you stop it?
Show answer
Established connections are accepted by a rule that is evaluated before zone matching, so the block applies to new connections but may not end a session that already exists. End the session on the server, for example by terminating the process or using ss -K, and the host cannot reconnect because new connections are dropped.