SELinux Commands: 7 Easy Fixes and Mistakes to Avoid

Most SELinux commands are used in one situation: a service that worked suddenly fails with "Permission denied" or HTTP 403, and the Unix permissions look correct. The old version of this post listed the commands without output and made one wrong claim about setsebool. This rewrite runs them in a real troubleshooting story on Rocky Linux 9.8 in a virtual machine, with SELinux in enforcing mode and the targeted policy (selinux-policy-targeted 38.1.75). Everything below comes from that run. I did not test the module commands (semodule) or a reboot with a changed config file, so those are not covered.

Step 1: check the state with these SELinux commands

# getenforce
Enforcing
# sestatus | head -8
SELinux status:                 enabled
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
# grep -E "^SELINUX=" /etc/selinux/config
SELINUX=enforcing

Of all SELinux commands, getenforce is the simplest: it gives the answer in one word, and sestatus shows the policy and two modes: the current one and the one from the config file. The tools used below come from policycoreutils-python-utils (semanage, audit2allow) and the audit package (ausearch), installed with one dnf command.

Step 2: read contexts with SELinux commands ls -Z and ps -Z

# ls -Z /etc/passwd /etc/shadow
system_u:object_r:passwd_file_t:s0 /etc/passwd
     system_u:object_r:shadow_t:s0 /etc/shadow
# ls -dZ /var/www/html /tmp /root
system_u:object_r:httpd_sys_content_t:s0 /var/www/html
system_u:object_r:tmp_t:s0 /tmp
system_u:object_r:admin_home_t:s0 /root
# ps -eZ | grep -E "sshd|auditd"
system_u:system_r:auditd_t:s0       685 ?        00:00:00 auditd
system_u:system_r:sshd_t:s0-s0:c0.c1023 881 ?    00:00:00 sshd

A context has four parts, user:role:type:level, and the third one, the type, decides most access. The web server runs as httpd_t and may read files of type httpd_sys_content_t, such as everything under /var/www/html. Remember this pair; the rest of the story depends on it.

Step 3: the SELinux commands setenforce and getenforce

# setenforce 0; getenforce; grep -E "^SELINUX=" /etc/selinux/config
Permissive
SELINUX=enforcing
# sestatus | grep -E "Current mode|Mode from config"
Current mode:                   permissive
Mode from config file:          enforcing
# setenforce 1; getenforce
Enforcing
# setenforce 2
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]

These two SELinux commands show that the runtime mode and the config file are separate: setenforce 0 made the system permissive while /etc/selinux/config still said enforcing, so a reboot returns to enforcing. Permissive mode logs denials without blocking, which makes it a diagnosis tool, not a fix. I did not test a reboot, so this post does not show the config change taking effect.

Step 4: SELinux commands for a 403 on a web root outside /var/www

The test server served /srv/web/index.html with Apache httpd and Require all granted. The file is readable by everyone, yet:

# ls -dZ /srv/web; ls -Z /srv/web
unconfined_u:object_r:var_t:s0 /srv/web
unconfined_u:object_r:var_t:s0 index.html
# curl -s -o /dev/null -w "HTTP %{http_code}\n" http://127.0.0.1/
HTTP 403

The files have the type var_t, which httpd_t may not read. The denial is in the audit log, and ausearch finds it:

# ausearch -m avc -ts recent | tail -1
type=AVC msg=audit(1793623847.890:519): avc:  denied  { getattr } for  pid=5222 comm="httpd" path="/srv/web/index.html" dev="vda4" ino=25184468 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfine...
# ausearch -m avc -ts recent | audit2allow
#============= httpd_t ==============
allow httpd_t var_t:file getattr;

audit2allow proposes a policy rule that would allow the access. Do not install such a rule blindly: it would let the web server read every var_t file on the system. Ask first which label the file should have. matchpathcon answers from the policy:

# matchpathcon /srv/web/index.html /var/www/html/index.html
/srv/web/index.html	system_u:object_r:var_t:s0
/var/www/html/index.html	system_u:object_r:httpd_sys_content_t:s0

The quick fix among the SELinux commands, chcon, works at once but does not last:

# chcon -t httpd_sys_content_t /srv/web/index.html; ls -Z /srv/web
unconfined_u:object_r:httpd_sys_content_t:s0 index.html
# curl -s -w " HTTP %{http_code}\n" http://127.0.0.1/
hello from srv
 HTTP 200
# restorecon -Rv /srv/web
Relabeled /srv/web/index.html from unconfined_u:object_r:httpd_sys_content_t:s0 to unconfined_u:object_r:var_t:s0
# curl -s -o /dev/null -w "HTTP %{http_code}\n" http://127.0.0.1/
HTTP 403

The chcon label is stored only on the file. Any restorecon run, a policy update or a relabel puts the policy's default back, and the 403 returns. The permanent fix teaches the policy the right label for the path, then applies it:

# semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
# semanage fcontext -l | grep "^/srv/web"
/srv/web(/.*)?                                     all files          system_u:object_r:httpd_sys_content_t:s0
# restorecon -Rv /srv/web
Relabeled /srv/web from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /srv/web/index.html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
# curl -s -w " HTTP %{http_code}\n" http://127.0.0.1/
hello from srv
 HTTP 200

A file created after the rule inherits the label (new.html was httpd_sys_content_t and served with HTTP 200). A file moved from another place keeps its old label. I moved a file from /root with mv:

