SELinux Documentation for RHEL
SELinux (Security-Enhanced Linux) is a mandatory access control (MAC) system integrated into RHEL. It provides fine-grained security policies for processes, users, and files, beyond standard Linux permissions.
1. SELinux Modes
SELinux can operate in three modes:
Mode | Description |
---|---|
Enforcing | SELinux enforces policies. Access denials are logged and blocked. |
Permissive | SELinux logs policy violations but does not block access. Useful for testing. |
Disabled | SELinux is completely turned off. |
Check current mode:
Check detailed SELinux status:
Change mode temporarily (no reboot needed):
Change mode permanently:
Edit /etc/selinux/config
:
2. SELinux Contexts
Every file, directory, and process has an SELinux context, defined as:
- user: SELinux user identity
- role: Role associated with user
- type (domain): Primary control element; most access decisions use type
- level: MLS/MCS security level (mostly used in multi-level security systems)
View file context:
View process context:
3. Common SELinux File Types
SELinux relies heavily on types to control access. Some common types:
Type | Description |
---|---|
httpd_sys_content_t |
Web server static content |
httpd_sys_rw_content_t |
Web server writable content |
var_log_t |
Log files |
tmp_t |
Temporary files |
user_home_t |
User home directories |
Change a file’s context:
Restore default context:
Set context permanently usingsemanage fcontext
:
sudo semanage fcontext -a -t <type> '/path/to/file_or_dir(/.*)?'
sudo restorecon -Rv /path/to/file_or_dir
-a
: Add a new file context mapping-t <type>
: Specify the SELinux type'/path/to/file_or_dir(/.*)?'
: Regular expression for the path and its contents
View all custom file contexts:
4. Booleans
SELinux has booleans to allow specific actions without changing policies.
List all SELinux booleans:
Enable or disable a boolean temporarily:
Make boolean change permanent:
5. Troubleshooting
a) Audit Logs
SELinux denials are logged in /var/log/audit/audit.log
. Use ausearch
or audit2allow
to analyze:
b) Troubleshoot Denials
- Temporarily set permissive mode to test:
- Review the denial logs.
- Create a policy module if needed:
6. Best Practices
- Keep SELinux in Enforcing mode on production servers.
- Use
restorecon
rather thanchcon
for persistent changes. - Use
semanage fcontext
for permanent file context changes. - Use booleans instead of modifying policies whenever possible.
- Regularly monitor
/var/log/audit/audit.log
for unexpected denials. - Test new applications in Permissive mode before moving to Enforcing.
7. Useful SELinux Commands
Command | Description |
---|---|
getenforce |
Show current mode (Enforcing/Permissive/Disabled) |
sestatus |
Detailed SELinux status |
setenforce 0 |
Temporarily set permissive/enforcing mode |
ls -Z |
Show file contexts |
ps -eZ |
Show process contexts |
restorecon -Rv <path> |
Restore default context |
chcon -t <type> <file> |
Change file context temporarily |
semanage fcontext -a -t <type> <file> |
Add a permanent file context mapping |
semanage fcontext -l |
List all custom file contexts |
getsebool -a |
List all SELinux booleans |
setsebool -P <boolean> on |
Permanently enable/disable a boolean |
audit2allow |
Generate SELinux policy from audit logs |