Security-Enhanced Linux (SELinux) Mandatory Access Controls
SELinux enforces Mandatory Access Control (MAC) via kernel-level policy rules. Even if standard UNIX permissions (chmod 777) grant access, SELinux blocks any operation that violates security policy.
1. SELinux Operating Modes
- Enforcing: Policy is strictly enforced; unauthorized actions are blocked and logged. (Required for RHCSA!)
- Permissive: Actions are allowed, but violations are logged (ideal for debugging).
- Disabled: SELinux is completely inactive.
# Check current enforcement state
getenforce
sestatus
# Temporarily switch mode (does NOT persist reboot)
setenforce 1 # Enforcing
setenforce 0 # Permissive
# Persistently set mode across reboots
# Edit /etc/selinux/config -> SELINUX=enforcing
2. File Context Management (semanage & restorecon)
Every file has a context formatted as user:role:type:level. The type (e.g., httpd_sys_content_t) is what the target daemon expects.
# View security context of files
ls -lZ /var/www/html/
# View process security context
ps auxZ | grep httpd
High-Yield Scenario: Custom Service Directories
When moving Apache DocumentRoot to /srv/website:
# 1. Inspect default reference context
ls -ldZ /var/www/html
# Output: system_u:object_r:httpd_sys_content_t:s0
# 2. Add rule to policy database using semanage
semanage fcontext -a -t httpd_sys_content_t "/srv/website(/.*)?"
# 3. Apply the context recursively from the database to disk
restorecon -Rv /srv/website
[!WARNING] Never use
chconto satisfy RHCSA exam requirements!chconchanges contexts only in memory/metadata, which are reverted to default uponrestoreconor filesystem relabeling during reboot. Always usesemanage fcontextfollowed byrestorecon.
3. SELinux Booleans (getsebool & setsebool)
Booleans act as on/off switches that modify SELinux policy behavior dynamically.
# List all booleans matching a service
getsebool -a | grep httpd
# View boolean description and default state
semanage boolean -l | grep httpd_can_network_connect
# Persistently enable a boolean across reboots with -P
setsebool -P httpd_can_network_connect on
setsebool -P ftpd_anon_write on
4. Troubleshooting SELinux Denials
# Inspect recent AVC denials in audit log
grep "AVC" /var/log/audit/audit.log
# Translate cryptic audit logs into actionable remedies
audit2why < /var/log/audit/audit.log
sealert -a /var/log/audit/audit.log
Cross-references:
- See Rootless Container Management with Podman for SELinux
:Zand:zcontainer volume labels. - See firewalld Network Packet Filtering and Zones for firewall security integration.