SELinux Troubleshooting on Rocky Linux 10: audit2why, semanage & Common Denials

audit2why, audit2allow, semanage fcontext, restorecon, setsebool, ausearch avc, sealert

“Permission denied” while the file permissions look perfect is the classic sign of SELinux. The most common reaction is to run setenforce 0 or edit /etc/selinux/config to disable it. That fixes the symptom and removes a security layer that limits what a compromised web server, database, or container can do.

SELinux is not hard to troubleshoot once you know the four-step routine: find the denial, understand it, choose the narrowest fix, and verify. This guide teaches that routine on Rocky Linux 10 (it applies equally to AlmaLinux and RHEL), with a table of the denials you will actually meet.

Table of Contents

  1. How SELinux decides
  2. Check the current state
  3. The troubleshooting workflow
  4. Install the tools
  5. Step 1: Find the denial
  6. Step 2: Understand it with audit2why and sealert
  7. Step 3: Choose the narrowest fix
  8. Booleans worth knowing
  9. Fix file labels: semanage fcontext and restorecon
  10. Fix ports
  11. Containers and bind mounts
  12. Custom policy modules with audit2allow
  13. Permissive domains and temporary testing
  14. Common denials cheat sheet
  15. Troubleshooting the troubleshooting
  16. FAQ

1. How SELinux decides

SELinux adds a second access check on top of ordinary Linux permissions. Every process and every file carries a label (a security context). Policy rules say which process types may do what to which file or port types.

  Process: nginx                    Target: /srv/www/index.html
  context: system_u:system_r:httpd_t:s0     context: unconfined_u:object_r:default_t:s0
                      \                         /
                       \                       /
                   +-----v---------------------v-----+
                   | Policy: may httpd_t read        |
                   | default_t files?   --> NO       |
                   +-----------------+---------------+
                                     |
              Classic permissions OK, but SELinux logs an AVC denial
              and the read fails: "Permission denied"

A context has four fields: user:role:type:level. In day-to-day troubleshooting you almost always work with the type (for example httpd_t, httpd_sys_content_t, postgresql_db_t).

2. Check the current state

getenforce                  # Enforcing | Permissive | Disabled
sestatus                    # policy name, mode, config file mode
ls -Z /var/www/html         # file contexts
ps -eZ | grep -E 'nginx|httpd|postgres'   # process contexts
id -Z                       # your own context
ModeMeaning
EnforcingPolicy is applied; violations are blocked and logged
PermissiveViolations are logged but not blocked (useful for diagnosis)
DisabledSELinux off; re-enabling later forces a full relabel

Rocky Linux installs in Enforcing mode with the targeted policy. Keep it that way.

3. The troubleshooting workflow

  App fails with "Permission denied" / 502 / won't start
                    |
                    v
   1. Is it SELinux?  -->  ausearch -m AVC,USER_AVC -ts recent
                    |
          no denial |  denial found
                    |       |
    (check classic  |       v
    perms, firewall)|   2. Understand  -->  audit2why / sealert
                            |
                            v
                  3. Pick the narrowest fix
           +--------+---------+----------+-------------+
           |        |         |          |             |
        boolean   relabel   port label  container     custom module
        setsebool  restorecon semanage   :Z / :z       audit2allow
                           semanage fcontext             (last resort)
                            |
                            v
                   4. Verify: repeat the action, check logs, keep Enforcing

4. Install the tools

sudo dnf install -y policycoreutils-python-utils setroubleshoot-server setools-console audit
sudo systemctl enable --now auditd
PackageGives you
policycoreutils-python-utilssemanage, audit2allow, audit2why
setroubleshoot-serversealert: human-readable explanations and suggested fixes
setools-consolesesearch, seinfo to query the policy
auditausearch, aureport, and the audit log itself

5. Step 1: Find the denial

Denials are recorded as AVC (Access Vector Cache) messages in /var/log/audit/audit.log.

# Recent denials
sudo ausearch -m AVC,USER_AVC,SELINUX_ERR -ts recent

# Today only, interpreted into readable form
sudo ausearch -m AVC -ts today -i

# Filter by program
sudo ausearch -m AVC -c nginx -ts today -i

# Summary report
sudo aureport -a --summary

A typical record looks like this:

type=AVC msg=audit(1793600000.123:456): avc:  denied  { name_connect } for  pid=2345 comm="nginx"
dest=5678 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:unreserved_port_t:s0
tclass=tcp_socket permissive=0

How to read it:

FieldMeaning
denied { name_connect }The action attempted (connect to a port)
comm="nginx"The program
scontext=...httpd_tSource: the process type
tcontext=...unreserved_port_tTarget: the port or file type
tclass=tcp_socketObject class
permissive=0Enforced, so the action was really blocked

If you find no AVC, SELinux is probably not the cause. Check classic permissions, the firewall (firewalld guide), and the application log. Also remember that some denials are hidden by dontaudit rules (see section 15).

6. Step 2: Understand it

audit2why translates a denial into a probable cause and fix:

sudo ausearch -m AVC -ts recent | audit2why

Typical output:

