Sub topic 6
Opening a service by name
This chapter assumes you can use the Linux command line with sudo, manage services with systemctl, and know what a listening TCP port is. It also assumes you have read the earlier chapters on zones, inspecting state, and runtime versus permanent configuration. Here you will use that background to open a predefined service in the right zone, test it safely, and make it persistent.
What a firewalld service is
A firewalld service is a named bundle of network settings. The simplest bundle is one port and one protocol. The http service, for example, means TCP port 80. When you allow a service in a zone, firewalld allows every port, protocol, and helper module the bundle lists.
Using a service name instead of a raw port has three benefits:
- The command says what you intend (
https), not just a number (443/tcp). - Services that need several ports are opened together. Some also load connection-tracking helper modules.
- If the definition changes in a package update, your rules follow it.
A service is only a firewall rule. Allowing http does not install or start a web server. If nothing listens on port 80, clients get a connection refused error even when the firewall is open.
Service definitions are XML files. Those shipped with firewalld live in /usr/lib/firewalld/services/. Your own definitions go in /etc/firewalld/services/, and a file there overrides a shipped file of the same name. You normally never edit these files directly, because firewall-cmd reads them for you.
Finding the right service name
List every service name your firewalld knows about:
sudo firewall-cmd --get-servicesThe output is one long line of space-separated names. Filter it when you know part of the name:
sudo firewall-cmd --get-services | tr ' ' '\n' | grep -E '^(http|https|ssh|smtp|postgresql|mysql)'Before opening a service, check what it actually opens:
sudo firewall-cmd --info-service=httpsTypical output looks like this:
https
ports: 443/tcp
protocols:
source-ports:
modules:
destination:
includes:
helpers: The ports line is the one that matters most. Some services open more than you might expect. --info-service=samba lists several ports, and --info-service=dns lists both TCP and UDP port 53. Reading the output takes a few seconds and avoids opening ports you did not mean to.
Choosing the zone
--add-service applies to one zone. If you omit --zone, firewalld uses the default zone. That is correct only when the interface that receives the traffic belongs to the default zone.
Check which zone applies to your network interface:
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zonesIf the output shows eth0 in public and eth1 in internal, then --add-service=http opens HTTP on public only. Clients on the internal network still cannot connect. Name the zone explicitly whenever more than one zone is active:
sudo firewall-cmd --zone=internal --add-service=httpExplicit zones also make your commands unambiguous if someone later changes the default zone.
The safe workflow: runtime first, then permanent
A change made without --permanent goes into the runtime configuration. It takes effect immediately and is lost on a reload or reboot. This gives you a rollback path: if something goes wrong, sudo firewall-cmd --reload discards it.
Add a --timeout for extra safety. The service is then removed automatically when the timer expires:
sudo firewall-cmd --add-service=http --timeout=5m
sudo firewall-cmd --list-services
sudo firewall-cmd --runtime-to-permanentThe steps are:
--add-service=http --timeout=5mopens HTTP in the default zone's runtime configuration for five minutes.--list-servicesshows the services now allowed in the default zone, so you can confirmhttpappears.- After you have tested the service from a client,
--runtime-to-permanentwrites the whole runtime configuration to disk.
A few details about --timeout are worth knowing:
- It works only on runtime changes. Combining it with
--permanentis an error. - When it expires, the service is removed from the runtime configuration. Existing established connections are not cut off, because firewalld tracks connection state.
- If you run
--runtime-to-permanentwhile the timeout is still counting, the service is written to the permanent configuration. The timer still removes it from runtime later, but after the next reload or reboot it returns. Run--runtime-to-permanentonly after the test passes, and expect the runtime copy to expire if you used a timeout. Re-add the service without a timeout, or run--reloadafter saving, so runtime matches permanent.
Opening a service permanently in one step
Once you are confident about a change, you can write it straight to the permanent configuration and reload:
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --query-service=https--permanent changes only the on-disk configuration. The running firewall does not change until --reload, which rebuilds the runtime configuration from the permanent one. --query-service prints yes or no and sets the exit status to 0 or 1, so it works in scripts.
The reload step is the most common thing people forget. If you add a service with --permanent and test straight away, the service still looks blocked, because the runtime configuration has not changed.
Opening several services
You can add more than one service in one command by repeating the option:
sudo firewall-cmd --permanent --zone=public --add-service=http --add-service=https
sudo firewall-cmd --reloadYou can compare the two views to find drift between them:
sudo firewall-cmd --zone=public --list-services
sudo firewall-cmd --permanent --zone=public --list-servicesIf the lists differ, either a runtime-only change exists (lost at the next reload) or a permanent change has not been loaded yet.
Never lock yourself out
The ssh service is normally allowed in the default zone. Before you reload, change zones, or remove services, check that it is present:
sudo firewall-cmd --zone=public --query-service=sshTwo habits protect you when you work over SSH:
- Keep a second SSH session open while you make changes. If the first one hangs, the second still works.
- Prefer a runtime change with
--timeoutfor anything risky, so a mistake corrects itself.
If your SSH daemon listens on a non-standard port, the ssh service (port 22) does not cover it. Open the actual port as well, which is the subject of the next chapter.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Error: INVALID_SERVICE` | Misspelled name, or the service file is not installed | Check the name with `--get-services` |
| `Warning: ALREADY_ENABLED` | The service is already in that zone | No action needed |
| `FirewallD is not running` | The daemon is stopped | `sudo systemctl enable --now firewalld` |
| Service appears in `--permanent --list-services` but not in plain `--list-services` | You forgot `--reload` | Run `sudo firewall-cmd --reload` |
| Service is listed but clients still cannot connect | Wrong zone for the interface, or no process is listening | Check `--get-active-zones` and `ss -tlnp` |
| Service disappeared after reboot | It was added to runtime only | Repeat with `--permanent`, or use `--runtime-to-permanent` |
Putting it together
Suppose a web server should serve HTTPS to the world on eth0, which is in the public zone. Follow this sequence:
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --info-service=https
sudo firewall-cmd --zone=public --add-service=https --timeout=10m
sudo firewall-cmd --zone=public --list-services
curl -sI https://server.example.com | head -n 1
sudo firewall-cmd --runtime-to-permanent
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --zone=public --list-servicesThe first two commands confirm the zone and what https opens. The third opens it temporarily, and the curl request from a client checks that it works. Only then do you save and reload. The last command confirms the service is in the permanent configuration. After the reload, the temporary runtime entry is replaced by the permanent one, so it no longer expires.
Exercise
You run sudo firewall-cmd --permanent --add-service=http on a server and then test from a client. The connection is still refused, and sudo firewall-cmd --list-services does not show http. Name the missing step, and give a command that would also tell you whether http is saved in the permanent configuration.
Show answer
The missing step is sudo firewall-cmd --reload, which loads the permanent configuration into the runtime configuration. You can check the saved configuration with sudo firewall-cmd --permanent --list-services or sudo firewall-cmd --permanent --query-service=http.
Exercise
A server has eth0 in the public zone and eth1 in the internal zone, and the default zone is public. You want only hosts on the internal network to reach a new HTTP service. Which command opens it, and why is --add-service=http without --zone the wrong choice?
Show answer
Run sudo firewall-cmd --permanent --zone=internal --add-service=http and then sudo firewall-cmd --reload. Without --zone, the service goes into the default zone (public), which exposes HTTP to the public network instead of to the internal one.