Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Check the Current Mode

getenforce
sestatus

getenforce prints one word: Enforcing, Permissive, or Disabled. sestatus adds the mode set in the configuration file and the loaded policy. AlmaLinux and Rocky Linux run in enforcing mode after installation. Ubuntu and Debian use AppArmor instead and do not need these steps. The background is in what is SELinux.

Switch to Permissive Until the Next Boot

sudo setenforce 0
getenforce

SELinux now logs what it would have blocked but blocks nothing. sudo setenforce 1 turns enforcing back on. The change does not survive a reboot, and it works only while SELinux is enabled.

Make Permissive Mode Permanent

Open /etc/selinux/config and set the mode, leaving the policy type as it is:

SELINUX=permissive
SELINUXTYPE=targeted

The file is read at boot. Run sudo setenforce 0 as well so the change takes effect now.

Why Permissive Is the Better Step

  • You keep the evidence: permissive mode still records every denial, so you learn exactly which rule an application hit.
  • Files keep their labels: files created while SELinux is disabled get no labels, which makes switching it back on harder.
  • It can be narrow: sudo semanage permissive -a httpd_t makes only the web server domain permissive and leaves the rest enforcing. -d removes it again.

Disabling SELinux Completely

Disabling removes the confinement of every service, and turning it back on later needs a full relabel. Take a snapshot first, and make sure you can reach the server console in your provider's control panel.

On AlmaLinux and Rocky Linux 9 and 10, SELINUX=disabled in the configuration file no longer turns SELinux off completely: the kernel still starts SELinux and only skips loading a policy. Red Hat's documentation gives the kernel argument selinux=0 as the way to disable it fully:

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

To undo it, run sudo grubby --update-kernel ALL --remove-args selinux and follow the steps in the last section before rebooting.

Find the Denial

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

ausearch reads the audit log; audit2why explains each denial and often names the boolean or label that would allow it. On AlmaLinux and Rocky Linux, it comes with the policycoreutils-python-utils package. How the audit system works is covered in auditd.

Fix the Real Cause

  • Wrong label after a move or copy: restore the default labels with restorecon.
  • Content in a non-standard folder: add a label rule with semanage fcontext, then run restorecon.
  • A service needs a network connection: switch on the matching boolean with setsebool -P.
  • A service on another port: label the port with semanage port.
sudo restorecon -Rv /var/www/site
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/site(/.*)?"
sudo restorecon -Rv /srv/site
sudo setsebool -P httpd_can_network_connect on
sudo semanage port -a -t http_port_t -p tcp 8090

If the port already has another type, semanage port reports it as already defined; use -m instead of -a to change it. The semanage tool also comes with policycoreutils-python-utils. Then run sudo setenforce 1 and test the application again.

Switching SELinux Back On

After a period with SELinux disabled, files created in that time have no labels. Going straight to enforcing can stop services and SSH logins. Use this order:

  1. Take a snapshot; see snapshot vs backup.
  2. Remove the selinux=0 kernel argument and set SELINUX=permissive in the configuration file.
  3. Run sudo touch /.autorelabel and reboot. The relabel runs during boot and can take a long time on a large disk; watch it from the console.
  4. Check for denials with ausearch and fix them.
  5. Set SELINUX=enforcing and reboot, or run sudo setenforce 1.
Was this answer helpful?
Loading...