SELinux (Security-Enhanced Linux) is a Linux kernel security module that implements mandatory access control (MAC), restricting processes and users to only the permissions they absolutely need. Unlike traditional Linux permissions, which rely on discretionary access control (DAC), SELinux enforces additional security policies that even root users cannot override.
SELinux operates by labeling all files, processes, and ports with additional context beyond standard Linux permissions. These labels determine what actions are allowed. For example, a web server process labeled as "httpd_t" can only access files labeled "httpd_sys_content_t," preventing unauthorized access even if file permissions allow it.
SELinux consists of several key components:
Many users confuse SELinux with AppArmor, another Linux security module. While both enforce MAC, SELinux uses a more complex policy language and is the default on Red Hat-based distributions. AppArmor, common on Debian-based systems, uses profiles that are easier to manage but less granular.
SELinux is particularly useful in multi-user environments where minimizing damage from compromised accounts is critical. For instance, if an attacker gains access to a user account, SELinux policies can prevent them from accessing sensitive system files. However, SELinux can complicate troubleshooting due to its strict policies. Many administrators disable it temporarily during development but re-enable it for production environments.
For those comparing Linux to Windows, understanding SELinux highlights how Linux handles security differently from Windows' discretionary access control model. Learn more about Linux vs Windows differences.
To check if SELinux is enabled on a Linux system, run:
# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
This output shows SELinux is active in enforcing mode, meaning it actively blocks unauthorized actions. To temporarily set SELinux to permissive mode (logging violations without enforcing them), use:
# setenforce 0
This is useful for debugging when SELinux policies interfere with legitimate operations.
| Policy Type | Description | Use Case |
|---|---|---|
| Targeted | Only specific daemons run in SELinux context; most processes run unconfined. | Default for many systems; balances security and usability. |
| Strict | All processes run in SELinux context; highly restrictive. | High-security environments where minimal access is critical. |
| Minimum | Similar to targeted but with fewer exceptions. | Systems needing moderate security without excessive restrictions. |
Most production systems use the targeted policy, which provides a good balance between security and functionality. The strict policy is rarely used due to its complexity, while the minimum policy offers a middle ground.
Firsthand experience: When setting up a web server, I initially disabled SELinux due to permission errors. After researching, I realized the issue was a missing file context. Relabeling the files with restorecon resolved the problem without compromising security.