Sub topic 2
Checking and Changing the SELinux Mode
This chapter assumes you can use the Linux command line with sudo, manage services with systemctl, and understand normal file permissions. It also assumes you are working on a RHEL-family system such as RHEL, Rocky Linux, AlmaLinux, or Fedora.
SELinux adds mandatory access control (MAC) to Linux. MAC is a security system where the policy decides what a process may access, even when normal file permissions would allow it. Normal ownership and permission bits are called discretionary access control (DAC), because the file owner can usually change them.
SELinux has three modes:
| Mode | What happens |
|---|---|
| Enforcing | SELinux blocks actions that the policy denies and records the denial. |
| Permissive | SELinux allows the action but records the denial. |
| Disabled | The SELinux policy is not loaded. |
Permissive mode is useful for short troubleshooting sessions. It is not a security solution because denied actions are allowed. Disabled mode removes SELinux protection entirely.
Check the current mode
Use getenforce for a quick answer. Use sestatus when you also need the policy name, configuration file, or loaded status.
getenforce
sestatusTypical output from getenforce is one of these values:
EnforcingPermissiveDisabled
The sestatus output distinguishes the running mode from the mode configured for the next boot. This matters because a runtime change does not automatically persist after a restart.
Change the mode temporarily
setenforce changes the running mode until the next reboot. It can switch between enforcing and permissive, but it cannot enable SELinux when the current mode is disabled.
sudo setenforce 0
getenforce
sudo setenforce 1
getenforceThe value 0 means permissive, and 1 means enforcing. You can also write the mode names:
sudo setenforce Permissive
sudo setenforce EnforcingA common diagnostic process is to switch to permissive mode, reproduce one problem, inspect the audit log, and return immediately to enforcing mode.
Make the mode persistent
The file /etc/selinux/config controls the mode selected at boot. Inspect it before changing it:
grep '^SELINUX=' /etc/selinux/configTo select enforcing mode after the next reboot, edit the file so it contains:
SELINUX=enforcingFor example, use an editor with sudo:
sudo vi /etc/selinux/configA change in this file does not change the current mode. Verify the current mode separately with getenforce. If SELinux is currently disabled, plan the transition carefully. Enabling it may require a complete filesystem relabel on the next boot, and the server can take longer to start.
After a persistent change, reboot during an approved maintenance window:
sudo systemctl rebootThen verify both the active mode and the boot configuration:
getenforce
sestatusDo not use permissive mode to hide an application problem permanently. The next step is to identify the policy denial and fix its cause, such as an incorrect file label, port label, or required boolean.
Exercise
You run sudo setenforce 0, see Permissive, and then reboot. The server returns as Enforcing. Why?
Show answer
setenforce changed only the runtime mode. The boot configuration still selected enforcing mode, so the reboot restored it.