Sub topic 3
Blocking a Source Address or Network with Zones
This chapter assumes you can use the Linux command line with sudo, understand TCP and UDP ports, manage services with systemctl, and read IPv4 addresses in CIDR notation, such as 203.0.113.0/24.
Blocking a source with the drop zone
A source is the IP address or network that a packet comes from. firewalld can assign a source to a zone. The drop zone discards all incoming traffic from that source without sending a reply.
sudo firewall-cmd --permanent --zone=drop --add-source=203.0.113.0/24
sudo firewall-cmd --reload
sudo firewall-cmd --get-active-zonesThe last command should list drop with the source 203.0.113.0/24. A source assignment takes priority over the zone bound to the interface.
Rich rules: reject, drop, and port-specific source rules
Rich rules match on a source, a port, or both, and then apply an action. reject sends an error reply. drop discards the packet silently. This rule rejects SSH from one address only:
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-rulesTo drop all traffic from one host, use rule family="ipv4" source address="203.0.113.10" drop.
> Rejecting SSH from your own address can lock you out. Confirm your source address before you add a rule that matches port 22.
Defining a custom service for your application
A custom service groups an application's port under one name. This example uses port 9000:
sudo firewall-cmd --permanent --new-service=myapp
sudo firewall-cmd --permanent --service=myapp --set-description="Internal web application"
sudo firewall-cmd --permanent --service=myapp --add-port=9000/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --add-service=myapp
sudo firewall-cmd --reloadVerifying from the server and from outside
On the server, confirm that a process listens on the port:
sudo ss -tlnp | grep ':9000'From another host, test whether the firewall allows the connection:
nc -zv server.example.com 9000When the port is still unreachable
Check in this order: whether the application listens, whether the firewall allows the port, and whether a source rule blocks the client. To see denied packets temporarily, enable logging and turn it off afterward:
sudo firewall-cmd --set-log-denied=all
sudo journalctl -k --since "10 minutes ago" | grep -i reject
sudo firewall-cmd --set-log-denied=off| Symptom | Likely cause |
|---|---|
| Rule disappears after reload | Command ran without `--permanent` |
| Client blocked, port open | Source listed in the drop zone or a rich rule |
| `ss` shows nothing on the port | Application is not running or uses another port |
Exercise
Which command silently discards all traffic from 198.51.100.7 in a permanent configuration, and which command applies it?
Show answer
sudo firewall-cmd --permanent --zone=drop --add-source=198.51.100.7, followed by sudo firewall-cmd --reload.