$ run as a normal user# run as root (or with sudo)
The quick answer
getenforce
Enforcing
ausearch -m AVC,USER_AVC -ts recent
sealert -a /var/log/audit/audit.log | head -40
restorecon -Rv /srv/wwwsealert (package setroubleshoot-server) explains each denial in plain words and suggests the fix.
Step by step
- Is it SELinux? Look for denials around the time of the error:
ausearch -m AVC -ts recent
type=AVC msg=audit(1795604711.123:812): avc: denied { read } for pid=2210 comm="nginx" name="index.html" scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=file permissive=0Read it as: nginx (httpd_t) may not read a file labelled **user_home_t**.
- Choose the fix by the cause:
| Cause | Typical sign | Fix |
|---|---|---|
| Wrong file label | tcontext=…user_home_t / default_t | semanage fcontext + restorecon |
| Service on a non-standard port | name_bind / name_connect denied | semanage port -a -t TYPE -p tcp PORT |
| A feature is off by default | Web server connects to a DB/network | setsebool -P BOOLEAN on |
- File labels — tell SELinux what a folder is, then apply it:
semanage fcontext -a -t httpd_sys_content_t '/srv/www(/.*)?'
restorecon -Rv /srv/www
ls -Zd /srv/wwwFiles moved (mv) from home keep their old label — that is the classic case; restorecon fixes it.
- Ports:
- Booleans:
getsebool -a | grep httpd_can_network
setsebool -P httpd_can_network_connect on
setsebool -P httpd_can_network_connect_db on-P keeps it after reboot.
- Test without turning SELinux off — make only that one domain permissive for a moment, never the whole system:
- Last resort: a custom module (only after the fixes above, and after reading what it allows):
ausearch -m AVC -ts recent | audit2allow -M local_nginx
semodule -i local_nginx.ppCheck that it worked
The app works, ausearch -m AVC -ts recent shows no new denials, and getenforce still says Enforcing.
Common problems
| Problem | Cause | Fix |
|---|---|---|
ausearch finds nothing, still denied | dontaudit rules hide some denials. | semodule -DB (show all), test, then semodule -B again. |
Label is right after chcon, wrong after relabel | chcon is temporary. | Use semanage fcontext + restorecon. |
| Service can not bind its port | Port not in the SELinux port type. | semanage port -a … (or -m to change an existing one). |
| 502 from Nginx/Apache to an app | httpd_can_network_connect off. | setsebool -P httpd_can_network_connect on. |
| Container volume denied | Missing :Z/:z label on the volume (Podman/Docker). | Mount with :Z (private) or :z (shared). |
Someone set SELINUX=disabled | Turned off "to make it work". | Turn it on (relabel at boot: touch /.autorelabel), then fix the real causes. |
Go further
- Command pages: ausearch, sealert, semanage, restorecon, getenforce.
- Next how-tos: Change file permissions and owner, Set up Nginx as a reverse proxy, Harden the SSH server.
- Learn it: RHCSA — Contexts, RHCSA — restorecon, RHCSA — Ports, RHCSA — Booleans, RHCSA — Denials, RH134 — SELinux.
- Practise: Lab: make a web server work with SELinux, Lab: find and fix SELinux denials.
1Why is setenforce 0 not a fix?
It turns off SELinux protection for the whole system; fix the label, port or boolean instead.
2Files moved from a home folder into /srv/www are denied. What fixes it?
restorecon -Rv /srv/www (after semanage fcontext sets the right type for the path).