Sub topic 2
Checking and changing the SELinux mode
This chapter assumes you can use the shell with sudo, edit files under /etc, and reboot a RHEL-family system (RHEL, Rocky, AlmaLinux or Fedora). It also assumes you know what DAC and MAC mean from the previous chapter. By the end you will be able to read the current mode, switch it for a test, make the change persistent, and recover safely from a mode change that goes wrong.
The three modes
SELinux runs in one of three modes.
| Mode | Policy is loaded | Violations are blocked | Violations are logged |
|---|---|---|---|
| Enforcing | Yes | Yes | Yes |
| Permissive | Yes | No | Yes |
| Disabled | No | No | No |
Enforcing is the normal production state. Permissive loads the policy and evaluates every access, but it only writes an AVC (Access Vector Cache) message to the audit log when the policy would have denied the access. The access then succeeds. Disabled turns SELinux off. Nothing is evaluated, and new files are created without labels.
Permissive is a diagnostic mode, and disabled is a different thing from both. Permissive keeps the labels current and keeps the log trail. Disabled does neither, which is why leaving disabled has a cost, covered below.
Checking the current mode
There are two commands to know.
getenforce
sestatusgetenforce prints one word: Enforcing, Permissive or Disabled. Use it in scripts and quick checks.
sestatus prints more detail. Typical output on a RHEL-family system:
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33Two lines matter most. Current mode is what the kernel is doing right now. Mode from config file is what the system will do after the next boot. They can differ, and when they do, someone has changed the mode at runtime and not made it persistent, or the reverse.
Changing the mode at runtime
setenforce switches between enforcing and permissive in the running kernel. It takes effect immediately and lasts until the next reboot.
sudo setenforce 0 # permissive
getenforce
sudo setenforce 1 # enforcing
getenforcesetenforce also accepts the words Permissive and Enforcing. It cannot switch to or from disabled. If SELinux is disabled, setenforce fails with an error such as setenforce: SELinux is disabled.
Runtime changes need no reboot and are easy to undo. That makes them the right tool for a quick test: "does this failure go away if SELinux does not block anything?" If the answer is yes, SELinux is involved, and you can go on to find the denial. Switch back to enforcing as soon as the test is done.
Changing the mode persistently
The boot-time mode comes from /etc/selinux/config.
# /etc/selinux/config
SELINUX=enforcing
SELINUXTYPE=targetedSELINUX= accepts enforcing, permissive or disabled. SELINUXTYPE= selects which policy to load. The default, targeted, is covered in a later chapter. Change the first line with an editor, or with sed:
sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
grep '^SELINUX=' /etc/selinux/configThe edit applies at the next boot. To apply it to the running system, run setenforce as well, or reboot. If you only edit the file, sestatus shows a mismatch between the two mode lines until you reboot. That mismatch is normal.
Check the file for typos before rebooting. A bad SELINUX= value can leave the system in an unexpected state, and you may not notice until the machine is already down.
Kernel command-line overrides
You can override the config file for one boot by adding parameters to the kernel command line at the boot loader menu:
enforcing=0boots in permissive mode.enforcing=1boots in enforcing mode.selinux=0boots with SELinux disabled.
The parameter wins over /etc/selinux/config. This is the main recovery tool when a policy or labeling problem keeps a system from booting normally. At the GRUB menu, press e, append enforcing=0 to the line that starts with linux, and boot with Ctrl+X. You get a working system in which you can fix the problem. Nothing changes permanently.
To make a kernel parameter persistent on RHEL-family systems, use grubby:
sudo grubby --update-kernel ALL --args selinux=0
sudo grubby --update-kernel ALL --remove-args selinux=0The first command disables SELinux on every installed kernel. The second reverses it.
On current kernels, setting SELINUX=disabled in the config file may not completely disable SELinux. Newer kernels removed the ability to disable SELinux after boot, and the boot-time parameter selinux=0 is the reliable way. Check sestatus after a reboot instead of assuming. This is another reason to prefer permissive over disabled.
Moving between modes
The transitions are not symmetrical. Going between enforcing and permissive is cheap. Going to or from disabled needs a reboot, and coming back needs a relabel.
Why leaving disabled needs a relabel
While SELinux is disabled, the kernel does not assign labels. Files created during that time have no security.selinux extended attribute, or they keep stale ones. If you then boot in enforcing mode, services are denied access to unlabeled files, and the system may fail to boot properly.
The safe path out of disabled is:
- Set
SELINUX=permissivein/etc/selinux/config. - Request a full relabel on the next boot.
- Reboot.
- Check the log for denials, then switch to enforcing.
sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
sudo touch /.autorelabel
sudo rebootThe file /.autorelabel tells the boot process to relabel the whole filesystem and then reboot again. On a large filesystem this takes minutes to hours. Starting in permissive mode means any problem remaining after the relabel is logged instead of blocking boot. Once the system is up and the audit log is clean, run sudo setenforce 1 and set SELINUX=enforcing in the config file.
Choosing between permissive and disabled
When something fails and you suspect SELinux, the instinct is to disable it. Permissive is almost always the better choice:
- Permissive records every denial that would have occurred. Disabled records nothing, so you learn nothing about what to fix.
- Permissive keeps labeling new files correctly. Disabled does not, and you pay for that later with a relabel.
- Returning from permissive to enforcing is one command. Returning from disabled takes a reboot and a relabel.
Permissive has one limit. In enforcing mode, a process usually stops at its first denial, so you see only that one. In permissive mode the process continues and you see all the denials in a sequence. This is useful. It lets you collect the whole list in one pass instead of fixing one denial, retrying, and hitting the next.
A short worked check
This sequence confirms the state, tests whether SELinux is involved in a failure, and restores the original mode. The test uses a harmless file access as the example. Replace it with the failing action you are investigating.
getenforce
sudo setenforce 0
getenforce
# reproduce the failing action here, for example: curl -I http://localhost/
sudo setenforce 1
getenforce
sestatus | grep -i modeIf the action fails in enforcing mode and works in permissive mode, SELinux is the cause. Look in the audit log for the denial and fix the label, boolean or port, as later chapters show. If it fails in both modes, SELinux is not the problem. Look at file permissions, the service configuration or the firewall.
Common errors
- Mismatch between
Current modeandMode from config file. This is expected aftersetenforce. It becomes a problem when you think a change is permanent. Edit/etc/selinux/configtoo. setenforce: SELinux is disabled. The system booted with SELinux off. Enable it through the config file or kernel parameters, relabel, and reboot.- Boot fails or hangs after enabling SELinux. Boot with
enforcing=0on the kernel command line, create/.autorelabel, and reboot. - A forgotten
setenforce 0. The system silently runs without protection until the next reboot. CompareCurrent modewithMode from config fileinsestatusas part of routine checks. - A typo in the config file. Verify with
grep '^SELINUX' /etc/selinux/configbefore you reboot.
Exercise
A server shows "Current mode: permissive" and "Mode from config file: enforcing" in sestatus. What most likely happened, and what happens at the next reboot? Then explain why you should not move from "disabled" straight to "enforcing".
Show answer
Someone ran "setenforce 0" at runtime without editing /etc/selinux/config. At the next reboot the system returns to enforcing, because the config file is read at boot. Going straight from disabled to enforcing is unsafe because files created while SELinux was off are unlabeled or mislabeled, so the policy denies services access to them. The safe path is to set SELINUX=permissive, create /.autorelabel, reboot, review the audit log, and only then switch to enforcing.
Exercise
Which command do you use to boot a single time in permissive mode from the GRUB menu when a labeling problem prevents normal boot, and does it change /etc/selinux/config?
Show answer
Press "e" at the GRUB menu, append "enforcing=0" to the kernel (linux) line, and boot. It does not change /etc/selinux/config, so the next normal boot uses the configured mode.