Skip to content
Search Oracle Linux

    Browse this documentation

    SELinux basics: contexts, booleans and port labels under the targeted policy, measured

    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.

    Terminal window
    getenforce # Enforcing
    sestatus | 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 alone

    Switching between Enforcing (deny and log) and Permissive (log only): setenforce 0/1 is temporary; for persistence change the SELINUX= line in /etc/selinux/config.

    Terminal window
    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/hosts

    Four 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”
    Terminal window
    # Temporary (a relabel reverts it) — good for testing a hypothesis
    chcon -t etc_t /tmp/seltest/f.txt
    ls -Z /tmp/seltest/f.txt # → etc_t confirms the hypothesis
    restorecon -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/web

    Port labels: the invisible gate when a service changes ports

    Section titled “Port labels: the invisible gate when a service changes ports”
    Terminal window
    semanage port -l | grep ^ssh_port
    # ssh_port_t tcp 22

    Moving 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.

    Terminal window
    getsebool httpd_can_network_connect
    # httpd_can_network_connect --> off
    setsebool -P httpd_can_network_connect on # -P persists to disk

    Booleans 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”
    Terminal window
    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.pp

    Read 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.

    • 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).