The file is 0644, owned by the account the service runs as, and every parent directory is traversable — yet the application still fails with Permission denied. When the Unix permissions are demonstrably correct and the operation is still refused, the usual cause on RHEL-family hosts is SELinux. SELinux runs a second access check, independent of file ownership and mode, based on security labels attached to the process and the file. If the file carries the wrong label, the read is denied and logged as an AVC (Access Vector Cache) denial. The fix is almost never to turn SELinux off; it is to find the mislabelled path and relabel it. This guide walks through confirming SELinux is the cause, reading the denial, and applying the correct remedy.
What you’ll accomplish
- Confirm whether SELinux — not Unix permissions — is denying the operation.
- Read an AVC denial with
ausearchandsealertand identify the process domain and file type. - Fix a mislabelled path with
restorecon, or register a custom path withsemanage fcontext. - Flip a policy boolean with
setsebool -Pwhen the behaviour is governed by one. - Generate a targeted policy module with
audit2allowas a last resort, and know when not to.
Prerequisites
- A RHEL, Rocky Linux, AlmaLinux, or Fedora host with SELinux in
enforcingmode. Confirm withgetenforce. - Root or
sudoaccess. - The tooling packages installed:
setroubleshoot-server(providessealert) andpolicycoreutils-python-utils(providessemanageandaudit2allow). Install withdnf install setroubleshoot-server policycoreutils-python-utils.
Debian and Ubuntu do not use SELinux by default — they ship AppArmor, a different mandatory-access-control system with its own tooling (aa-status, aa-complain). Nothing in this guide applies to an AppArmor host.
How it works
SELinux attaches a security label to every process and every file. The label has four fields: user:role:type:level, for example system_u:object_r:httpd_sys_content_t:s0. For most day-to-day troubleshooting only the third field — the type — matters. This model is called type enforcement: policy declares which process types may act on which file types, and anything not explicitly allowed is denied.
The important consequence is that two independent checks stand between a process and a file. The first is the classic Unix permission check — owner, group, mode — known as discretionary access control (DAC). The second is the SELinux type-enforcement check, known as mandatory access control (MAC). A request must pass both. This is exactly why a file that is 0644 and correctly owned can still be refused: DAC passes, MAC denies. The two-gate model is worth internalising.
Every MAC denial is written to the kernel audit log. On a host running auditd the denials land in /var/log/audit/audit.log; if auditd is not running they surface in the systemd journal (journalctl). Each denial names the source context (the process), the target context (the file), and the operation — which is everything you need to diagnose it.
Step-by-step
1. Confirm SELinux is enforcing. If it is Permissive or Disabled, SELinux is not your problem and you should look elsewhere.
getenforce
Enforcing
2. Find the AVC denial. Query the audit log for recent AVC records. -ts recent limits the search to roughly the last ten minutes; reproduce the failure first, then run this.
ausearch -m AVC -ts recent
time->Sat Jul 19 14:22:07 2026
type=AVC msg=audit(1752935727.812:441): avc: denied { read } for pid=1287
comm="httpd" name="index.html" dev="vda1" ino=131079
scontext=system_u:system_r:httpd_t:s0
tcontext=unconfined_u:object_r:default_t:s0 tclass=file permissive=0
Read that line carefully. comm="httpd" and scontext=...:httpd_t:s0 tell you the web server (domain httpd_t) tried to { read } the file. tcontext=...:default_t:s0 tells you the file carries the type default_t. Apache’s policy does not permit httpd_t to read default_t, so the read is denied. The mismatch between the type Apache expects (httpd_sys_content_t) and the type the file actually has (default_t) is the whole story.
3. Get a plain-language explanation. sealert turns the raw record into a readable summary with a suggested fix. It reads from the setroubleshoot database.
sealert -a /var/log/audit/audit.log
SELinux is preventing httpd from read access on the file index.html.
***** Plugin restorecon (92.2 confidence) suggests ************************
If you want to fix the label.
/srv/www/app/index.html default label should be httpd_sys_content_t.
Then you can run restorecon. The access attempt may have been stopped due
to insufficient permissions to access a parent directory.
Treat the confidence percentage as a hint, not gospel — but a restorecon suggestion at high confidence almost always means a path is simply mislabelled.
4. Compare the actual context to the expected one. ls -Z shows the SELinux label. Compare the offending file to a file the app reads successfully.
ls -Z /srv/www/app/index.html /var/www/html/index.html
unconfined_u:object_r:default_t:s0 /srv/www/app/index.html
system_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html
The working file is httpd_sys_content_t; the broken one is default_t. That confirms the diagnosis.
5. Relabel a path that has a known default. If the path lives under a location the policy already knows about (like /var/www), restorecon resets it to the correct type from policy. -R recurses, -v reports each change.
restorecon -Rv /var/www/html
Relabeled /var/www/html/index.html from unconfined_u:object_r:default_t:s0 to system_u:object_r:httpd_sys_content_t:s0
6. Register a custom path, then relabel it. restorecon only knows the default for a path if policy has a rule for it. A non-standard web root like /srv/www has no such rule, so you must add one with semanage fcontext first, then apply it. The regex (/.*)? matches the directory and everything beneath it.
semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?"
restorecon -Rv /srv/www
Relabeled /srv/www/app/index.html from unconfined_u:object_r:default_t:s0 to system_u:object_r:httpd_sys_content_t:s0
The semanage fcontext -a rule is written to local policy and survives a full filesystem relabel, which is what makes it the correct permanent fix.
7. Flip a boolean when the behaviour is policy-gated. Some capabilities are toggles rather than labels — Apache making outbound network connections, for instance, is governed by httpd_can_network_connect. When the denial is about an action the policy can permit conditionally, set the boolean. -P makes it persist across reboots.
setsebool -P httpd_can_network_connect on
(no output on success)
List the booleans relevant to a domain with getsebool -a | grep httpd to see what is available before reaching for a policy module.
8. Generate a policy module — last resort. If, and only if, no label fix or boolean applies and you understand exactly what the denial represents, audit2allow can build a module that permits it. Read the human-readable rule first with -w, then compile a module with -M.
ausearch -m AVC -ts recent | audit2allow -M my_httpd_local
semodule -i my_httpd_local.pp
******************** IMPORTANT ***********************
To make this policy package active, execute:
semodule -i my_httpd_local.pp
Inspect the generated my_httpd_local.te before installing it. If it grants something broad — for instance httpd_t writing to default_t across the filesystem — stop and relabel instead.
Validate it works
Re-run the failing operation. Restart or re-request whatever failed — reload the page, restart the service, re-run the job.
Confirm no new denial appears. Reproduce the action, then check for fresh AVCs. An empty result is the goal.
ausearch -m AVC -ts recent
<no matches>
Confirm the label is now correct. The file should carry the type the domain expects.
ls -Z /srv/www/app/index.html
system_u:object_r:httpd_sys_content_t:s0 /srv/www/app/index.html
Troubleshooting
The operation still fails but no AVC appears. Some denials are suppressed by dontaudit rules to keep the logs quiet. Temporarily disable that suppression, reproduce, then re-enable it.
semodule -DB
ausearch -m AVC -ts recent
semodule -B
-DB disables dontaudit and rebuilds policy; -B restores the default. Leaving suppression off floods the log, so turn it back on once you have the denial.
A custom directory under /srv keeps getting denied after you chmod it. Permissions are not the issue — the type is. Add an fcontext rule for the path and relabel it (step 6). Changing mode bits will never change the SELinux type.
The denial is about a port, not a file. A service told to listen on a non-standard port (say Apache on 8443) is denied by a port label, not a file context. semanage fcontext does nothing here; use semanage port.
semanage port -a -t http_port_t -p tcp 8443
A chcon fix disappeared after a relabel. chcon changes a label on disk but does not record it in policy, so the next restorecon or a filesystem autorelabel (a .autorelabel at the root) reverts it. Use chcon only for a quick test; make the change permanent with semanage fcontext plus restorecon.
audit2allow “fixed” it but the box feels less safe now. It probably is. Piping a batch of denials into audit2allow and installing the result allows every operation that failed, including ones caused by a genuine mislabel that should have been relabelled. Always read the .te file, and prefer a targeted label or boolean fix over a generated module.
When this is the wrong approach
Running setenforce 0 or setting SELINUX=disabled makes the symptom vanish, and that is exactly the trap. Permissive mode still logs denials but enforces nothing, and disabling SELinux removes a working layer of protection from an internet-facing host to save a five-minute relabel. Neither is a fix; both are a downgrade you will have to justify later. The related layers are worth keeping healthy too — pair a correctly labelled host with Fail2ban for SSH and a tight UFW firewall baseline on the hosts where those apply.
There are cases where a policy adjustment genuinely is the right move: an application that legitimately needs a new port, a new file type, or a capability the shipped policy never anticipated. That is what semanage port, semanage fcontext, and a reviewed audit2allow module exist for — a deliberate, minimal grant, not a blanket disable. Containers are a separate world again: they carry their own SELinux labels (commonly container_t and container_file_t), and mounting a host path into a container without the right label is its own class of denial with its own :z/:Z mount-flag remedy. For the authoritative reference, see the man pages for restorecon(8) and semanage-fcontext(8).
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.