SELinux Explained: Enforcing, Permissive and Why It Keeps Blocking Your Server


Every Linux beginner meets SELinux the same way: something that should work doesn’t, a forum post says “just disable SELinux,” and the problem vanishes. Six months later, a compromised web server has full run of the system — because the one layer that would have contained it was switched off. SELinux is not the enemy. It’s the last line of defense, and understanding it takes about ten minutes.

What SELinux Actually Does

Normal Linux permissions answer one question: who can access a file. SELinux answers a different one: what is this process allowed to do at all? It’s called mandatory access control (MAC). Even root can’t override it. If your web server process is only allowed to read web files, then even if an attacker hijacks that process, SELinux stops it from reading /etc/shadow, writing to system directories, or opening outbound connections it was never meant to make.

The Three Modes

SELinux runs in one of three modes:

  • Enforcing — rules are applied. Violations are blocked and logged. This is the secure default on RHEL, Fedora, CentOS and AlmaLinux.
  • Permissive — violations are only logged, never blocked. Useful for testing a new policy, dangerous as a permanent setting.
  • Disabled — SELinux is completely off. The classic “fix” that removes your safety net.

Check your current mode:

getenforce        # prints Enforcing, Permissive, or Disabled
sestatus          # detailed status, including the loaded policy

Switch temporarily (until reboot) with sudo setenforce 0 for permissive or sudo setenforce 1 for enforcing. Permanent changes go in /etc/selinux/config.

Contexts: The Labels That Matter

SELinux labels every file and process with a context — user:role:type:level. The type is what matters most. A web server running as httpd_t can read files labeled httpd_sys_content_t but not files labeled user_home_t. That’s why a website placed in a home directory fails with a mysterious “permission denied” even though chmod looks fine — the file permissions are right, but the SELinux label is wrong.

See the labels:

ls -Z /var/www/html/        # -Z shows SELinux contexts
ps -Z | grep httpd          # contexts of running processes

Fixing a Block Without Disabling SELinux

When SELinux blocks something, it logs the denial. Use those logs instead of turning it off:

# See recent denials in plain English
sudo ausearch -m avc -ts recent

# Get the exact fix command for a denial
sudo sealert -a /var/log/audit/audit.log

The most common fixes:

# Fix a wrong label (e.g. website files in the wrong place)
sudo restorecon -Rv /var/www/html/

# Allow a boolean for a known need (e.g. web server connecting to a database)
sudo setsebool -P httpd_can_network_connect_db on

# Label a custom directory correctly
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?"
sudo restorecon -Rv /srv/www

Why You Should Keep It Enforcing

Disabling SELinux is the security equivalent of removing your seatbelt because it wrinkles your shirt. Real-world attacks regularly exploit a vulnerable service — and SELinux is what stops the attacker from turning one compromised process into a full system takeover. The CVE databases are full of incidents where SELinux contained the damage. Learn the three commands above (getenforce, ausearch, restorecon) and you’ll almost never need to disable it again.

If your distribution doesn’t ship SELinux, the same idea exists elsewhere — AppArmor on Ubuntu and Debian works on a similar principle. Either way, the lesson is the same: mandatory access control is a feature, not a bug.

Comments

Popular Posts