Many administrators disable SELinux at the first sign of trouble — and give up one of the strongest security layers on their server. In reality, SELinux on AlmaLinux is predictable once you understand three ideas: modes, contexts and booleans. This guide explains how SELinux works on AlmaLinux 9, how to read its denials, and how to fix the most common problems the correct way instead of switching the whole system off.
What SELinux Adds on Top of Normal Permissions – SELinux on AlmaLinux 9
Standard Linux permissions (owner, group, mode bits) are discretionary: the owner of a file decides who can access it. SELinux adds mandatory access control enforced by the kernel. Every process and file carries a security label, and a central policy decides which labels may interact. If Apache is compromised, SELinux still confines it to web content files — it cannot read your home directories or mail spool, even running as root. That containment is exactly why SELinux matters.
SELinux Modes on AlmaLinux – SELinux on AlmaLinux 9
SELinux runs in one of three states:
- Enforcing – policy is applied and violations are blocked and logged. This is the default and the mode you should run in production.
- Permissive – violations are logged but allowed. Useful temporarily while diagnosing a problem.
- Disabled – SELinux is off entirely. Avoid this; it also stops file labeling.
Check and temporarily change the current mode:
getenforce sestatus sudo setenforce 0 # permissive until reboot sudo setenforce 1 # back to enforcing
The persistent setting lives in /etc/selinux/config (SELINUX=enforcing). Changing it requires a reboot.
Understanding File Contexts
Most SELinux problems are labeling problems. View contexts with the -Z flag:
ls -Z /var/www/html ps auxZ | grep httpd
A file labeled httpd_sys_content_t can be served by the web server; the same file labeled user_home_t cannot. When you copy files into place from a home directory, they keep the wrong label. Restore the expected labels with:
sudo restorecon -Rv /var/www/html
To make a label permanent for a custom directory — so it survives relabeling — register it in the policy and then apply it:
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?" sudo restorecon -Rv /srv/web

Booleans: The Policy’s Switches
Booleans toggle optional policy behaviors without editing the policy itself. List them with getsebool -a. Two that web administrators meet constantly:
sudo setsebool -P httpd_can_network_connect 1 # let web apps make outbound connections sudo setsebool -P httpd_enable_homedirs 1 # serve content from home dirs
The -P flag makes the change persistent across reboots.
Troubleshooting SELinux on AlmaLinux the Right Way
When something fails mysteriously — a 403 from Apache, a service that will not start — check for denials before touching permissions:
sudo ausearch -m AVC -ts recent sudo journalctl -t setroubleshoot
For a human-readable explanation with suggested fixes, install the troubleshooter tools and inspect the alert:
sudo dnf install -y setroubleshoot-server sudo sealert -a /var/log/audit/audit.log
The report names the exact context or boolean involved. In the rare case a legitimate application needs a permission the stock policy lacks, generate a small local policy module from the audit log:
sudo ausearch -c 'httpd' --raw | audit2allow -M myhttpd sudo semodule -i myhttpd.pp
Treat generated modules as a last resort and review them first — a sloppy module can reopen the hole SELinux was closing.
Common Real-World Scenarios
Website works with setenforce 0 but not enforcing: almost always wrong file labels — run restorecon on the document root. Web application cannot write uploads: the directory needs the read-write type httpd_sys_rw_content_t via semanage fcontext. Service on a non-standard port: register the port, e.g. sudo semanage port -a -t http_port_t -p tcp 8080. Our CSF installation guide shows a complementary firewall layer, and the AlmaLinux wiki collects further SELinux references.
Keeping Contexts Correct Over Time
Labels drift when administrators copy files from home directories, restore backups with tools that ignore extended attributes, or unpack tarballs created elsewhere. Build two habits. First, after moving content into system locations, always run restorecon on the destination. Second, remember the difference between chcon (changes the label now, lost on a full relabel) and semanage fcontext (teaches the policy the label, so it is reapplied forever). Anything you intend to keep belongs in the policy via semanage.
A full filesystem relabel happens automatically when you switch from disabled to enforcing, and can be forced at the next boot with sudo touch /.autorelabel followed by a reboot. On large filesystems this can take a long time — schedule it, and never interrupt it. With these practices, SELinux on AlmaLinux becomes boring infrastructure: present, enforced, and almost never the reason you get paged.
Conclusion
SELinux on AlmaLinux stops being scary once you work with it instead of against it: keep it enforcing, fix labels with restorecon and semanage, use booleans for sanctioned exceptions, and read AVC denials when something breaks. Your server keeps a powerful safety net — and you keep control of it.