Beyond basic SELinux troubleshooting, deploying a custom application sometimes requires creating tailored SELinux policy adjustments — this guide covers a practical approach for common scenarios.
Building on Basic SELinux Understanding
See Understanding SELinux Basics on AlmaLinux/Rocky Linux for foundational concepts (contexts, booleans, modes) before this more advanced guide — this article assumes familiarity with those basics.
Step 1 — Identify SELinux Denials
sudo ausearch -m avc -ts recent
Review recent Access Vector Cache (AVC) denials — these show exactly what SELinux blocked and why, the essential starting point for any custom policy work.
Step 2 — Use audit2why for Human-Readable Explanation
sudo ausearch -m avc -ts recent | audit2why
Translates raw denial logs into a more understandable explanation of what was blocked and typical remediation approaches.
Step 3 — Check for an Existing Boolean First
getsebool -a | grep httpd
Before creating custom policy, check whether an existing SELinux boolean already covers your specific need — booleans are simpler, well-tested toggles for common scenarios, preferable to custom policy when applicable.
Step 4 — Setting the Correct File Context
sudo semanage fcontext -a -t httpd_sys_content_t "/opt/myapp/public(/.*)?"
sudo restorecon -Rv /opt/myapp/public
For non-standard file locations, explicitly defining the correct context is often sufficient without needing custom policy modules at all.
Step 5 — Generating a Custom Policy Module from Denials (When Needed)
sudo ausearch -m avc -ts recent | audit2allow -M mycustompolicy
Generates a custom policy module addressing the specific observed denials — review the generated policy carefully before applying, since automatically generated policies can sometimes be broader than genuinely necessary.
Step 6 — Reviewing the Generated Policy Before Applying
cat mycustompolicy.te
Understand exactly what access the generated policy grants — don't apply custom policy blindly; verify it's genuinely scoped to what your application actually needs, not overly permissive.
Step 7 — Applying the Custom Policy Module
sudo semodule -i mycustompolicy.pp
Step 8 — Verifying the Application Now Works
Test your application's actual functionality, and check ausearch again to confirm no further relevant denials are occurring for the specific operations you've addressed.
A Better Practice: Use Booleans and Contexts First, Custom Policy Last
Custom policy modules are more powerful but also more complex to maintain and audit — prefer existing booleans and correct file context assignment wherever they suffice; reserve custom policy generation for scenarios genuinely requiring it.
Documenting Custom Policies
If you create custom SELinux policy, document why it was needed and what it grants — important for future maintainers (including future you) to understand the security implications and whether the policy remains genuinely necessary as the application evolves.
Never Simply Disable SELinux as a Shortcut
See Understanding SELinux Basics on AlmaLinux/Rocky Linux — disabling SELinux entirely removes a valuable security layer; the additional effort of proper policy configuration is worthwhile for genuinely security-conscious deployments.
Common Errors
Generated custom policy still doesn't resolve the issue — check for additional denials that occurred after your first fix; complex applications sometimes trigger multiple distinct denials requiring iterative ausearch/audit2allow cycles.
Continue Reading
- Understanding SELinux Basics on AlmaLinux/Rocky Linux
- How to Troubleshoot Common firewalld Errors
- How to Get Started with AlmaLinux/Rocky Linux on a VPS
Browse more articles in AlmaLinux & Rocky Linux.