Sub topic 8
Closing a Service or Port with firewalld
This chapter assumes that you can use sudo, understand TCP and UDP ports, and have already inspected firewalld zones. A service is a named group of ports, such as http for TCP port 80. A port rule allows one port and protocol directly, such as 8080/tcp.
Closing a rule means removing it from the zone that currently allows the traffic. You must know whether the rule is in the runtime configuration, the permanent configuration, or both.
Understand what you are removing
firewalld keeps two configurations:
- The runtime configuration is active now but disappears after a restart or reload.
- The permanent configuration is saved and restored after a reload or restart.
A rule can exist in one configuration but not the other. The following commands inspect the default zone:
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --list-all
sudo firewall-cmd --permanent --list-allThe first --list-all shows active runtime rules. The second shows saved permanent rules. If the application uses another zone, add --zone=ZONE to each command.
| Rule type | Example opening rule | Matching removal rule |
|---|---|---|
| Named service | `--add-service=http` | `--remove-service=http` |
| TCP port | `--add-port=8080/tcp` | `--remove-port=8080/tcp` |
| UDP port | `--add-port=5353/udp` | `--remove-port=5353/udp` |
Use the same service name, port number, and protocol that were used when the rule was added.
Remove a rule from the active configuration
To stop accepting HTTP immediately, remove the service from the runtime configuration:
sudo firewall-cmd --remove-service=http
sudo firewall-cmd --query-service=http
sudo firewall-cmd --list-services--query-service=http returns yes when the service is still allowed and no when it is absent from that zone. These commands affect the default zone unless you specify another zone.
To close TCP port 8080 immediately:
sudo firewall-cmd --remove-port=8080/tcp
sudo firewall-cmd --query-port=8080/tcp
sudo firewall-cmd --list-portsRemoving a service does not stop the application. The process may continue listening on port 80, but firewalld will no longer allow traffic to reach it through that zone.
Remove a rule permanently
To make the change survive a reload or reboot, remove the rule from the permanent configuration and reload firewalld:
sudo firewall-cmd --permanent --remove-port=8080/tcp
sudo firewall-cmd --permanent --remove-service=http
sudo firewall-cmd --reloadThe reload activates the modified permanent configuration. Check both the active and saved state:
sudo firewall-cmd --query-port=8080/tcp
sudo firewall-cmd --query-service=http
sudo firewall-cmd --permanent --query-port=8080/tcp
sudo firewall-cmd --permanent --query-service=httpEach command should return no after the corresponding rule has been removed. If a rule remains, check the zone explicitly:
sudo firewall-cmd --zone=public --list-all
sudo firewall-cmd --permanent --zone=public --list-allThe rule may have been added to public while you are inspecting a different default zone.
Runtime-only removal
Sometimes you need to close a port temporarily while investigating an incident. Omit --permanent:
sudo firewall-cmd --remove-port=8080/tcp
sudo firewall-cmd --reloadThe reload restores the permanent configuration, so port 8080 becomes allowed again if it remains in the permanent rules. This behavior is useful for testing, but it can surprise operators who expect a runtime change to persist.
Exercise
You opened TCP port 9000 permanently in the public zone. What commands close it and verify the result?
Show answer
Run sudo firewall-cmd --permanent --zone=public --remove-port=9000/tcp, then sudo firewall-cmd --reload, and finally sudo firewall-cmd --zone=public --query-port=9000/tcp. The final result should be no.