Contexts: ls -Z, chcon, restorecon, semanage fcontext
Sign in to read this lesson
Most SELinux problems come from a wrong file context — especially when a file is moved with mv or a site is placed in a non-standard directory. In this lesson we solve a 403 Forbidden error without turning SELinux off, and learn the important difference between chcon and semanage fcontext.
What you will learn
- A new file gets the directory's context;
cpcreates a new file (the context changes), whilemvkeeps the old context. restorecon -Rv pathreturns files to the default context in the policy — the first fix to try.chcon -t type fileis temporary:restoreconor a full relabel removes it.- A permanent rule:
semanage fcontext -a -t type "/path(/.*)?", thenrestorecon -Rv /path. matchpathcon/semanage fcontext -lshow what the policy expects for a path.
The lesson has 5 key points, an explanation and 5 questions. It opens with a package.