Sub topic 11
Defining a custom service for your application
Earlier chapters opened predefined services such as http and individual ports such as 8080/tcp. This chapter covers a third option: a custom service, a named group of ports and protocols that you define for your own application.
This chapter assumes you can run firewall-cmd with sudo and that you know the difference between runtime and permanent configuration. It also assumes you can tell zones from services and read a TCP port number.
Why define a service
Suppose an internal application listens on TCP 9000. You can open it with --add-port=9000/tcp, and that works. The cost shows up later:
- The rule says nothing about its purpose. A teammate reading
9000/tcpin--list-allhas to guess what it is for. - If the application grows to use a second port, you must remember to open it on every server and in every zone that needs it.
- You repeat the port numbers in every script, playbook, or runbook that touches the firewall.
A service moves those facts into one named definition. Rules refer to myapp, and the definition says which ports myapp means. When the application changes, you edit the definition once.
| Approach | Best for | Weakness |
|---|---|---|
| Predefined service (`http`) | Well-known software that ships with firewalld | Does not exist for your own application |
| Raw port (`9000/tcp`) | One-off or temporary access | The purpose is not recorded, and it is easy to forget when it grows |
| Custom service (`myapp`) | Your own application, especially with several ports or several servers | You maintain one extra definition |
What a service contains
A service is a small definition with these parts:
- A short name and a description, which are labels for humans.
- One or more ports, each with a protocol (
tcp,udp,sctp, ordccp). A port can be a single number or a range such as9000-9010. - Optionally, extra protocols (such as
icmporesp), source ports, destination addresses, and connection-tracking helper modules.
Most applications need only the name, the description, and the ports. A service does not say which zone it applies to. A service defined once can be allowed in any zone, and it takes effect only in the zones where you add it.
firewalld looks for service definitions in two places:
| Location | Contents | Edit it? |
|---|---|---|
| `/usr/lib/firewalld/services/` | Definitions shipped with the package | No. Package updates overwrite it. |
| `/etc/firewalld/services/` | Your custom services and overrides | Yes. This is where `firewall-cmd` writes. |
If the same service name exists in both places, the file in /etc/firewalld/services/ wins. That lets you override a predefined service, but a new name is clearer for a new application.
Creating the service
Custom services exist only in the permanent configuration. There is no runtime-only version of --new-service, so every command in this section uses --permanent.
sudo firewall-cmd --permanent --new-service=myapp
sudo firewall-cmd --permanent --service=myapp --set-short="myapp"
sudo firewall-cmd --permanent --service=myapp --set-description="Internal web application"
sudo firewall-cmd --permanent --service=myapp --add-port=9000/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --add-service=myapp
sudo firewall-cmd --reloadThe steps do the following:
--new-service=myappcreates an empty definition and writes/etc/firewalld/services/myapp.xml.--set-shortand--set-descriptionfill in the labels. They are optional, but they are what--info-serviceshows later.--add-port=9000/tcpadds the port to the service definition. This differs from the earlier chapter, where the same option added a port to a zone. Here the--service=myappoption makes the command edit the service, not a zone.- The first
--reloadmakes the running firewall aware thatmyappexists. --add-service=myappallows the service in the default zone's permanent configuration.- The second
--reloadapplies that to the runtime configuration.
Defining a service does not open anything. Until you add the service to a zone, traffic to port 9000 is still handled by that zone's normal rules.
Looking at what you created
sudo firewall-cmd --permanent --info-service=myapp
sudo firewall-cmd --permanent --service=myapp --get-ports
cat /etc/firewalld/services/myapp.xmlThe first command prints the short name, description, ports, and other fields. The file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>myapp</short>
<description>Internal web application</description>
<port protocol="tcp" port="9000"/>
</service>The file is plain XML, so you can also create it with an editor or ship it from configuration management. After placing a file in /etc/firewalld/services/, run sudo firewall-cmd --reload. Keep the file readable by root, and match the filename to the service name, because firewalld uses the filename as the name.
Services with several ports
The advantage of a service shows when the application needs more than one port. Say myapp also accepts a metrics scrape on TCP 9100 and a UDP discovery port at 9200:
sudo firewall-cmd --permanent --service=myapp --add-port=9100/tcp
sudo firewall-cmd --permanent --service=myapp --add-port=9200/udp
sudo firewall-cmd --permanent --service=myapp --add-port=9300-9310/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --info-service=myappAfter the reload, every zone that already allows myapp allows the new ports as well. You do not touch the zone. This is the main reason to prefer a service over loose ports when an application has a stable set of them.
To remove a port from the definition, use the matching option:
sudo firewall-cmd --permanent --service=myapp --remove-port=9200/udp
sudo firewall-cmd --reloadOther fields you may need
Most services use only ports. These other options exist for special cases:
--add-protocol=NAMEallows a whole IP protocol, such asespfor IPsec, with no port number.--add-source-port=PORT/PROTOmatches traffic from a particular source port. This is rare and only useful when a peer always connects from a fixed port.--set-destination=ipv4:ADDRESSrestricts the service to traffic going to a given local address.--add-module=NAMEloads a connection-tracking helper, such asftp, for protocols that open extra connections dynamically.
Setting a destination is easy to get wrong. If the address is not the one clients actually connect to, the service silently matches nothing. Leave it unset unless you have a reason.
Reusing a predefined service as a starting point
If your application is a variant of something firewalld already knows, copy the shipped definition and change it, instead of writing it from scratch:
sudo firewall-cmd --permanent --new-service-from-file=/usr/lib/firewalld/services/http.xml --name=myweb
sudo firewall-cmd --permanent --service=myweb --remove-port=80/tcp
sudo firewall-cmd --permanent --service=myweb --add-port=8081/tcp
sudo firewall-cmd --reloadThe new name myweb leaves the original http untouched. Avoid editing the file under /usr/lib/firewalld/services/ directly, because an update would overwrite your change.
Editing and deleting services
Changing a service's ports affects every zone that allows it, and it takes effect on the next --reload. Check where the service is used before you change it:
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=public --query-service=myapp
sudo firewall-cmd --permanent --zone=public --query-service=myappThe two query commands print yes or no. Comparing runtime and permanent answers also reveals changes you forgot to save.
To retire the service, remove it from every zone first, then delete the definition:
sudo firewall-cmd --permanent --remove-service=myapp
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --delete-service=myapp
sudo firewall-cmd --reloadfirewalld refuses to delete a service that a zone still references, or leaves the configuration in an inconsistent state. Removing the zone reference first avoids that.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `INVALID_SERVICE` when running `--add-service=myapp` | The service exists only in the permanent configuration and the runtime has not loaded it | Run `sudo firewall-cmd --reload`, then add the service again |
| `NAME_CONFLICT` on `--new-service` | A service with that name already exists | Pick another name, or edit the existing one |
| `myapp` is missing from `--get-services` | The listing shows runtime services, and you have not reloaded | Reload, or use `--permanent --get-services` |
| Service is allowed but the port is unreachable | Nothing listens on the port, or the service is in the wrong zone | Use `ss -tlnp` on the server and check the zone of the connection's interface |
| Changes disappear after a reboot | You edited only the runtime configuration | Repeat with `--permanent`, or use `--runtime-to-permanent` |
The fourth row matters most. A service allows traffic to a port, and the application must still be listening. The verification chapter covers ss and nc for this.
A complete worked example
This sequence defines a service with two ports, allows it in the default zone, and confirms the result without changing anything outside the application:
sudo firewall-cmd --permanent --new-service=myapp
sudo firewall-cmd --permanent --service=myapp --set-short="myapp"
sudo firewall-cmd --permanent --service=myapp --set-description="Internal web application"
sudo firewall-cmd --permanent --service=myapp --add-port=9000/tcp
sudo firewall-cmd --permanent --service=myapp --add-port=9100/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --add-service=myapp
sudo firewall-cmd --reload
sudo firewall-cmd --list-services
sudo firewall-cmd --query-service=myapp
sudo firewall-cmd --info-service=myapp--list-services should now include myapp next to the services you had before, and --query-service should print yes. From a remote host, nc -zv server.example.com 9000 should connect if the application is running.
Exercise
You created a service with sudo firewall-cmd --permanent --new-service=billing and added --add-port=7000/tcp to it. Running sudo firewall-cmd --add-service=billing then fails with INVALID_SERVICE. What is missing, and what would you run next to allow the service in the default zone permanently?
Show answer
The running firewall has not loaded the new permanent service yet. Run sudo firewall-cmd --reload so the runtime learns the service. Then run sudo firewall-cmd --permanent --add-service=billing followed by sudo firewall-cmd --reload. Confirm with sudo firewall-cmd --query-service=billing, which should print yes.
Exercise
The application behind myapp now also needs UDP port 9200. Three servers allow myapp in the public zone. What do you change, and do you have to edit the zones?
Show answer
Add the port to the service definition on each server: sudo firewall-cmd --permanent --service=myapp --add-port=9200/udp, then sudo firewall-cmd --reload. You do not edit the zones, because each zone already allows myapp and picks up the new port from the definition.