What SELinux Is
Normal Linux permissions ask who owns a file. SELinux adds a second question: which type of program may do what to which type of object. Every process, file, and port carries a label, and a policy lists the allowed combinations. A web server labeled httpd_t may read files labeled httpd_sys_content_t and listen on ports labeled http_port_t. If an attacker takes over the web server, SELinux still keeps it away from SSH keys, users' homes, and most of the system.
The Three Modes
| Mode | What happens |
|---|---|
enforcing | The policy applies; denials are blocked and logged. |
permissive | Nothing is blocked; denials are only logged. |
disabled | SELinux is off, and new files get no labels. |
Enforcing is the default and the mode to keep. Coming back from disabled needs a relabel of the whole file system at boot, one more reason not to go there. For diagnosis, make one program permissive, as shown below.
Check the Status and the Labels
getenforce
sestatus
ls -Z /var/www/html
ps -eZ | grep nginx
getenforce prints the current mode. sestatus adds the mode set in /etc/selinux/config, which applies at boot, and the loaded policy, normally targeted: it confines network services and system daemons and leaves ordinary user sessions mostly unconfined. -Z shows labels such as system_u:object_r:httpd_sys_content_t:s0; the third part, the type, is the one that matters in daily work.
Read the Denials
When SELinux blocks an action, it writes an AVC denial to the audit log:
sudo ausearch -m avc -ts recent
sudo ausearch -m avc -ts recent | audit2why
sudo journalctl -t setroubleshoot
A denial names the program (scontext), the object (tcontext), and the action, such as read or name_connect. audit2why explains it and often names the boolean that would allow it. With the setroubleshoot-server package installed, the journal also holds a plain-language summary and a suggested command, and sealert -l with the ID from that message prints it in full. semanage, audit2why, and audit2allow come from policycoreutils-python-utils; ausearch comes from the audit package.
Fix 1: Turn On a Boolean
Booleans are switches for common needs. A typical case: Nginx as a reverse proxy cannot reach its backend, and the denial shows name_connect:
getsebool -a | grep httpd
sudo setsebool -P httpd_can_network_connect on
-P makes the change permanent; without it, the boolean resets at the next boot. See Nginx reverse proxy.
Fix 2: Correct a File Label
Files moved with mv keep their old label, while new files, including copies made with cp without options that preserve the context, get the label of their new folder. That is why a site moved from a home folder can return 403 errors. Restore the expected labels:
sudo restorecon -Rv /var/www/html
For a folder the policy does not know, such as /srv/site, define the label first, then apply it:
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/site(/.*)?"
sudo restorecon -Rv /srv/site
Avoid chcon for lasting fixes; its labels are lost at the next relabel. More on that error in 403 Forbidden in Nginx.
Fix 3: Label a Port
sudo semanage port -l | grep http_port_t
sudo semanage port -a -t http_port_t -p tcp 8081
A service may listen only on ports labeled for it. SSH on a new port needs the type ssh_port_t in the same way; see how to change the SSH port. If the port already has another type, use -m instead of -a.
Diagnose One Program in Permissive Mode
sudo semanage permissive -a httpd_t
sudo semanage permissive -d httpd_t
The first command lets only the web server domain run unblocked while every denial is still logged; the rest of the system stays enforcing. Reproduce the problem, read all the denials, apply the right boolean or label, then remove the exception with the second command. Only as a last resort, after reading the denials, turn them into a small local module with audit2allow -M, and check what it allows before you load it.
AppArmor on Debian and Ubuntu
Ubuntu and Debian use AppArmor instead, enabled by default on both: it confines programs with profiles based on file paths rather than labels. sudo aa-status lists the loaded profiles, and denials appear in the kernel log with apparmor="DENIED".