Candid photograph of a Linux server terminal with a soft olive-green glow against a dark slate background, conveying a calm technical atmosphere.

Step-by-step guides for system administrators — covering command-line basics, web server setup, and preparation material for technical interviews.

Browse Tutorials

Configuring SELinux policies for custom applications on RHEL

SELinux adds mandatory access control to RHEL systems, enforcing rules that operate independently of normal Unix ownership and permissions. When a custom application runs under an unexpected path, opens a non-standard port, or writes to a private directory, SELinux may block it even when traditional permissions look correct.

The safest approach is to describe the application’s intended behaviour, label its files correctly, and create the smallest policy required. This avoids the common mistake of switching SELinux to permissive mode whenever an access denial appears.

These techniques apply to RHEL and closely related systems such as CentOS Stream and Fedora. They are useful for internal APIs in Sydney data centres, staging servers in Melbourne, or workloads hosted in an Australian cloud region where security controls and audit records must remain predictable.

Before making changes, confirm the operating system version, application owner, service unit, listening ports, and expected data paths. A short maintenance window aligned with local business hours can make testing easier, particularly when the application supports customers across Australian time zones.

Confirm SELinux is enforcing

Start by checking the current mode and policy:

getenforce
sestatus

A result of Enforcing means SELinux is actively denying unauthorised actions. Record the loaded policy, booleans, and file labels before changing anything:

semanage boolean -l
ls -Z /opt/myapp
ps -eZ | grep myapp

Do not use setenforce 0 as a permanent solution. Temporary permissive mode can help distinguish an SELinux problem during controlled testing, but it removes an important protection and can hide application design errors.

Map the application’s behaviour

List every resource the service needs: executable files, configuration, logs, sockets, uploaded content, databases, and network destinations. A service launched by systemd may run with a different user, working directory, or environment from an interactive shell.

For example, an application installed under /opt/myapp might need to read /etc/myapp/app.conf, write /var/lib/myapp, and bind to TCP port 8443. If Nginx proxies traffic to it, review relevant Nginx server guides alongside SELinux rules.

Capture denials while exercising each feature:

ausearch -m AVC -ts recent
journalctl -t setroubleshoot --since "10 minutes ago"

Reproduce one operation at a time. A clean audit trail makes it easier to identify whether the denial concerns a file type, process domain, port label, or boolean.

Generate a focused policy

Install the policy utilities if required:

dnf install policycoreutils-python-utils setroubleshoot-server

After collecting a small set of verified denials, inspect them:

ausearch -m AVC -c myapp --raw | audit2why

audit2allow can turn denials into a loadable module, but its output is a starting point rather than an automatic approval. Generate a candidate policy with a meaningful name:

ausearch -m AVC -c myapp --raw | audit2allow -M myapp_local
semodule -i myapp_local.pp

Review myapp_local.te before installation. Remove rules caused by incorrect paths, missing labels, or accidental test activity. A broad allow rule may make the error disappear while granting the process access it does not genuinely need.

Label files and ports correctly

SELinux relies heavily on contexts. Define persistent labels for custom paths rather than applying temporary labels with chcon:

semanage fcontext -a -t myapp_var_lib_t "/opt/myapp/data(/.*)?"
restorecon -Rv /opt/myapp/data

The type must exist in the policy module. For an application that serves web content, an existing type such as httpd_sys_content_t may be appropriate; private application data should generally use a dedicated type.

Label non-standard listening ports with a recognised port type:

semanage port -a -t http_port_t -p tcp 8443
semanage port -l | grep http_port_t

Choose the type according to the process and protocol. Avoid assigning a web-server port label merely because it is convenient.

Resource Typical check Persistent fix
Application files ls -Z /opt/myapp semanage fcontext and restorecon
Service process ps -eZ Policy domain and systemd configuration
Custom TCP port semanage port -l semanage port -a
Boolean-controlled feature getsebool -a setsebool -P
AVC denial ausearch -m AVC Correct label or narrow module rule

Test the module safely

Restart the service and test each documented function:

systemctl restart myapp
systemctl status myapp
curl -vk https://127.0.0.1:8443/health

Monitor both application logs and SELinux events. A successful health check does not prove that uploads, scheduled jobs, backups, or outbound connections work correctly.

Remove temporary permissive settings and verify the module after a reboot or policy reload. Test on a staging host before production, then keep the policy package, source file, installation command, and rollback command in the same administration repository.

Keep custom rules maintainable

A small policy is easier to audit and update than a generated collection of unrelated allowances. Store local modules in version control and document why each rule exists, which RHEL release was tested, and which service account uses it.

Useful maintenance habits include:

When a vendor changes its installation path or service architecture, reassess the policy rather than repeatedly appending generated rules. This is especially important for systems subject to Australian privacy expectations or internal controls for data held in Sydney and Melbourne regions.

Troubleshoot without weakening protection

If access still fails, compare the process context, file context, and port context:

ps -eZ | grep myapp
ls -lZ /opt/myapp/data
semanage port -l | grep 8443

Use matchpathcon to see the expected label for a path:

matchpathcon /opt/myapp/data

A denial may result from a mislabeled file, a missing boolean, a service running in the wrong domain, or a policy rule that was generated from an invalid test. Correct the underlying model first, then generate a narrowly scoped rule only when the behaviour is legitimate.

SELinux policy work is most reliable when treated as application documentation expressed in enforceable form. The key principle to remember is simple: label resources accurately, define the application’s real behaviour, and grant the smallest access required.