# mv /root/moved.html /srv/web/; ls -Z /srv/web/moved.html
unconfined_u:object_r:admin_home_t:s0 /srv/web/moved.html
# curl -s -o /dev/null -w "HTTP %{http_code}\n" http://127.0.0.1/moved.html
HTTP 403
# restorecon -v /srv/web/moved.html
Relabeled /srv/web/moved.html from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
# curl -s -o /dev/null -w "HTTP %{http_code}\n" http://127.0.0.1/moved.html
HTTP 200
SELinux commands on Rocky Linux 9.8: a 403 from var_t, audit2allow, chcon lost by restorecon, semanage fcontext, and a boolean for a proxy
One web server problem, solved with the SELinux commands in this post.

After a mv, run restorecon, or use cp, which creates a new file with the label of the destination. The SELinux getting started guide on Fedora Docs explains the labelling model in more depth.

Step 5: SELinux commands for booleans (setsebool)

The next problem for our SELinux commands: Apache as a reverse proxy to a backend on 127.0.0.1:8081 (a Python web server). The backend answered directly, but not through httpd:

# curl -s -o /dev/null -w "backend direct: HTTP %{http_code}\n" http://127.0.0.1:8081/
backend direct: HTTP 200
# curl -s -o /dev/null -w "through httpd: HTTP %{http_code}\n" http://127.0.0.1/app/
through httpd: HTTP 503
# ausearch -m avc -ts recent | grep name_connect | tail -1
type=AVC msg=audit(1793623853.558:531): avc:  denied  { name_connect } for  pid=24003 comm="httpd" dest=8081 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:transproxy_port_t:s0 ...

The denial is name_connect: httpd tried to open a network connection, which a boolean controls. The policy had 44 booleans starting with httpd_. Look at the one you need, then change it:

# getsebool httpd_can_network_connect
httpd_can_network_connect --> off
# setsebool httpd_can_network_connect on; curl -s -w " HTTP %{http_code}\n" http://127.0.0.1/app/
backend page
 HTTP 200
# setsebool httpd_can_network_connect off
# semanage boolean -l | grep -E "^httpd_can_network_connect "
httpd_can_network_connect      (off  ,  off)  Allow httpd to can network connect
# setsebool -P httpd_can_network_connect on
# semanage boolean -l | grep -E "^httpd_can_network_connect "
httpd_can_network_connect      (on   ,   on)  Allow httpd to can network connect

The old post claimed that setsebool -P httpd_can_network_connect 1 "allows the use of a specific port". That is wrong: the boolean lets httpd make outgoing network connections in general, and the lab's description column says the same ("Allow httpd to can network connect"). The -P option makes the change permanent across reboots; the manual describes it that way, but I did not reboot to prove it. In the output above, semanage boolean -l lists the current and the default value in brackets, which is a quick way to see whether the permanent value was set.

Step 6: SELinux commands for ports (semanage port)

The third problem is a web server on a non-standard port. In this test Apache started on a new port only after that port was labelled http_port_t:

# semanage port -l | grep -E "^http_port_t"
http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
# echo "Listen 8888" > /etc/httpd/conf.d/port.conf
# systemctl restart httpd; systemctl is-active httpd
Job for httpd.service failed because the control process exited with error code.
failed
# semanage port -a -t http_port_t -p tcp 8888
# semanage port -l | grep -E "^http_port_t"
http_port_t                    tcp      8888, 80, 81, 443, 488, 8008, 8009, 8443, 9000
# systemctl restart httpd; systemctl is-active httpd
active
# curl -s -o /dev/null -w "HTTP %{http_code}\n" http://127.0.0.1:8888/
HTTP 200

I did not capture the audit line for the bind failure, but the sequence is clear: the service failed to start, adding the port to http_port_t fixed it, and the port 8888 then answered with HTTP 200. The service-level view of the same failure is covered in the systemd tricks post.

A short routine for SELinux commands

Put the SELinux commands from this post in a fixed order, so you do not guess.

  1. getenforce: is SELinux involved at all? setenforce 0 for a minute tells you quickly, but switch back.
  2. ausearch -m avc -ts recent: read the denial (source type, target type, class, permission).
  3. ls -Z and matchpathcon: is the label wrong? Fix it with semanage fcontext and restorecon.
  4. getsebool -a | grep name: is there a boolean? Use setsebool -P.
  5. semanage port: is it a non-standard port?
  6. Only then consider audit2allow, and read the rule before you load it.

SELinux commands mistakes to avoid

  • Using chcon and thinking the problem is solved: restorecon undid it in the lab.
  • Moving files with mv into a web root and keeping the old label.
  • Installing every audit2allow suggestion, which can open far more than the one file.
  • Staying in permissive mode "for now".
  • Believing a boolean opens a port; httpd_can_network_connect opens outgoing connections.

Containers add another labelling layer; the SELinux options :z and :Z for Docker volumes were not tested in the Docker volumes post, since that host had no SELinux. For another dnf-based system tool, see the yum command post.

FAQ

What do SELinux commands change, and what do they leave alone? They change labels, booleans and port types. They do not change normal Unix permissions, which still apply on top of SELinux.

Do SELinux commands need root? All of the commands above were run as root in the lab. I did not test them as a normal user.

Should I disable SELinux? The lab fixed three real problems without disabling it. Disabling was not tested here, and you would lose the protection for the whole system.

Which SELinux commands should I learn first? getenforce, ls -Z, ausearch, restorecon and semanage fcontext. They solved the largest of the three problems in this post.

Comments

Popular posts from this blog

How to Play Minecraft Bedrock Edition on Linux: A Comprehensive Guide for Tech Professionals

Linux desktop frozen? My 5 go-to tricks to try - before forcing a hard reboot

The Ultimate Guide: How to Set Up DXVK in Wine on Linux for Enhanced Gaming Performance