Fedora SELinux Management & Context Resolution Cheat Sheet¶
Security-Enhanced Linux (SELinux) is a mandatory access control (MAC) security mechanism provided in the kernel. In Fedora and RHEL-based systems, it is enabled by default and operates primarily by labeling files, processes, and ports with a "context" and enforcing rules on how they interact.
Tested on: Fedora 44 KDE Plasma
1. State & Mode Management¶
Check Full SELinux Status:
Check Current Mode:
Set Mode Temporarily (Enforcing):
Set Mode Temporarily (Permissive):
Permanent Mode Configuration: To permanently change the SELinux mode across reboots, you must edit
/etc/selinux/configand change theSELINUX=directive toenforcing,permissive, ordisabled. (Note: Disabling SELinux entirely is highly discouraged).
2. Viewing Security Contexts (Labels)¶
SELinux contexts are typically displayed in the format: user:role:type:level. The type (e.g., httpd_sys_content_t) is the most important part for standard administration.
View File/Directory Contexts:
View Process Contexts:
View Current User Context:
View Port Contexts:
3. Modifying File Contexts¶
Temporary Changes (chcon)¶
Changes made with chcon will be lost the next time the filesystem is relabeled or restorecon is run.
# Temporarily change the type context of a file
sudo chcon -t httpd_sys_content_t /var/www/html/index.html
Permanent Changes (semanage fcontext & restorecon)¶
To permanently define the default context for a directory and ensure the labels survive reboots and relabeling operations.
# 1. Add a rule to the SELinux policy (Regex specifies the directory and all contents)
sudo semanage fcontext -a -t virt_image_t "/mnt/storage/vms(/.*)?"
# 2. Traverse the directory recursively (-R) and apply the newly defined policy (-v for verbose)
sudo restorecon -Rv /mnt/storage/vms
4. SELinux Booleans¶
Booleans are on/off switches that allow you to modify SELinux policy behavior on the fly without having to write custom policy modules.
List All Booleans & Current State:
List Booleans with Descriptions:
Change a Boolean Temporarily:
Change a Boolean Permanently (-P):
5. Troubleshooting & Audit Logs¶
When a daemon or process is denied access, SELinux logs an AVC (Access Vector Cache) denial.
Finding the Denials¶
# View recent SELinux denials in the audit log
sudo ausearch -m avc -ts recent
# If auditd is not running, denials may be in the system journal
sudo journalctl -t setroubleshoot
Analyzing Denials with sealert¶
sealert translates cryptic audit logs into human-readable explanations and suggests fixes.
Generating Custom Policy Modules (audit2allow)¶
If you are running a custom application and need to allow it to bypass a restriction, you can compile a custom policy module based on the denial logs.
# 1. Pipe the denial log into audit2allow to create a custom policy module named 'mypol'
grep my_custom_app /var/log/audit/audit.log | audit2allow -M mypol
# 2. Install the newly generated policy package (.pp file)
sudo semodule -i mypol.pp
6. Common Context Types Reference¶
httpd_sys_content_t: Standard read-only web server content.httpd_sys_rw_content_t: Web server content that the web daemon needs to write to (e.g., upload directories).public_content_t: Read-only files shared via FTP, Samba, or Apache.public_content_rw_t: Read/write files shared via FTP, Samba, or Apache.virt_image_t: Disk image files used by KVM/libvirt.container_file_t: Files and directories mapped into Podman/Docker containers.