SELinux is the mandatory access control system in the RHEL family — Rocky Linux, AlmaLinux, Fedora, CentOS Stream, RHEL itself. Most operators learn one command (setenforce 0) because every unfamiliar problem on a fresh box turns out to be SELinux. This article is the alternative path: enough vocabulary and tooling to keep SELinux in enforcing mode in production without throwing away an hour every time you add a new service.
How to verify
Check the current mode and the context of a file or process you care about:
getenforce
sudo sestatus
ls -Z /var/www/html | head
ps -eZ | grep nginx
sudo ausearch -m AVC -ts recent
sudo semanage boolean -l | grep httpd
sestatus prints the mode, the policy version, and the loaded policy name. ausearch -m AVC is the audit log filter for SELinux denials — the single most useful debugging command.
What’s happening
SELinux labels every process and every file with a four-part context: user:role:type:level, e.g. system_u:object_r:httpd_sys_content_t:s0. The type is the part that matters day to day. A policy rule says, in effect, “a process of type httpd_t may read files of type httpd_sys_content_t.” Anything else is denied and logged to the audit subsystem.
The modes are enforcing (rules enforced), permissive (denials logged but allowed), and disabled (kernel skips SELinux entirely — requires a reboot to leave). The trap most operators fall into is using setenforce 0 as a debugger. The right call is to leave the host in enforcing, run ausearch -m AVC to read the denial, and either add a boolean (a pre-baked policy toggle) or write a tiny custom module with audit2allow.
Booleans are the lightweight escape hatch: httpd_can_network_connect, nis_enabled, setrousable_pwexpire — each one flips a chunk of policy on or off. semanage boolean -l lists every boolean and its current state. For anything not covered by a boolean, the custom module path is short and well-understood.
The procedure
- Verify the host is in enforcing mode.
sudo setenforce 1 && sudo sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config. The file matters becausesetenforceis reset on reboot otherwise. - When a service fails, do not change the mode — read the denial.
sudo ausearch -m AVC -ts recentprints every AVC (Access Vector Cache) denial with timestamps, source context, target context, and the operation that was blocked. - Check whether a boolean already covers the case.
sudo semanage boolean -l | grep <pattern>. For example, an Nginx pod that needs to call a back-end on a non-standard port:sudo setsebool -P httpd_can_network_connect 1. The-Pflag makes it persist across reboots. - Fix the file context when the issue is “process can’t read its own files”.
sudo semanage fcontext -a -t httpd_sys_content_t '/srv/myapp(/.*)?'thensudo restorecon -Rv /srv/myapp. Thefcontextrule survives arestorecon;chconalone does not. - If no boolean and no fcontext fits, build a custom module.
sudo ausearch -m AVC -ts recent -c myapp | audit2allow -M myapp_localthensudo semodule -i myapp_local.pp. Review the generated.tefile before installing —audit2allowhappily generates a module that grants more than you want. - Port management for non-default ports.
sudo semanage port -a -t http_port_t -p tcp 8081. SELinux types know which ports a daemon is “supposed” to bind; without this, an Nginx listening on 8081 cannot bind. - Log every SELinux denial to your aggregator and alert on new types. A new
AVC: deniedafter months of silence is either a new feature or a compromise — always worth a glance.
Common pitfalls
setenforce 0is reverted on reboot if/etc/selinux/configsaysenforcing. Operators who change one and not the other get a “fixed” host that breaks again at the next maintenance window.chconchanges a file’s context immediately but the change is wiped byrestoreconor bysemanage fcontextruns. Always pair a context change with an fcontext rule.- A custom module installed with
semodule -ipersists across reboots and across SELinux policy updates — the upgrade does not re-evaluate it. Review your local modules quarterly. - Some “SELinux denials” are actually
dontauditrules silently suppressed.sudo semodule -DBre-enables dontaudit logging temporarily while you diagnose, thensudo semodule -Brestores normal behaviour. - Container runtimes (Podman, Docker on RHEL) ship their own SELinux types and labels —
container_t,container_file_t. A volume mount without:zor:Zlands as the wrong type and the container cannot read it.
In the engagements we run on RHEL-family hosts, SELinux stays enforcing. The audit-then-allow loop is part of Managed Operations — when a new deployment breaks on SELinux, the fix is a tiny policy module shipped with the deploy, not a setenforce 0 someone forgets about.