type=AVC ... denied { name_connect } ... scontext=...httpd_t tcontext=...unreserved_port_t
	Was caused by:
	One of the following booleans was set incorrectly.
	Description:
	Allow HTTPD scripts and modules to connect to the network using TCP.

	Allow access by executing:
	# setsebool -P httpd_can_network_connect 1

sealert gives a fuller explanation with a confidence rating:

sudo sealert -a /var/log/audit/audit.log | less

Read the “If you believe that …” sections. Choose a fix that matches your intent, not just the one listed first.

7. Step 3: Choose the narrowest fix

Work down this list and stop at the first fix that applies:

OrderFixWhen
1BooleanThe policy already has a switch for this behavior
2Correct the file labelFiles are in a non-standard location or were moved (not copied)
3Label the portA service uses a non-default port
4Container volume flagA container needs host files (:Z or :z)
5Custom policy moduleGenuinely new behavior with no boolean; review carefully
NeverDisable SELinuxNot a fix

8. Booleans worth knowing

Booleans are policy switches you can flip without writing policy.

getsebool -a | grep httpd                 # list web-related booleans
sudo semanage boolean -l | grep httpd_can_network   # with descriptions
sudo setsebool -P httpd_can_network_connect on      # -P makes it persistent
BooleanAllowsTypical symptom
httpd_can_network_connectWeb server processes connect to any TCP portNginx/Apache reverse proxy returns 502
httpd_can_network_connect_dbWeb apps connect to database portsPHP cannot reach MariaDB/PostgreSQL
httpd_can_sendmailWeb apps send mailForm mail fails silently
httpd_use_nfs / httpd_use_cifsServe content from NFS or SMB403 on mounted content
httpd_unified and httpd_enable_homedirsPer-user web directories403 under ~user
use_nfs_home_dirsHome directories on NFSLogin or file access problems
samba_enable_home_dirs, samba_export_all_rwSamba share accessShares appear empty or read-only
container_manage_cgroupContainers managing cgroupsSystemd-in-container issues
selinuxuser_* familyConfined-user capabilitiesRestricted users blocked

Only enable what you need. httpd_can_network_connect is acceptable for a reverse proxy, but you can also label the specific upstream port instead (section 10), which is narrower.

9. Fix file labels

New files inherit the label of their directory. Files moved with mv keep their old label, which is why “I copied the site and it stopped working” happens.

# See what label a path should have according to policy
matchpathcon /var/www/html/index.html
ls -Z /var/www/html/index.html

# Reset to the policy default
sudo restorecon -Rv /var/www/html

For a non-standard location, add a rule first and then apply it:

# Serve content from /srv/mysite instead of /var/www
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/mysite(/.*)?"
sudo restorecon -Rv /srv/mysite

# Writable directory for an app (uploads)
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/srv/mysite/uploads(/.*)?"
sudo restorecon -Rv /srv/mysite/uploads

# PostgreSQL data on a dedicated disk
sudo semanage fcontext -a -t postgresql_db_t "/data/pgsql(/.*)?"
sudo restorecon -Rv /data/pgsql

# Review what you added
sudo semanage fcontext -l -C

chcon changes a label temporarily and is lost on a relabel; use semanage fcontext plus restorecon for anything permanent.

Common file typeUse for
httpd_sys_content_tRead-only web content
httpd_sys_rw_content_tWeb content apps may write
httpd_log_tWeb server logs
postgresql_db_tPostgreSQL data directories
mysqld_db_tMariaDB/MySQL data directories
var_log_t, var_lib_tGeneric log and state files
bin_tExecutables a service may run
container_file_tFiles containers may use

Full relabel after major label damage (slow; plan downtime):

sudo touch /.autorelabel && sudo reboot

10. Fix ports

Services may only bind or connect to ports labeled for them.

# What is allowed for a type
sudo semanage port -l | grep -E 'http_port_t|ssh_port_t|postgresql_port_t'

# Allow nginx to listen on 8088
sudo semanage port -a -t http_port_t -p tcp 8088

# Allow SSH on 2222
sudo semanage port -a -t ssh_port_t -p tcp 2222

# PostgreSQL on 5433
sudo semanage port -a -t postgresql_port_t -p tcp 5433

# If the port is already defined for another type, modify instead
sudo semanage port -m -t http_port_t -p tcp 8088

Also open the port in the firewall (firewalld guide); SELinux and firewalld are independent layers.

11. Containers and bind mounts

Container processes run as container_t and may only touch files labeled for containers. Bind-mounting a host directory needs a relabel flag:

volumes:
  - ./redis/redis.conf:/usr/local/etc/redis/redis.conf:ro,Z   # private to this container
  - ./shared-data:/data:z                                      # shared among containers
FlagMeaning
:ZRelabel with a private label for this container only
:zRelabel with a shared label usable by several containers

Warning: never use :Z on system directories such as /home, /etc, or /var; it would relabel them. Mount specific project directories. More in Docker on Rocky Linux 10 with SELinux and the Docker Redis Compose guide.

12. Custom policy modules with audit2allow

When no boolean, label, or port fix applies and you have confirmed the behavior is legitimate, generate a small module:

