SELinux

Advanced · 16 min

How SELinux labels confine services even as root, how to read a denial, and how to fix it with restorecon, booleans and port labels.

Before thisSpecial permission bits and umaskAccess control listsThe kernel command line and initramfs

Red Hat · 16 min

SELinux

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Red HatReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Ordinary Unix permissions are discretionary: the owner decides, and root bypasses them. SELinux adds mandatory access control. Policy decides, and it applies to root too.
  • Every process and file has a label, `user:role:type:level`. Under the targeted policy what matters is the type: a process in `httpd_t` may read files labelled `httpd_sys_content_t`, and not much else.
  • `getenforce` and `sestatus` show the mode. Enforcing blocks and logs. Permissive only logs. On RHEL 9 and later, really turning SELinux off takes the `selinux=0` kernel parameter.
  • Most denials have one of three causes: a file with the wrong label (fix with `semanage fcontext` and `restorecon`), a feature switched off by a boolean (`setsebool -P`), or a non-standard port (`semanage port -a`).
  • Find denials with `ausearch -m AVC -ts recent` and explain them with `audit2why`. Treat `audit2allow` output as a draft to review, not a fix to apply.

Root is not the last word

Suppose an attacker finds a bug in your web server and runs code as the process. Under ordinary Unix permissions they now have whatever the apache or www-data user has. Worse, if the service runs as root, or the attacker escalates, file permissions stop meaning anything. Root bypasses them by design.

That model is discretionary access control: a file’s owner decides who may read it, and root decides everything. Permission bits and ACLs both work this way. SELinux adds mandatory access control: a central policy says which kinds of process may touch which kinds of object, and nobody, root included, can override it at runtime. Think of a hospital where a doctor’s badge opens the pharmacy but not the server room, regardless of how senior the doctor is. The building’s policy decides, not the person holding the key.

SELinux is on and enforcing by default on Fedora and the RHEL family. Debian and Ubuntu use AppArmor instead, covered in the next lesson.

Labels

Every process and every file carries a security context, shown by adding -Z to familiar commands:

ps -eZ | grep httpd       # process labels
ls -Z /var/www/html       # file labels
id -Z                     # your own

A context has four fields, user:role:type:level, for example system_u:object_r:httpd_sys_content_t:s0. Under the targeted policy that RHEL ships, almost every decision is made on the type, the third field. A process type is often called a domain. The policy contains rules such as “a process in httpd_t may read files of type httpd_sys_content_t”. Anything not explicitly allowed is denied, and logged.

Files get their type from rules that map paths to labels. A new file usually inherits the label of the directory it is created in. Moving a file with mv keeps its old label, which is why moved content so often breaks.

Modes

getenforce               # Enforcing, Permissive or Disabled
sestatus                 # more detail, including the loaded policy
sudo setenforce 0        # permissive until reboot
sudo setenforce 1        # enforcing again

Enforcing blocks and logs. Permissive allows everything but still logs what would have been denied, which makes it a diagnostic tool. If a problem vanishes in permissive mode, SELinux was the cause. Switch back once you have the log entries.

On RHEL 9 and later, SELINUX=disabled in /etc/selinux/config no longer fully turns SELinux off. Red Hat’s adoption notes say the system then boots with SELinux hooks registered but no policy loaded, and point to the selinux=0 kernel parameter (see the kernel command line). Turning it off at all should be very rare.

Reading a denial

Denials are written by the audit system, as AVC records. The audit system in general comes later, in auditing with auditd:

sudo ausearch -m AVC -ts recent
sudo ausearch -m AVC -ts recent | audit2why

The audit2allow man page describes audit2why as translating audit messages “into a description of why the access was denied”. It names the source type, the target type and the class of object, and often says which boolean would allow it.

The three usual fixes

1. The file has the wrong label. The most common case by far. Fix the rule, then apply it, as in the semanage-fcontext man page’s own example:

sudo semanage fcontext -a -t httpd_sys_content_t "/srv/site(/.*)?"
sudo restorecon -Rv /srv/site

restorecon resets files to what the rules say. chcon can set a label directly, but the next relabel undoes it, so use it only for an experiment.

2. A feature is switched off by a boolean. The policy includes switches for common optional behaviour:

getsebool -a | grep httpd
sudo setsebool -P httpd_can_network_connect_db on

-P makes it persistent.

3. A service uses a non-standard port. Ports have labels too. Moving SSH to 2222, as discussed in hardening SSH, needs:

sudo semanage port -a -t ssh_port_t -p tcp 2222

If none of those fits, audit2allow -M can generate a local policy module from the denials. The man page warns that care “must be exercised while acting on the output”, because it can recommend broad changes. Read what it generates. If it allows httpd_t to write to etc_t, the real fix is elsewhere.

None of these commands were run. SELinux needs a Linux kernel and there is none here. They are written from the man7.org pages for restorecon, semanage-fcontext and audit2allow, and from Red Hat’s RHEL 9 adoption notes, checked 2026-11-28. Red Hat’s RHEL 10 SELinux guide refused automated fetches, so I could not quote it directly.

About the video

The embed is all of “Stop disabling SELinux | RHEL Field Guide” from the Red Hat channel, March 2026, about 16 minutes, presented by Scott McBrien. Its chapters cover contexts, ps -Z, the targeted policy, ls -Z, file placement and inheritance, port contexts, getenforce and sestatus, and troubleshooting with setenforce. It is light on ausearch, audit2why and booleans, which this lesson adds. For a longer troubleshooting session, Red Hat’s “Into the Terminal” episode is in deeper. I judged the video from its chapters and description, and English from its English caption tracks.

Where this fits

SELinux labels everything. AppArmor, the other common Linux security module, takes a path-based approach that is easier to start with: AppArmor.

Check yourself

  1. 01You move a website from `/var/www/html` to `/srv/site`. File permissions are fine, but Apache returns 403 and the audit log shows an AVC denial for `httpd_t`. What is the fix?

    Label the new location: `semanage fcontext -a -t httpd_sys_content_t "/srv/site(/.*)?"` then `restorecon -Rv /srv/site`. The files kept a label that `httpd_t` may not read.

  2. 02Why is `chcon` a worse fix than `semanage fcontext` plus `restorecon` for the same problem?

    `chcon` changes the label on the files directly, but the policy's file-context rules still say otherwise, so a relabel or a later `restorecon` puts the old label back. `semanage fcontext` changes the rule, which makes the fix permanent.

  3. 03PHP on your web server cannot connect to a database on another host, with an SELinux denial. What kind of fix is most likely, and how do you find it?

    A boolean. Run `audit2why` on the denial, which often names it, here `httpd_can_network_connect_db`, and enable it with `setsebool -P`.

  4. 04What does switching to permissive mode tell you when debugging?

    If the problem disappears in permissive mode, SELinux caused it, and the logged denials show what to fix. Then return to enforcing. Permissive is a diagnostic, not a setting to leave on.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark