Sub topic 7
Opening a port and protocol
This chapter assumes you can use the Linux command line with sudo, and that you know what a TCP or UDP port and a listening service are. It also assumes you have read the earlier chapters on zones and on runtime versus permanent configuration. You will use those ideas here.
When to open a port instead of a service
The previous chapter opened a service by name, such as http. A firewalld service is a named bundle of ports and protocols defined in an XML file. Use a service when one exists for what you run. Use a port rule when your application listens on a number that no predefined service covers, for example a development web server on 8080 or a metrics endpoint on 9100.
A port rule has two parts, the port and the protocol, written as port/protocol. Both are required.
8080/tcpallows TCP connections to port 8080.8080/udpallows UDP datagrams to port 8080. This is a separate rule.8000-8010/tcpallows a range of 11 TCP ports.
TCP and UDP are independent. Opening 53/tcp does not open 53/udp. DNS servers usually need both.
Opening a port permanently
The usual workflow is to write the rule to the permanent configuration, reload, and then check the result.
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --query-port=8080/tcp
sudo firewall-cmd --list-portsEach command does the following:
--permanent --add-port=8080/tcpwrites the rule into the saved configuration of the default zone. The running firewall does not change yet.--reloadreads the saved configuration into the running firewall. After this the rule is active.--query-port=8080/tcpprintsyesorno, and its exit status is 0 for yes and 1 for no. This makes it usable in scripts.--list-portsshows every port opened in the zone, for example8080/tcp.
A successful add prints success. If the rule already exists, firewalld prints Warning: ALREADY_ENABLED: 8080:tcp and still exits normally, so repeating a command is safe.
Note that firewalld prints port ranges in its own style in warnings, with a colon between the port and the protocol. In --list-ports the form is 8080/tcp.
Choosing the zone
Without --zone, firewalld applies the change to the default zone. On many servers that is public. The zone that matters is the one that traffic actually arrives in, and that may not be the default. Check first:
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zonesIf your network interface is assigned to another zone, name it explicitly:
sudo firewall-cmd --permanent --zone=internal --add-port=9100/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --zone=internal --list-portsOpening a port in a zone that no interface or source belongs to has no visible effect. This is a common reason for a rule that looks correct and does not work. The check is the output of --get-active-zones.
Trying a port first in the runtime configuration
You can also add a port to the runtime configuration only. The change takes effect at once and disappears on the next reload or reboot. Adding --timeout makes it disappear on its own after a set time:
sudo firewall-cmd --add-port=8080/tcp --timeout=10m
sudo firewall-cmd --list-portsThe timeout accepts seconds by default, or a suffix of s, m, or h. Once the port works as intended, save it, or do nothing and let it expire:
sudo firewall-cmd --runtime-to-permanentBe careful with --runtime-to-permanent. It saves the whole runtime state, not only your new port. If someone else added a temporary rule earlier, that rule becomes permanent too. Run --list-all and read it before you save.
The timeout cannot be combined with --permanent. firewalld rejects that combination, because a permanent rule has no concept of expiring.
How the two configurations interact
The diagram shows the two choices and how each reaches the running firewall.
One consequence matters in practice. --reload replaces the running configuration with the saved one. Any runtime-only rule you added is dropped. If you tested a port at runtime and forgot to save it, a later reload silently closes it.
Port or service: a comparison
| Aspect | Service rule | Port rule |
|---|---|---|
| Example | `--add-service=http` | `--add-port=8080/tcp` |
| What it opens | All ports and protocols in the service definition | Exactly the port and protocol you give |
| Readability in `--list-all` | Name shows intent | Number only |
| Best for | Standard software | One-off or non-standard ports |
| Changing the port later | Edit the service once | Remove and add rules in each zone |
If you find yourself opening the same port in several zones, or documenting what a number means, a custom service is the better tool. A later chapter covers it.
Ranges and multiple rules
A range is one rule:
sudo firewall-cmd --permanent --add-port=5000-5010/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --list-portsKeep ranges as narrow as you can. Every port in the range accepts connections as soon as something listens on it. Also check for overlap. If you open 8080/tcp and then 8000-8100/tcp, both rules stay listed, and removing one does not close the ports the other covers.
To add several ports in one command, repeat the option:
sudo firewall-cmd --permanent --add-port=8080/tcp --add-port=8443/tcp
sudo firewall-cmd --reloadVerifying the result
A firewall rule is only one of the things that must be true for a connection to work. Check them in order, starting on the server and then from outside.
sudo ss -tlnp | grep ':8080'
nc -zv server.example.com 8080The first command, run on the server, shows whether a process is listening on port 8080. The flags are -t for TCP, -l for listening sockets, -n for numeric ports, and -p for the owning process. The second command is run from another host. nc -zv tests the connection without sending data, and it prints either succeeded or an error.
The error tells you where to look.
| Result from the remote host | Likely meaning |
|---|---|
| `Connection refused` immediately | The packet reached the server and nothing is listening. Or the zone uses `reject`. |
| Hangs, then `timed out` | A firewall, here or on the network path, is dropping packets. |
| `succeeded` | The firewall allows the port and a service is listening. |
Two points avoid wrong conclusions. First, the default behavior of a zone for unlisted traffic is to reject with an ICMP message, so a blocked port often gives Connection refused, the same as a closed one. The ss check separates the cases. Second, nc from the server to itself uses the loopback interface, which firewalld normally does not filter. A local success proves nothing about the firewall, so always test from another host.
Common errors
- The rule is only in one configuration. You used
--permanentand skipped--reload, so nothing changed yet. Or you skipped--permanent, and the port vanished after a reload. Comparesudo firewall-cmd --list-portswithsudo firewall-cmd --permanent --list-ports. - Wrong zone. The port is open in
public, but the interface is ininternal. Check--get-active-zones. - Wrong protocol. The application uses UDP and you opened TCP. Check with
ss -ulnpfor UDP listeners. - Nothing is listening. The firewall allows the traffic and the service is stopped or bound to
127.0.0.1only. Thessoutput shows the bound address. - Invalid syntax. A missing protocol such as
--add-port=8080returnsINVALID_PORT. The protocol is required. - Another layer blocks it. A cloud security group, a network firewall, or SELinux can stop the traffic after firewalld allows it. SELinux, for example, restricts which ports a service may bind to, and that is configured separately from firewalld.
Keeping SSH safe while you work
Adding a port does not affect SSH. Mistakes happen when you reload after editing the configuration in several ways, or when you remove rules. Before changing a remote server, confirm that ssh is listed in the zone you connect through, and keep one SSH session open while you test from a second one. For risky edits, use a runtime rule with --timeout so the change reverts without your help.
Exercise
You run sudo firewall-cmd --permanent --add-port=8080/tcp on a server and the output is success. A remote nc -zv server.example.com 8080 still times out, and ss -tlnp | grep ':8080' shows the application listening on 0.0.0.0:8080. What command is missing, and how do you confirm the fix?
Show answer
The permanent rule has not been loaded into the running firewall. Run sudo firewall-cmd --reload, then confirm with sudo firewall-cmd --query-port=8080/tcp, which should print yes, and repeat nc -zv server.example.com 8080 from the remote host. If it still times out, check --get-active-zones to make sure the rule went into the zone that holds the interface, and check for a network or cloud firewall in the path.
Exercise
A DNS server must answer queries on port 53 over both TCP and UDP, only in the internal zone. Write the commands to allow this permanently and then verify it.
Show answer
Run sudo firewall-cmd --permanent --zone=internal --add-port=53/tcp --add-port=53/udp, then sudo firewall-cmd --reload. Verify with sudo firewall-cmd --zone=internal --list-ports, which should show 53/tcp 53/udp, and with sudo firewall-cmd --zone=internal --query-port=53/udp, which should print yes.
Exercise
You test a port with sudo firewall-cmd --add-port=9000/tcp --timeout=10m, and it works. Ten minutes later it stops working. Why, and what should you have done after confirming it worked?
Show answer
A rule added with --timeout lives only in the runtime configuration and is removed when the timeout ends. After confirming it worked, you should have run sudo firewall-cmd --runtime-to-permanent after checking --list-all. Or you should have added it again with --permanent and run --reload.