Understanding SELinux Basics on AlmaLinux/Rocky Linux

SELinux (Security-Enhanced Linux) is a mandatory access control system enabled by default on AlmaLinux and Rocky Linux — a significant difference from Ubuntu/Debian. Rather than disabling it (a common but security-reducing shortcut), this guide covers working with it correctly.

Why SELinux Exists

Standard Linux permissions (owner/group/other) control access based on identity. SELinux adds a second, independent layer that restricts what a process can do based on defined security policy — so even a process running as root is still constrained by SELinux policy, providing defense in depth against exploited vulnerabilities.

Checking SELinux Status

getenforce

Possible values:

ModeMeaning
EnforcingSELinux actively blocks policy violations (default, recommended)
PermissiveViolations are logged but not blocked (useful for testing/debugging)
DisabledSELinux is completely off (not recommended)

Temporarily Switching to Permissive Mode (For Testing)

sudo setenforce 0

This is temporary and reverts on reboot — useful for confirming whether SELinux is actually the cause of an issue you're troubleshooting, without a permanent change.

Permanently Changing SELinux Mode

sudo nano /etc/selinux/config
SELINUX=enforcing

Understanding SELinux Contexts

ls -Z /var/www/html

Every file has an SELinux context (a label) in addition to standard permissions. Web content typically needs the httpd_sys_content_t type to be readable by Nginx/Apache.

Fixing Incorrect File Contexts

sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/mysite(/.*)?"
sudo restorecon -Rv /var/www/mysite

semanage fcontext defines the rule persistently; restorecon actually applies it to existing files.

Understanding SELinux Booleans

Booleans are toggleable policy settings for common scenarios without writing custom policy:

getsebool -a | grep httpd
sudo setsebool -P httpd_can_network_connect 1

-P makes the boolean change persistent across reboots.

Diagnosing SELinux Denials

sudo ausearch -m avc -ts recent

Shows recent SELinux denials (AVC = Access Vector Cache) — the first place to check when something "should" work based on standard permissions but doesn't.

Using sealert for Human-Readable Explanations

sudo dnf install setroubleshoot-server -y
sudo sealert -a /var/log/audit/audit.log

Translates cryptic AVC denials into plain-language explanations and suggested fixes.

Common Contexts for Web Hosting

Context TypeUsed For
httpd_sys_content_tStatic web content (read-only)
httpd_sys_rw_content_tWeb content the web server needs to write to (uploads)
httpd_sys_script_exec_tCGI scripts

Setting a Writable Directory for Uploads

sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/mysite/uploads(/.*)?"
sudo restorecon -Rv /var/www/mysite/uploads

Why Not Just Disable SELinux?

Disabling SELinux removes a meaningful security layer that specifically limits damage from compromised applications — the correct approach is understanding and correctly configuring contexts/booleans for your legitimate use case, not disabling the entire subsystem.

Common Errors

"Permission denied" despite correct chmod/chown — almost always an SELinux context mismatch; check with ausearch -m avc -ts recent.

Continue Reading

Browse more articles in AlmaLinux & Rocky Linux.

  • selinux, selinux basics, almalinux selinux, rhel security
  • 0 Utilisateurs l'ont trouvée utile
Cette réponse était-elle pertinente?

Articles connexes

AlmaLinux vs Rocky Linux vs CentOS: What Happened to CentOS?

If you're choosing a RHEL-compatible Linux distribution for your VPS, understanding the CentOS...

How to Get Started with AlmaLinux/Rocky Linux on a VPS

This guide covers the essential first steps after deploying a new AlmaLinux or Rocky Linux VPS...

How to Use dnf: The Package Manager for AlmaLinux & Rocky Linux

dnf (Dandified YUM) is the package manager for AlmaLinux, Rocky Linux, and other RHEL-family...

How to Configure firewalld on AlmaLinux/Rocky Linux

firewalld is the default firewall management tool on AlmaLinux and Rocky Linux, using a...

How to Install Nginx on AlmaLinux/Rocky Linux

This guide covers installing and configuring Nginx on AlmaLinux or Rocky Linux, including the...