Skip to content

SELinux basics for operators who keep choosing permissive

A working operator's introduction to SELinux on RHEL-family hosts — modes, contexts, booleans, and the audit-then-allow loop that beats setenforce 0.

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

  1. Verify the host is in enforcing mode. sudo setenforce 1 && sudo sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config. The file matters because setenforce is reset on reboot otherwise.
  2. When a service fails, do not change the mode — read the denial. sudo ausearch -m AVC -ts recent prints every AVC (Access Vector Cache) denial with timestamps, source context, target context, and the operation that was blocked.
  3. 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 -P flag makes it persist across reboots.
  4. 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(/.*)?' then sudo restorecon -Rv /srv/myapp. The fcontext rule survives a restorecon; chcon alone does not.
  5. If no boolean and no fcontext fits, build a custom module. sudo ausearch -m AVC -ts recent -c myapp | audit2allow -M myapp_local then sudo semodule -i myapp_local.pp. Review the generated .te file before installing — audit2allow happily generates a module that grants more than you want.
  6. 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.
  7. Log every SELinux denial to your aggregator and alert on new types. A new AVC: denied after months of silence is either a new feature or a compromise — always worth a glance.

Common pitfalls

  • setenforce 0 is reverted on reboot if /etc/selinux/config says enforcing. Operators who change one and not the other get a “fixed” host that breaks again at the next maintenance window.
  • chcon changes a file’s context immediately but the change is wiped by restorecon or by semanage fcontext runs. Always pair a context change with an fcontext rule.
  • A custom module installed with semodule -i persists across reboots and across SELinux policy updates — the upgrade does not re-evaluate it. Review your local modules quarterly.
  • Some “SELinux denials” are actually dontaudit rules silently suppressed. sudo semodule -DB re-enables dontaudit logging temporarily while you diagnose, then sudo semodule -B restores normal behaviour.
  • Container runtimes (Podman, Docker on RHEL) ship their own SELinux types and labels — container_t, container_file_t. A volume mount without :z or :Z lands 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.