# 1. Reproduce the failure so the denial is in the log
# 2. Build a module from recent denials of one program
sudo ausearch -m AVC -c myapp -ts recent | audit2allow -M myapp_local

# 3. READ the generated rules before installing
cat myapp_local.te

# 4. Install
sudo semodule -i myapp_local.pp
sudo semodule -l | grep myapp_local

# Remove it later if you fix the root cause
sudo semodule -r myapp_local

Review rules carefully. If the .te file allows something broad (for example, a web server process writing to arbitrary types or unconfined access), find a narrower solution. audit2allow happily allows anything it saw denied, including attacker behavior.

13. Permissive domains and temporary testing

To test whether SELinux is the cause without loosening the whole system:

# Make only one domain permissive (log, but allow)
sudo semanage permissive -a httpd_t
# ... reproduce the problem and collect ALL denials in one pass ...
sudo ausearch -m AVC -ts recent | audit2why
# Put it back
sudo semanage permissive -d httpd_t

Permissive mode shows every denial the action would trigger, while enforcing mode stops at the first one. That helps when several separate fixes are needed.

For a quick whole-system test, sudo setenforce 0 switches to permissive until reboot, and sudo setenforce 1 restores enforcing. Use this only for diagnosis on a short timeline, then fix the real cause.

14. Common denials cheat sheet

SymptomLikely causeFix
Nginx/Apache reverse proxy returns 502httpd_t cannot connect to the upstream portsetsebool -P httpd_can_network_connect on, or label the upstream port
403 on files you copied to a new web rootWrong type (default_t, user_home_t)semanage fcontext -a -t httpd_sys_content_t ... then restorecon -Rv
Web app cannot write uploadsContent type is read-onlyLabel upload dir httpd_sys_rw_content_t
Service will not start on a custom portPort not labeledsemanage port -a -t <type> -p tcp <port>
PostgreSQL fails after moving the data directoryWrong label on new locationsemanage fcontext -a -t postgresql_db_t ...; restorecon -Rv
Container: “permission denied” on a bind mountHost files lack container labelAdd :Z (or :z) to the volume
systemd service cannot run a script in /opt or /homeScript labeled usr_t or user_home_tLabel the script bin_t or move it to a standard path; restorecon
Cron job fails, works manuallyScript label or the crond_t domainCheck with ausearch; fix label
SSH key login fails after editing authorized_keys as rootWrong context on ~/.sshrestorecon -Rv ~/.ssh
Samba or NFS share inaccessibleMissing booleanEnable the relevant samba_* or use_nfs_* boolean
FreeRADIUS or another daemon cannot read a custom certificate pathFile label does not matchMove files to the service’s standard directory or label correctly
Backup tool cannot read some filesConfined domainCheck denials; run the tool in an appropriate domain or adjust labels

15. Troubleshooting the troubleshooting

ProblemExplanationWhat to do
No AVC in the log, but SELinux is suspectedSome denials are hidden by dontaudit rulessudo semodule -DB disables dontaudit; reproduce; re-enable with sudo semodule -B
ausearch returns nothingauditd not running or logs rotatedsystemctl status auditd; check /var/log/audit/
sealert not installed or silentsetroubleshoot missingInstall setroubleshoot-server, or use ausearch + audit2why
Fix works until rebootUsed chcon or setsebool without -PUse semanage fcontext + restorecon, and setsebool -P
semanage: command not foundPackage missingdnf install policycoreutils-python-utils
Label looks correct but still deniedParent directory or mount-point label wrongCheck every path component with namei -Z and ls -Zd
NFS or network filesystem labels cannot changeLabeled by mount optionUse a context= mount option or the appropriate boolean
After relabel the system is slow to bootFull relabel in progressWait; it is a one-time operation
Application works in permissive, fails in enforcing, no denial foundHidden denialUse semodule -DB, test, then semodule -B

Namei is underused: namei -Z /srv/mysite/uploads/file.txt prints the context of every directory along the path, showing the one label that blocks traversal.

16. FAQ

Is it safe to disable SELinux on a server?
It removes a protection layer that limits the damage from a compromised service. Almost every problem has a narrow fix, so disabling is rarely justified, and re-enabling later requires a full filesystem relabel.

What is the difference between setenforce 0 and disabling in the config file?
setenforce 0 switches to permissive until the next reboot. Setting SELINUX=disabled in /etc/selinux/config turns it off at boot and takes effect after a reboot.

What does restorecon do?
It resets files to the label that policy says they should have, based on semanage fcontext rules.

When should I use audit2allow?
As a last resort, after you have ruled out booleans, labels, and port labels, and after you have read the rules it generates.

Does SELinux replace the firewall?
No. SELinux controls what processes may do. firewalld controls network traffic. A request can be blocked by either layer, so check both.

How do I know which boolean I need?
audit2why and sealert usually name it. You can also search: sudo semanage boolean -l | grep -i <keyword>.

Does this guide apply to AlmaLinux and RHEL?
Yes. The tools, policy, and commands are the same across the RHEL family.

(Visited 12 times, 1 visits today)

You may also like