SELinux Overview — Mandatory Access Control in Linux¶
SELinux (Security-Enhanced Linux) is a Linux security framework that provides Mandatory Access Control (MAC). Unlike traditional Linux permissions, which rely on file ownership and user permissions, SELinux applies security policies that determine what processes are allowed to access specific files, directories, ports, and system resources. Even if a process is running as the root user, SELinux can still restrict its actions according to the defined security policy. Every Linux administrator, DevOps engineer, Cloud Architect, Platform Engineer, and Site Reliability Engineer (SRE) should understand SELinux to secure enterprise Linux systems.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand SELinux fundamentals
- Learn Mandatory Access Control (MAC)
- Understand SELinux modes
- View and interpret security contexts
- Manage SELinux policies
- Use common SELinux commands
- Troubleshoot SELinux issues
- Apply SELinux best practices
Prerequisites¶
Complete:
- Modules 1–10
- Module 11 Lessons 1–3
Why Learn SELinux?¶
Imagine a web server running as the root user.
Without SELinux:
With SELinux:
SELinux reduces the impact of successful attacks.
What is SELinux?¶
SELinux stands for:
It is a security framework that enforces Mandatory Access Control (MAC).
Instead of relying only on:
- User ownership
- File permissions
SELinux also evaluates:
- Security contexts
- Security policies
- Process permissions
DAC vs MAC¶
Linux normally uses:
Users control access through:
- Owner
- Group
- Permissions
SELinux introduces:
System policies decide whether access is allowed.
Even the root user must follow SELinux policies.
How SELinux Works¶
Every object has a security context.
Example:
Both traditional Linux permissions and SELinux policies must allow access.
SELinux Modes¶
SELinux has three operating modes.
| Mode | Description |
|---|---|
| Enforcing | Policies are enforced |
| Permissive | Violations are logged but not blocked |
| Disabled | SELinux is disabled |
Check SELinux Status¶
Example:
Detailed information:
Example output:
Change SELinux Mode¶
Temporarily switch to permissive mode.
Return to enforcing mode.
These changes are temporary and last until the next reboot.
Persistent Configuration¶
Configuration file:
Example:
Possible values:
Reboot the system after changing the configuration file.
Security Contexts¶
Display SELinux contexts.
Example:
Understanding Security Context¶
Example:
Components:
| Component | Description |
|---|---|
| User | SELinux user |
| Role | SELinux role |
| Type | Security type |
| Level | Security level |
The type is the most commonly used component in policy enforcement.
View Process Contexts¶
Example:
Common SELinux Commands¶
Check status.
Detailed status.
Display file contexts.
Display process contexts.
Restore Default Contexts¶
Restore the default SELinux context.
Useful after moving or copying files.
Change File Context¶
Assign a new context.
Note
chcon changes are temporary and may be lost after a relabel. For permanent changes, use semanage fcontext followed by restorecon.
Managing Ports¶
View allowed ports.
Example:
Viewing SELinux Booleans¶
List booleans.
Example:
Enable a boolean permanently.
Common Commands¶
Check mode.
View status.
View contexts.
Restore contexts.
Change mode.
Real Production Examples¶
Check SELinux status.
Restore web content.
View process contexts.
Enable HTTP network access.
Production Perspective¶
SELinux is widely used in:
- Red Hat Enterprise Linux
- Rocky Linux
- AlmaLinux
- Oracle Linux
- Enterprise web servers
- Database servers
- Kubernetes worker nodes
- Government and regulated environments
Many security standards recommend keeping SELinux enabled in enforcing mode.
Hands-on Lab¶
Task 1¶
Check SELinux mode.
Task 2¶
Display SELinux status.
Task 3¶
View file contexts.
Task 4¶
View process contexts.
Task 5¶
Switch to permissive mode.
Task 6¶
Return to enforcing mode.
Task 7¶
Restore file contexts.
Task 8¶
Display available SELinux booleans.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
getenforce | Display SELinux mode | System verification |
sestatus | Show SELinux status | Troubleshooting |
ls -Z | View file contexts | Security auditing |
ps -eZ | View process contexts | Process analysis |
restorecon | Restore contexts | File recovery |
setsebool | Configure SELinux booleans | Application configuration |
Common SELinux Mistakes¶
| Mistake | Solution |
|---|---|
| Disabling SELinux permanently | Keep it enabled whenever possible |
| Ignoring SELinux logs | Review audit logs to identify denials |
Using chcon for permanent changes | Use semanage fcontext and restorecon |
| Switching to permissive mode permanently | Use only for troubleshooting |
| Changing file permissions instead of fixing SELinux contexts | Restore or assign the correct context |
Production Troubleshooting Scenario¶
Scenario
A web server cannot access newly copied website files.
Linux permissions:
Application still fails.
Investigation:
The files have incorrect SELinux contexts.
Solution:
The correct SELinux labels are restored, and the web server can access the files without changing Linux file permissions.
Best Practices¶
- Keep SELinux in Enforcing mode in production.
- Use Permissive mode only for troubleshooting.
- Never disable SELinux unless absolutely necessary.
- Restore correct file contexts after moving files.
- Review SELinux denials using audit logs.
- Use
semanagefor permanent policy changes. - Test policy changes in a non-production environment.
Common Mistakes¶
❌ Disabling SELinux to bypass configuration issues.
✅ Avoid disabling SELinux to bypass configuration issues; fix the configuration instead.
❌ Ignoring SELinux denial messages.
✅ Always review SELinux denial messages.
❌ Using chmod 777 instead of fixing SELinux contexts.
✅ Prefer fixing SELinux contexts rather than using chmod 777.
❌ Forgetting to restore file contexts after copying application files.
✅ Remember to to restore file contexts after copying application files.
❌ Leaving systems in permissive mode permanently.
✅ Do not leave systems in permissive mode permanently.
Interview Questions¶
Beginner¶
- What is SELinux?
- What is Mandatory Access Control (MAC)?
- What are the three SELinux modes?
- Which command displays the current SELinux mode?
Intermediate¶
- What is the difference between DAC and MAC?
- What is a security context?
- What does
restorecondo? - Why should SELinux remain in enforcing mode?
Architect Level¶
- How would you deploy production web applications with SELinux enabled?
- How would you troubleshoot an application blocked by SELinux?
- Why is SELinux considered an important layer in defense-in-depth security?
Summary¶
In this lesson, you learned:
- SELinux fundamentals
- Mandatory Access Control (MAC)
- SELinux operating modes
- Security contexts
- SELinux commands
- File context management
- SELinux booleans
- Production security best practices
SELinux provides an additional layer of security beyond traditional Linux file permissions by enforcing mandatory access control policies. When configured correctly, it limits what processes can do, helping to contain attacks and protect critical system resources.
Key Takeaways¶
- SELinux implements Mandatory Access Control (MAC).
- Keep SELinux in Enforcing mode for production systems.
- Use
getenforceandsestatusto verify SELinux status. - Restore correct file contexts with
restorecon. - Use
semanagefor permanent SELinux policy changes. - Treat SELinux as an important layer in a defense-in-depth security strategy.
What's Next?¶
AppArmor — Application-Level Security in Linux
You'll explore:
- What AppArmor is
- AppArmor profiles
- Enforce and Complain modes
- Managing profiles
- Common AppArmor commands
- Monitoring policy violations
- Production security best practices
By the end of the lesson, you'll understand how AppArmor confines applications using security profiles and how it complements Linux security by restricting application capabilities.