Sub topic 10
Rich Rules: Reject, Drop, and Port-Specific Source Rules
This chapter assumes that you can use the Linux command line, run commands with sudo, read IPv4 addresses and CIDR notation, and understand TCP ports. You should also know how to manage services with systemctl and understand that firewalld rules belong to zones.
A rich rule is a firewalld rule with conditions and an action. It can match a source address, destination port, protocol, or address family. This gives you more control than simply adding a service or port to a zone.
Reject and drop
A reject action refuses matching traffic and normally sends an error response to the client. A drop action discards matching traffic without responding. Use reject when a clear failure response helps legitimate clients. Use drop when you do not want to reveal that the server is reachable.
| Action | Client receives a response | Typical use |
|---|---|---|
| `reject` | Yes | Refuse a known unwanted connection clearly |
| `drop` | No | Silently discard traffic from an unwanted source |
A rich rule is evaluated in a zone. If you do not specify --zone, firewalld uses the default zone. Confirm the correct zone before adding the rule:
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zonesReject SSH from one address
The following rule rejects SSH connections from 203.0.113.10 while leaving other SSH sources unaffected:
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 family attribute limits the rule to IPv4. The source address attribute identifies the client. The port element matches TCP port 22. The final word, reject, is the action.
You can match a whole network by using CIDR notation:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port port="22" protocol="tcp" reject'
sudo firewall-cmd --reloadThis rule rejects SSH from every address in that /24 network.
Drop traffic from a source
To silently discard all IPv4 traffic from one address, omit the port condition and use drop:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.25" drop'
sudo firewall-cmd --reload
sudo firewall-cmd --list-rich-rulesThis affects all traffic from that address in the selected zone, not only SSH. A source-based zone assignment can also block a network, but a rich rule is useful when you need a specific source exception inside an existing zone policy.
Port-specific source rules
You can combine a source network with a non-standard port. This example rejects TCP port 8080 from a test network but does not affect other ports:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.0/24" port port="8080" protocol="tcp" reject'
sudo firewall-cmd --reloadIf several rich rules overlap, their order matters. Use priorities when you need one rule to be evaluated before another. A lower priority number is evaluated first:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" priority="-10" source address="198.51.100.25" port port="8080" protocol="tcp" accept'
sudo firewall-cmd --reloadReview the complete zone configuration when diagnosing an unexpected result:
sudo firewall-cmd --zone=public --list-all
sudo firewall-cmd --zone=public --list-rich-rulesReplace public with the zone that owns the relevant interface or source.
Remove a rule by repeating the exact rule text and replacing --add-rich-rule with --remove-rich-rule:
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 --reloadExercise
Which action should you choose when the client should receive an explicit refusal, and which action should you choose when the packet should be silently discarded?
Show answer
Use reject for an explicit refusal. Use drop to discard the packet without sending a response.