Sub topic 6
Opening a Service by Name
Assumptions: you know basic Linux commands, sudo, TCP and UDP ports, systemctl, and IPv4 CIDR notation. You are using a Linux server with firewalld installed and a predefined service such as HTTP or SSH.
What a service name means
A network service is a named group of firewall settings. For example, the predefined http service represents the TCP port normally used by web servers, port 80. The https service represents TCP port 443.
Using a service name is clearer than entering a port number when the application uses a standard protocol. firewall-cmd --add-service=http explains the purpose of the rule. firewall-cmd --add-port=80/tcp expresses the same port-level change but does not identify the protocol's purpose as clearly.
The service definition does not start an application. It only tells the firewall to allow matching traffic. A web server must still be installed, running, and listening on the allowed port.
How the zone affects the change
Firewalld assigns network traffic to a zone. A zone is a rule set with its own allowed services, ports, and sources. A network interface or source address determines which zone handles traffic.
If you omit --zone, firewall-cmd changes the default zone. This is convenient, but first confirm that the default zone is the one handling the server's interface.
Check the active zones and their rules before changing anything:
sudo systemctl enable --now firewalld
sudo firewall-cmd --state
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-allThe last command lists the rules in the default zone. To inspect a particular zone, provide its name:
sudo firewall-cmd --zone=public --list-allOpen a predefined service safely
Firewalld keeps a runtime configuration in memory and a permanent configuration on disk. A runtime change takes effect immediately but disappears after a reload or reboot unless you save it. A permanent change survives reloads but does not affect current traffic until you reload firewalld.
For a cautious change, open HTTP only in the runtime configuration first. The timeout automatically removes the rule after five minutes:
sudo firewall-cmd --add-service=http --timeout=5m
sudo firewall-cmd --list-servicesTest the web service during this window. If the result is correct, save the current runtime configuration as permanent:
sudo firewall-cmd --runtime-to-permanentYou can also make a lasting change directly:
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --query-service=httpsThe --query-service command returns yes when the service is enabled in the selected configuration. Add --zone=ZONE when the rule belongs to a zone other than the default zone.
| Command form | Configuration changed | Takes effect |
|---|---|---|
| `--add-service=http` | Runtime | Immediately |
| `--permanent --add-service=http` | Permanent | After reload |
| `--runtime-to-permanent` | Copies runtime to permanent | Already active |
Confirm the service definition
Predefined service names vary by distribution and firewalld package. List available names, or inspect one service:
sudo firewall-cmd --get-services
sudo firewall-cmd --info-service=httpThe information command shows the ports and protocols included in the service. If the application uses a non-standard port, use a specific port rule or define a custom service instead of pretending that the standard service name describes it.
Exercise
You need to expose a web server using HTTPS in the public zone and keep the change after a reboot. Which commands should you run?
Show answer
Run sudo firewall-cmd --permanent --zone=public --add-service=https, then sudo firewall-cmd --reload, and verify with sudo firewall-cmd --zone=public --query-service=https.