Sub topic 11
Defining a Custom Service for Your Application
This chapter assumes you know basic Linux commands, sudo, TCP and UDP ports, systemctl, and IPv4 CIDR notation. You should also know that an application must be listening on a port before a firewall rule can make that port reachable.
What a Custom Service Provides
A firewalld service is a named collection of ports and protocols. Predefined services include ssh, http, and https. A custom service lets you give your application's port a meaningful name instead of adding raw port rules directly to a zone.
For example, suppose an internal application listens on TCP port 9000. You can define a service named myapp, add port 9000/tcp to it, and then enable myapp in a zone.
The custom service does not start or configure the application. It only describes which traffic firewalld should allow.
A service definition can later contain several ports. This is useful when an application grows to need, for example, a web port and a separate metrics port.
Create and Enable the Service
The following commands create a permanent custom service named myapp, describe it, add TCP port 9000, and enable it in the default zone.
sudo firewall-cmd --permanent --new-service=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 first reload makes the new service definition available to firewalld. The second reload activates the zone change. The commands use the permanent configuration, so the service remains enabled after a reboot.
You can inspect the service definition and the zone rule separately:
sudo firewall-cmd --info-service=myapp
sudo firewall-cmd --list-services
sudo firewall-cmd --list-all--info-service=myapp should show port 9000/tcp. The zone listing should include myapp under its enabled services.
The service definition is normally stored as an XML file under /etc/firewalld/services/. Use firewall-cmd to manage it rather than editing that file during routine administration. The command keeps the service integrated with firewalld's configuration.
> Callout note > Choose a service name that describes the application, not only its current port. If the application later needs another port, you can add it to the same service without changing every zone rule.
Change or Remove a Custom Service
To add another port to the service, update the service definition and reload:
sudo firewall-cmd --permanent --service=myapp \
--add-port=9443/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --info-service=myappThis changes the service definition. It does not automatically enable myapp in additional zones. Each zone must explicitly allow the service.
To stop allowing the application in the current zone while keeping the service definition for later use:
sudo firewall-cmd --permanent --remove-service=myapp
sudo firewall-cmd --reloadTo remove a port from the service definition itself:
sudo firewall-cmd --permanent --service=myapp \
--remove-port=9000/tcp
sudo firewall-cmd --reloadRemoving a port from the service affects every zone that enables myapp. Check all intended zones before making that change.
Common Errors
- Creating the service but forgetting to add it to a zone.
- Adding
9000/udpwhen the application listens on9000/tcp. - Reloading before a permanent service definition exists.
- Allowing the firewall port while the application is not listening.
- Assuming a custom service restricts clients. It only allows matching traffic. Use zones or rich rules when source restrictions are required.
Exercise
An application listens on TCP port 9000, and firewalld already has a custom service named myapp. Which command adds TCP port 9443 to that service permanently?
Show answer
sudo firewall-cmd --permanent --service=myapp --add-port=9443/tcp