LLI Wiki — My Digital Knowledge Base

SELinux essential commands on RHEL

Check SELinux state, diagnose denials, manage booleans and file contexts, and disable SELinux if needed.

SELinux (Security-Enhanced Linux) is the mandatory access control system of RHEL and its derivatives. This page collects common commands to check its state, diagnose denials and fix file contexts or booleans.

Check the state

Command Purpose
sestatus Show the current SELinux status
getenforce Show the current mode (enforcing, permissive, disabled)
ls -Z Show the SELinux context of files
ps -Z Show the SELinux context of processes
sesearch Search the currently loaded policy
getsebool Show the value of booleans
matchpathcon Show the context a file should have

Change the mode

sudo setenforce 0   # permissive
sudo setenforce 1   # enforcing

Diagnose denials

Denials are logged in the audit log. audit2why explains the cause of a denial:

grep denied /var/log/audit/audit* | audit2why

To generate a policy module that allows the denied accesses (review it before installing):

grep logrotate /var/log/audit/audit* | audit2allow -M logselinux
semodule -i logselinux.pp

Booleans

List the booleans related to a service, then change the one that fits:

sudo semanage boolean -l | grep logrotate
sudo setsebool -P <boolean_name> 1

Example with NFS-mounted home directories:

getsebool -a | grep -i home | grep nfs
sudo setsebool use_nfs_home_dirs on

Without the -P option, the change does not persist across a reboot.

File contexts

To let a service access a non-standard directory, assign it a context:

sudo semanage fcontext -a -t logrotate_var_lib_t '/var/lib/appdb/trace(/.*)?'
sudo restorecon -Rv /var/lib/appdb/trace
ls -alZ /var/lib/appdb/trace
sudo semanage fcontext -l | grep logrota

A semanage fcontext rule only takes effect after restorecon is run.

For relocated MySQL data directories, list the existing contexts with sudo semanage fcontext -l | grep mysql.

On RHEL 9, add selinux=0 to the kernel command line:

sudo dnf install grubby
sudo grubby --update-kernel ALL --args selinux=0
sudo reboot

To re-enable SELinux:

sudo grubby --update-kernel ALL --remove-args selinux

On RHEL 8, 7 and 6, two methods exist:

References

Mastodon LLIMastodon SocialFosstodon