Sub topic 7
Opening a Port and Protocol
This chapter assumes that you can use the Linux command line, run commands with sudo, identify TCP and UDP ports, manage services with systemctl, and read IPv4 addresses in CIDR notation. It focuses on opening a specific port with firewall-cmd. Earlier chapters cover how zones are selected and how to inspect them.
Port Rules Include a Protocol
A firewall rule must identify both a port number and a transport protocol. A protocol defines how network messages are exchanged.
- TCP provides a connection-oriented session. HTTP, HTTPS, and SSH normally use TCP.
- UDP sends independent datagrams without establishing a connection first. DNS and many streaming or discovery protocols use UDP.
- The same port number can have separate TCP and UDP rules.
For example, 8080/tcp allows TCP traffic to port 8080. It does not allow UDP traffic to port 8080.
| Rule | Traffic allowed |
|---|---|
| `8080/tcp` | TCP packets addressed to port 8080 |
| `8080/udp` | UDP packets addressed to port 8080 |
| `8080` | Invalid or incomplete; specify the protocol |
Opening a port only changes the firewall. An application must also be listening on that port, and other network controls must permit the traffic.
Choose the Correct Zone
A zone is a named set of firewall rules. An interface or source address is assigned to a zone, so the correct zone depends on how the incoming traffic matches firewalld.
Check the active zones before changing anything:
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --zone=public --list-allReplace public with the zone that contains the server's active interface or source. If you omit --zone, firewalld uses the default zone. That may not be the zone handling the traffic.
Open a Port
The following commands add TCP port 8080 to the permanent configuration of the public zone:
sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --query-port=8080/tcp
sudo firewall-cmd --zone=public --list-portsThe first command writes the rule to the permanent configuration. The reload replaces the active runtime rules with the saved configuration. The query returns yes when the rule exists, and list-ports displays the explicitly opened ports.
To allow UDP instead, use:
sudo firewall-cmd --permanent --zone=public --add-port=8080/udp
sudo firewall-cmd --reloadTo allow both protocols, add both rules. Do not add UDP unless the application needs it.
Runtime and Permanent Changes
You can test a rule without saving it permanently. A runtime-only rule takes effect immediately but disappears when firewalld restarts or the system reboots.
sudo firewall-cmd --zone=public --add-port=8080/tcp
sudo firewall-cmd --zone=public --query-port=8080/tcpAfter confirming the change, save the current runtime configuration:
sudo firewall-cmd --runtime-to-permanentFor a temporary safety test, add a timeout. Firewalld removes this rule automatically after the timeout expires:
sudo firewall-cmd --zone=public --add-port=8080/tcp --timeout=5mA timeout applies only to the runtime configuration. It is useful when you are working over SSH and want an unconfirmed change to disappear automatically.
Check the Listener
Run this on the server:
sudo ss -tlnp | grep ':8080'The -t option selects TCP, -l shows listening sockets, -n keeps port numbers numeric, and -p shows the owning process. If this command returns nothing, opening the firewall port cannot create a service.
Test reachability from another host:
nc -zv server.example.com 8080Use the server's real hostname or address. A successful connection shows that the remote host can reach the port. It does not prove that the application protocol is working correctly.
Exercise
You need an application using UDP port 5353. Which firewalld command opens that port permanently in the public zone, and how do you activate the change?
Show answer
Run sudo firewall-cmd --permanent --zone=public --add-port=5353/udp, followed by sudo firewall-cmd --reload. The rule allows UDP only; it does not open TCP port 5353.