Sub topic 10
Rich rules: reject, drop, and port-specific source rules
This chapter assumes you can run firewall-cmd with sudo. It also assumes you know the difference between runtime and permanent configuration, and that you can read IPv4 addresses in CIDR notation such as 203.0.113.0/24. You should know how zones match traffic by interface and by source.
Zones and services work on whole categories of traffic. A service is either open in a zone or it is not. They cannot express "refuse SSH from this one host" or "allow PostgreSQL only from the database subnet". Rich rules can, and this chapter covers them.
What a rich rule is
A rich rule is a single rule written in firewalld's rich language and attached to a zone. It can combine a source or destination address, an element such as a service or port, an optional log statement, and an action. The general shape is:
rule [family="ipv4|ipv6"] [source address="ADDR[/MASK]"] [element] [log ...] [action]The parts you will use most often are:
- family:
ipv4oripv6. It is required whenever the rule contains a source or destination address. - source address: a single address or a network in CIDR notation.
- element:
service name="..."orport port="..." protocol="...". - action:
accept,reject, ordrop.
Rich rules are stored in the zone's configuration. Like every other change, they exist only in the runtime configuration unless you add --permanent or run --runtime-to-permanent.
Reject versus drop
Both actions stop a packet from reaching your service. They differ in what the sender sees.
| Action | What the sender sees | Typical use |
|---|---|---|
| `reject` | An immediate error reply, so the client reports "connection refused" or "host unreachable" | Internal networks, where fast and clear failures help people debug |
| `drop` | Nothing. The client waits until it times out | Untrusted or hostile sources, where you do not want to confirm that the host exists |
A rejected connection fails in milliseconds. A dropped one can hang for tens of seconds, which makes it slower to diagnose. Choose reject for people you want to give a clear message, and drop for traffic you want to ignore.
You can also pick the kind of error reply with reject type="icmp-host-prohibited". Without type, firewalld uses its default reject type for the address family.
Rejecting one address from one port
The most common pattern is a rule that matches a source address and a specific port. This command rejects SSH connections from one host and leaves SSH open to everyone else:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="22" protocol="tcp" reject'
sudo firewall-cmd --reload
sudo firewall-cmd --list-rich-rulesThe command has these parts:
- The whole rule is in single quotes, and the values inside it are in double quotes. The shell passes the rule to
firewall-cmdas one argument. - The rule has no
--zone=option, so it goes into the default zone. Add--zone=public(or another zone name) to target a zone explicitly. - The
sshservice stays enabled in the zone. The rich rule is more specific than the service, so it applies to that one address and to nothing else.
--list-rich-rules prints the rule exactly as stored. Keep that text. You need it to remove the rule later.
Dropping a whole network
To discard everything from a network, omit the port element. The rule then matches all protocols and ports:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" drop'
sudo firewall-cmd --reload
sudo firewall-cmd --list-rich-rulesThe previous chapter did the same job by assigning the network to the drop zone. The two approaches have different strengths:
| Approach | Strength | Weakness |
|---|---|---|
| `drop` zone with `--add-source` | One command, easy to list with `--get-active-zones` | Blocks everything from that source, with no port selection |
| Rich rule with `drop` | Can limit the block to one port or protocol, and can add logging | Longer syntax, and the whole rule must be repeated to remove it |
Use the zone for a blanket block. Use a rich rule when the block should apply to one port only.
Allowing a port only from a trusted source
Rich rules can also work in the other direction. Suppose a database listens on TCP 5432 and only the application subnet should reach it. Do not open the port to the whole zone. Add an accept rule scoped to the subnet:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.0.2.0/24" port port="5432" protocol="tcp" accept'
sudo firewall-cmd --reload
sudo firewall-cmd --query-rich-rule='rule family="ipv4" source address="192.0.2.0/24" port port="5432" protocol="tcp" accept'--query-rich-rule prints yes or no and sets the exit status to match. Hosts outside 192.0.2.0/24 do not match this rule. If the zone has no other rule allowing 5432, those hosts are refused by the zone's default behaviour.
Do not also run --add-port=5432/tcp in the same zone. A plain port rule opens 5432 to everyone and makes the restriction pointless.
Logging what a rule catches
A rich rule can log matching packets before it takes its action. Add a log statement before the action:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="22" protocol="tcp" log prefix="ssh-reject " level="info" limit value="3/m" reject'
sudo firewall-cmd --reloadprefixis text added to each log line. It makes the entries easy to find withgrep.levelis the syslog level.limit value="3/m"caps logging at three entries per minute, so an attacker cannot fill your disk with log lines.
The entries go to the kernel log. Read them with sudo journalctl -k | grep ssh-reject. This rule is a different rule from the one in the earlier example, because the text differs. If you added both, you would have two rich rules for the same address.
Where rich rules sit in the decision
A packet is first assigned to a zone, then checked against that zone's rules. Two points explain most surprises.
- Source matches come before interface matches. If the source address belongs to a zone through
--add-source, that zone handles the packet, even when the packet arrives on an interface assigned to another zone. A rich rule in the interface's zone then never sees it. - A rich rule belongs to one zone. It affects only traffic that firewalld has assigned to that zone. A reject rule in
publicdoes nothing to a source that you have placed intrusted.
The diagram shows the decision path for a packet from the blocked host.
Newer firewalld versions let you add a priority to a rich rule when you need to control the order of rules in a zone. Check the version with firewall-cmd --version and read man firewalld.richlanguage before relying on it.
Removing and testing rich rules
To remove a rich rule, repeat its exact text with --remove-rich-rule. The shortcut is to copy the line from --list-rich-rules:
sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="22" protocol="tcp" reject'
sudo firewall-cmd --reload
sudo firewall-cmd --list-rich-rulesIf the text differs by even one attribute, firewalld reports that the rule is not enabled and removes nothing.
Test a risky rule in the runtime configuration first, with a timeout. The rule disappears on its own if you lock yourself out:
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="22" protocol="tcp" reject' --timeout=5m
sudo firewall-cmd --list-rich-rulesOnce the rule behaves correctly, add it permanently with the same text and --permanent, or run sudo firewall-cmd --runtime-to-permanent. Note that a timed rule is runtime only. Saving the runtime state while the timeout is still counting down stores the rule without the timeout.
Common errors
Error: INVALID_RULEor a parse error. Usually a missing family, unbalanced quotes, or an attribute name misspelled. Check that every value is in double quotes and the whole rule is in single quotes.- The rule is listed but traffic still passes. Check that the packet is handled by the zone holding the rule. Run
--get-active-zonesand look for a source assignment that sends the traffic to another zone. - The rule disappears after a reload or reboot. It was added without
--permanent. Add it again permanently, or save the runtime state. - Removal fails. The text does not match the stored rule. Copy it from
--list-rich-rules. - IPv6 clients still connect. A rule with
family="ipv4"does not match IPv6 traffic. Add a matching rule withfamily="ipv6"and the client's IPv6 address.
Exercise
A server's default zone allows the ssh service. You want hosts on 198.51.100.0/24 to be unable to reach SSH, and you want them to see an immediate "connection refused" instead of a timeout. You also want to test the change first without risking a lockout. Write the commands, and say what you do after the test.
Show answer
Add the rule to the runtime configuration with a timeout: sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="198.51.100.0/24" port port="22" protocol="tcp" reject' --timeout=5m. The action is reject because it gives the client an immediate refusal, whereas drop would make it wait for a timeout. Confirm it with sudo firewall-cmd --list-rich-rules and a connection test from one of those hosts. Your own address must not be inside that network. If the rule works, save it with sudo firewall-cmd --runtime-to-permanent, or add it again with --permanent and run --reload. If it does not work, do nothing and the rule expires after five minutes.
Exercise
You run sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="22" protocol="tcp" drop' and firewalld says the rule is not enabled, yet --list-rich-rules shows a rule for that address and port. What is the most likely cause, and how do you fix it?
Show answer
The stored rule uses a different action or has extra attributes, for example reject instead of drop, or a log statement. A rich rule is removed only when its text matches exactly. Run sudo firewall-cmd --list-rich-rules, copy the line as printed, and pass it to --remove-rich-rule. Then run --reload and list the rules again to confirm.