SELinux basics: contexts, booleans and port labels under the targeted policy, measured
What you will learn
Section titled “What you will learn”OL defaults to SELinux Enforcing with the targeted policy. This page covers the operations surface actually used day to day: reading state, reading contexts, fixing contexts (temporary vs persistent), port labels, booleans — and the standard troubleshooting flow when something gets denied.
State: read it in three lines
Section titled “State: read it in three lines”getenforce # Enforcingsestatus | head -6# SELinux status: enabled# Current mode: enforcing# Mode from config file: enforcing ← /etc/selinux/config matches the current mode# Policy name: targeted ← constrains network services only, leaves unconfined users aloneSwitching between Enforcing (deny and log) and Permissive (log only): setenforce 0/1 is temporary; for persistence change the SELINUX= line in /etc/selinux/config.
Contexts: files carry their own labels
Section titled “Contexts: files carry their own labels”ls -Z /etc/passwd /etc/hosts# system_u:object_r:passwd_file_t:s0 /etc/passwd# system_u:object_r:net_conf_t:s0 /etc/hostsFour segments user:role:type:level; day to day only the type matters. A service process may read and write only the types its rules allow — when “a service cannot read a file”, nine times out of ten the type is wrong.
Fixing contexts: temporary chcon vs persistent semanage
Section titled “Fixing contexts: temporary chcon vs persistent semanage”# Temporary (a relabel reverts it) — good for testing a hypothesischcon -t etc_t /tmp/seltest/f.txtls -Z /tmp/seltest/f.txt # → etc_t confirms the hypothesisrestorecon -v /tmp/seltest/f.txt # restore the rule default
# Persistent (the rule is recorded; restorecon honors it)semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"restorecon -R /srv/webPort labels: the invisible gate when a service changes ports
Section titled “Port labels: the invisible gate when a service changes ports”semanage port -l | grep ^ssh_port# ssh_port_t tcp 22Moving sshd to 2222 also needs: semanage port -a -t ssh_port_t -p tcp 2222. Otherwise sshd will not start (an AVC recording a port-type denial). ss shows the listener, firewalld allows the port, and SELinux must also pass — all three layers, no exceptions.
Booleans: policy switches
Section titled “Booleans: policy switches”getsebool httpd_can_network_connect# httpd_can_network_connect --> offsetsebool -P httpd_can_network_connect on # -P persists to diskBooleans are switches the policy authors left exposed (several hundred); getsebool -a lists them all. Common ones: httpd_can_network_connect (reverse proxies making outbound calls), ftpd_full_access, and so on.
When something gets denied: the standard flow
Section titled “When something gets denied: the standard flow”ausearch -m avc -ts recent # find denial records (measured: 0 on a clean system)# When records exist:ausearch -m avc -ts recent | audit2allow -M mymod # generate a policy module (understand it before installing)semodule -i mymod.ppRead the record before allowing anything — a one-shot audit2allow is the last resort; the correct fix for most denials is adjusting the context, port label or boolean.
Evidence and limits on this page
Section titled “Evidence and limits on this page”- Measured: state, contexts, the chcon/restorecon flow, port labels, booleans, and the zero-AVC baseline (E3);
- Not measured: the complete audit2allow module-generation chain (needs a real AVC scenario; to be triggered later on a service-deployment page with a non-standard port);
- Sources: the OL10 SELinux user guide,
semanage(8),audit2allow(1).