Sub topic 8
Closing a service or port
Opening a port is only half of managing a firewall. Services get retired, test ports get forgotten, and every rule that stays open is attack surface you have to keep track of. This chapter covers how to remove a service or port, how to confirm it is gone, and why a rule sometimes appears to survive its own removal.
It assumes you have read the earlier chapters on zones, the runtime and permanent configurations, and opening services and ports. You should be comfortable running firewall-cmd with sudo.
What "closing" means in firewalld
firewalld has no "close" command. You close a service or port by removing the rule that allowed it. The zone's default behavior then applies. In most zones that means the packet is rejected, so the client sees "connection refused" or an ICMP error rather than silence.
The removal commands mirror the add commands:
| Task | Add | Remove |
|---|---|---|
| Service | `--add-service=NAME` | `--remove-service=NAME` |
| Port | `--add-port=PORT/PROTO` | `--remove-port=PORT/PROTO` |
| Port range | `--add-port=8000-8100/tcp` | `--remove-port=8000-8100/tcp` |
| Rich rule | `--add-rich-rule='...'` | `--remove-rich-rule='...'` |
| Source in a zone | `--add-source=CIDR` | `--remove-source=CIDR` |
Each command acts on one configuration set. Without --permanent it changes the runtime configuration. With --permanent it changes the saved configuration. Without --zone=, it acts on the default zone.
Find what is currently open
Before you remove anything, look at the zone that applies to the traffic. A rule you try to remove in the wrong zone is not found there, so nothing changes.
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=public --list-allRead the services: and ports: lines. Note that a port can be open in two ways at once. If services: lists http and ports: lists 80/tcp, both rules allow port 80. Removing only one leaves the port open.
To see what a service actually opens, ask firewalld:
sudo firewall-cmd --info-service=httpRemove a service or port permanently
This is the standard way to close something for good. You edit the permanent configuration, then reload so the runtime configuration matches.
sudo firewall-cmd --permanent --remove-port=8080/tcp
sudo firewall-cmd --permanent --remove-service=http
sudo firewall-cmd --reloadThe first line removes a custom port. The second removes the predefined http service, which stops allowing TCP port 80. The reload applies the saved configuration to the running firewall.
Check that both are gone:
sudo firewall-cmd --query-port=8080/tcp
sudo firewall-cmd --query-service=httpEach --query-* command prints yes or no and sets the exit status to 0 for yes and 1 for no. This makes the queries easy to use in scripts:
if sudo firewall-cmd --query-port=8080/tcp --quiet; then
echo "8080/tcp is still open"
else
echo "8080/tcp is closed"
fiPort protocol must match. --remove-port=8080/tcp does not remove 8080/udp. If you opened both, remove both.
Remove from the runtime configuration only
Sometimes you want to close something immediately without touching the saved configuration. Leave out --permanent:
sudo firewall-cmd --remove-port=8080/tcp
sudo firewall-cmd --list-portsThe port is closed now, but it opens again after the next --reload, --complete-reload, or reboot, because the permanent configuration still lists it. This is useful in an incident, when you want to cut off a service quickly and decide later whether the closure should be saved. Once you are satisfied, you can save the runtime state:
sudo firewall-cmd --runtime-to-permanent--runtime-to-permanent overwrites the permanent configuration with whatever the runtime configuration holds, for every zone. It saves any other temporary changes too, including ones you forgot about. Run --list-all first and read it carefully.
Closing in a specific zone
If the server has more than one active zone, name the zone explicitly. A server with a public interface and an internal interface might allow a service in both zones, and you may want to close it on only one:
sudo firewall-cmd --permanent --zone=public --remove-service=cockpit
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --query-service=cockpit
sudo firewall-cmd --zone=internal --query-service=cockpitAfter this, the public zone returns no and the internal zone still returns yes. Omitting --zone= would have changed whichever zone is the default, which may not be the one that handles your traffic.
Established connections are not cut off
Removing a rule stops new connections. A connection that is already established usually continues. firewalld accepts packets that belong to an existing connection through connection tracking, so a client that connected before you closed the port can keep its session until it ends.
This matters in two situations:
- You closed a port to stop an unwanted client, but that client is still connected. Stop the service, or end the connection on the server, for example with
ss -K(which needs kernel support) or by restarting the application. - You removed the
sshservice while logged in over SSH. Your session may keep working until you disconnect, and then you cannot get back in. Do not treat a working session as proof that the change was safe.
firewall-cmd --complete-reload reloads the netfilter modules and tends to drop existing connections. Avoid it on a remote server unless you have console access.
Verify from outside
The firewall's own listing shows what firewalld believes. It does not show whether the port is reachable. After closing something, test it from another host:
nc -zv server.example.com 8080A closed port that is rejected returns "Connection refused" immediately. A port that is dropped (for example in the drop zone) makes the client wait until it times out. If the port still connects, check these in order:
- The rule is removed in the right zone:
sudo firewall-cmd --get-active-zones, then--zone=ZONE --list-all. - The same port is not opened by a second rule, such as a service plus a port, a rich rule, or a service that includes the port.
- The runtime and permanent configurations agree. Compare
--list-allwith--permanent --list-all. - A different layer allows the traffic, such as a cloud security group or another host firewall in front of the server.
You can also confirm that the service itself is not listening, which is the most reliable way to make a port unreachable:
sudo ss -tlnp | grep ':8080'If a process is still bound to the port, it will answer as soon as any rule allows it. Closing the firewall port reduces exposure but does not stop the program. For a service you no longer need, stop and disable it as well:
sudo systemctl disable --now myapp.serviceRemoving rules that are not there
Removing something that is not enabled does not break the firewall. firewalld prints a warning such as NOT_ENABLED and leaves the configuration unchanged. This is useful as a clue. If you see it, you are probably in the wrong zone, or you are editing the wrong configuration set (runtime versus permanent).
Rich rules need the exact text. The string you pass to --remove-rich-rule must describe the same rule that --list-rich-rules shows:
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-rulesRemoving a custom service definition
A custom service you created has two parts: the definition and its use in zones. Removing the service from a zone closes the ports. Deleting the definition removes the service from firewalld entirely. Do them in that order:
sudo firewall-cmd --permanent --remove-service=myapp
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --delete-service=myapp
sudo firewall-cmd --reloadYou only need --delete-service when you no longer want the definition at all. Keeping it costs nothing and lets you re-enable the service later by name.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Port closed, then open again after reload | Removed in runtime only | Repeat with `--permanent`, or run `--runtime-to-permanent` |
| `--query-port` says `no` but the port connects | Another rule or service also opens it | Check `services:`, `ports:`, and rich rules |
| `NOT_ENABLED` warning | Wrong zone or wrong configuration set | Check the active zone and use `--permanent` if needed |
| Client stays connected after removal | Connection tracking keeps established sessions | Stop the service or end the connection |
| Cannot reconnect over SSH | Removed `ssh` service in the active zone | Use console access and add it back |
Exercise
A server has services: ssh http and ports: 80/tcp 8080/tcp in the public zone. You run sudo firewall-cmd --permanent --remove-service=http and sudo firewall-cmd --reload. A remote nc -zv server.example.com 80 still succeeds. Why, and which command fixes it?
Show answer
The 80/tcp port rule is a separate entry from the http service, and it still allows TCP port 80. Remove it with sudo firewall-cmd --permanent --remove-port=80/tcp, then run sudo firewall-cmd --reload and test again.
Exercise
You run sudo firewall-cmd --remove-port=8080/tcp during an incident and the traffic stops. The next morning the port is open again after a reboot. What happened, and how should you have made it last?
Show answer
The command changed only the runtime configuration, and the permanent configuration still listed the port, so it was restored at boot. Either add --permanent to the remove command and reload, or run sudo firewall-cmd --runtime-to-permanent after checking the runtime configuration.