SELinux

From wiki
Revision as of 12:07, 20 September 2022 by Johayek (talk | contribs)
Jump to navigation Jump to search

learning.oreilly.com :

access.redhat.com :

context of a file

So far my usual approach for getting the SELinux context of a file right is:

# find a file with the right context! ("FILE_WITH_RIGHT_CONTEXT")
$ ll --context FILE_WITH_RIGHT_CONTEXT
# rename the file, whose context isn't right, to FILE- or so!
$ mv FILE FILE-
# …
$ cp --arch FILE_WITH_RIGHT_CONTEXT FILE
# …
cat FILE- > FILE
# …
$ rm FILE-
…

What "ll -Z" (resp. "ll --context") in addition to what "ll" usually displays, is called:

  • a SELinux_USER_CONTEXT (unconfined_u) -> "semanage fcontext --modify --seuser"
  • a role (object_r)
  • a TYPE_CONTEXT (user_home_t) -> "semanage fcontext --modify --type"
  • a level AKA RANGE (s0) -> "semanage fcontext "
root@… $ chcon --no-dereference --user SELinux_USER_CONTEXT --role ROLE --type TYPE_CONTEXT --range RANGE /usr/local/foo.txt
root@… $ semanage fcontext --add/--modify --seuser SELinux_USER_CONTEXT --type TYPE_CONTEXT --range RANGE /usr/local/foo.txt
root@… $ restorecon -vF /usr/local/foo.txt     # -v for ''verbose'', -F for ''force reset''

So usually for a systemd service file you would execute these 2 command lines:

root@… $ semanage fcontext --add/--modify --seuser system_u --type systemd_unit_file_t /usr/lib/systemd/system/tomcat.service
root@… $ restorecon -vF /usr/lib/systemd/system/tomcat.service

More examples: