Sub topic 8
Letting a Service Use a Non-Standard Port
Assume you can use sudo, manage a web service with systemctl, and read basic Linux network information. This chapter uses a RHEL-family system with the SELinux targeted policy.
A network port is a numbered endpoint that a service uses to receive traffic. Web servers commonly use TCP ports 80 and 443. SELinux does not allow a confined service to bind to every port just because the service configuration requests one. The port must have a suitable SELinux type.
For an HTTP service, the usual type is http_port_t. SELinux policy allows the httpd_t process domain to bind to ports labeled with this type.
Why the port label matters
Linux has two separate access-control checks:
- Discretionary access control checks the process user, group, and file permissions.
- Mandatory access control checks the SELinux domain, object type, and policy rules.
A service can pass the normal permission checks and still fail because its requested port has the wrong SELinux label.
| Port | SELinux type | Typical purpose |
|---|---|---|
| 80 | `http_port_t` | Standard HTTP traffic |
| 443 | `http_port_t` | Standard HTTPS traffic |
| 8888 | Often `unreserved_port_t` | Custom port until explicitly labeled |
The label belongs to the port and protocol combination. TCP port 8888 and UDP port 8888 are separate policy objects.
Inspect the current port policy
First check which ports already have the HTTP port type:
sudo semanage port -l | grep '^http_port_t'The output includes port numbers and ranges. Check whether the service is already listening and confirm its configured port separately:
sudo ss -ltnp
sudo systemctl status httpdChanging an SELinux label does not change the web server configuration, open a firewall port, or start the service. Those are separate tasks.
Add a non-standard HTTP port
Suppose Apache is configured to listen on TCP port 8888. Add that port to the existing http_port_t type:
sudo semanage port -a -t http_port_t -p tcp 8888The options mean:
-aadds a port rule.-t http_port_tassigns the HTTP port type.-p tcplimits the rule to TCP.8888identifies the port.
Verify the persistent rule:
sudo semanage port -l | grep '^http_port_t'If port 8888 already has an SELinux label, the add operation fails instead of replacing it. Inspect the exact entry, then modify it only when you have confirmed that the port belongs to this service:
sudo semanage port -l | grep -w 8888
sudo semanage port -m -t http_port_t -p tcp 8888Understand persistence and removal
semanage port changes the SELinux policy store, so the assignment survives a reboot and a full relabel. It is different from a temporary runtime experiment.
Remove the rule when the service no longer needs the port:
sudo semanage port -d -t http_port_t -p tcp 8888After removal, an HTTP service may fail to bind to 8888 even though its ordinary Linux permissions remain correct.
Exercise
Apache is configured for TCP port 8888, but SELinux logs a denial when it starts. What persistent change should you make, and what separate check is still required for remote clients?
Show answer
Add TCP 8888 to httpportt with sudo semanage port -a -t http_port_t -p tcp 8888. Then verify the service is listening and check the host firewall and any external network firewall.