Chapter 16: SELinux

SELinux cheat sheet

The chapter on one page: what to remember, the commands to have at your fingertips, and flashcards for quick revision.

The chapter in eight sentences

Cheat sheet

See

CommandDoes
getenforce · sestatusMode and policy
setenforce 0 / 1Permissive / enforcing (until reboot)
/etc/selinux/configSELINUX=enforcing (permanent)
ls -Z · ps -eZ · id -ZContexts
matchpathcon PATHThe expected type

Fix

CommandDoes
semanage fcontext -a -t T "/p(/.*)?"Add a labelling rule
semanage fcontext -l -C · -d "/p(/.*)?"List / delete local rules
restorecon -Rv PATH (-n to preview)Apply the rules
chcon -t T FILETemporary label
getsebool -a · setsebool -P B onBooleans
semanage boolean -l -CChanged booleans
semanage port -a -t T -p tcp N · -l -C · -dPort labels

Diagnose

CommandDoes
setenforce 0, retry, setenforce 1Is it SELinux?
grep AVC /var/log/audit/audit.logThe denials
sealert -a /var/log/audit/audit.logPlain-language explanation
ausearch -m AVC -i (add -if FILE if needed)Search the audit log
journalctl -u SERVICEWhat the service says
audit2allow -M nameCustom module: last resort

Flashcards

What is a context, and which part matters?
user:role:type:level; the type (…_t) is what the targeted policy rules are about.
See contexts of files, processes, yourself?
ls -Z · ps -Z · id -Z
Show and set the mode?
getenforce · sestatus · setenforce 0|1 (temporary) · SELINUX= in /etc/selinux/config (permanent)
Which type should a path have?
matchpathcon PATH
Permanent label for a directory tree?
semanage fcontext -a -t TYPE "/dir(/.*)?" then restorecon -Rv /dir
Why is chcon temporary?
It records no rule; restorecon or a relabel resets the label.
cp versus mv?
cp takes the destination default; mv keeps the old label: restorecon after mv.
List and set booleans?
getsebool -a · semanage boolean -l · setsebool -P NAME on|off
Allow a service on a non-standard port?
semanage port -a -t http_port_t -p tcp 82 (list: semanage port -l)
Where are denials?
/var/log/audit/audit.log: grep AVC · sealert -a /var/log/audit/audit.log
Is SELinux the cause?
setenforce 0, retry, setenforce 1.
Troubleshooting order?
Test permissive, read the AVC, choose label / boolean / port, smallest fix, test in enforcing.