semanage-fcontext(8)
π semanage-fcontext β the girlies' guide to SELinux file contexts π
okay bestie, welcome to semanage-fcontext(8) β yes that's the manual section, yes I'm obsessed, sit down, we're doing this π«Ά
β¨ NAME β¨
semanage-fcontext β SELinux Policy Management file context tool πΈ
β¨ SYNOPSIS β¨
semanage fcontext [-h] [-n] [-N] [-S STORE] [ --add ( -t TYPE -f FTYPE
-r RANGE -s SEUSER | -e TARGET_PATH ) FILE_SPEC | --delete ( -t TYPE -f
FTYPE | -e TARGET_PATH ) FILE_SPEC | --deleteall | --extract | --list
[-C] | --modify ( -t TYPE -f FTYPE -r RANGE -s SEUSER | -e TARGET_PATH )
FILE_SPEC ]
like, look at her. a whole menu of moods. pick one π
β¨ DESCRIPTION β¨
semanage is the tool you use to configure certain elements of SELinux policy without needing to modify or recompile anything from policy sources. iconic behavior, honestly.
semanage fcontext specifically handles the default file system labeling on an SELinux system. basically: it maps file paths β using regular expressions β onto SELinux labels. path goes in, label comes out, slay β¨
π¦ about FILE_SPEC π¦
FILE_SPEC can be either a fully qualified path, or a Perl compatible regular expression (PCRE) that describes fully qualified path(s).
The only PCRE flag in use is PCRE2_DOTALL, which makes a wildcard . match anything, including a new line. yes, even that. π
Strings representing paths are processed as bytes (as opposed to Unicode), which means non-ASCII characters are not matched by a single wildcard. no cap, that's just how it is.
π priority tea (this part matters) π
Note, that file context definitions specified usingsemanage fcontext(i.e. local file context modifications stored infile_contexts.local) have higher priority than those specified in policy modules.
So: whenever a match for a given file path is found in file_contexts.local, no other file context definitions are considered. periodt.
Entries in file_contexts.local are processed from most recent one to the oldest, with the first match being used β as opposed to the most specific match, which is what's used when matching other file context definitions. π
So all your regular expressions should be as specific as possible, to avoid unintentionally impacting other parts of the filesystem. don't be messy with it bestie, she will notice π
β¨ OPTIONS β¨
-h,--helpβ Show this help message and exit. the classic "help me" button π«Ά-n,--noheadingβ Do not print heading when listing the specified object type. minimalism, but make it CLI.-N,--noreloadβ Do not reload policy after commit. bold move, bestie.-C,--locallistβ List local customizations. just the local stuff, nothing else π-S STORE,--store STOREβ Select an alternate SELinux Policy Store to manage. shop around, why not ποΈ-a,--addβ Add a record of the specified object type.-d,--deleteβ Delete a record of the specified object type.-m,--modifyβ Modify a record of the specified object type.-l,--listβ List records of the specified object type.-E,--extractβ Extract customizable commands, for use within a transaction.-D,--deleteallβ Remove all local customizations. all of them. every last one. π-e TARGET_PATH,--equal TARGET_PATHβ SubstituteFILE_SPECwithTARGET_PATHfor file label lookup. This is used withfcontext. Requires source and target path arguments to be path prefixes and does not support regular expressions. The context labeling for the target subtree is made equivalent to that defined for the source. Note that substitution based on another substitution does not work. no chain reactions here, sorry π§-f [{a,f,d,c,b,s,l,p}],--ftype [{a,f,d,c,b,s,l,p}]β File Type. This is used withfcontext. Requires a file type as shown in the mode field byls, e.g. use'd'to match only directories or'f'to match only regular files. The following file type options can be passed:f(regular file),d(directory),c(character device),b(block device),s(socket),l(symbolic link),p(named pipe). If you do not specify a file type, the file type will default to"all files". go big or go all-files π-s SEUSER,--seuser SEUSERβ SELinux user name.-t TYPE,--type TYPEβ SELinux Type for the object. the main character of the label tbh π-r RANGE,--range RANGEβ MLS/MCS Security Range (MLS/MCS Systems only). SELinux Range for SELinux login mapping defaults to the SELinux user record range. SELinux Range for SELinux user defaults tos0.
β¨ EXAMPLE β¨
Remember to run restorecon after you set the file context. literally the most important sentence here bestie, don't skip her π
Add file-context httpd_sys_content_t for everything under /web
# semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
# restorecon -R -v /web
Substitute /home1 with /home when setting file context
i.e. label everything under /home1 the same way /home is labeled
# semanage fcontext -a -e /home /home1
# restorecon -R -v /home1
If you substitute /home2 with /home1 from the example above it will not work. You must use direct substitution.
# semanage fcontext -a -e /home /home2
# restorecon -R -v /home2
For home directories under top level directory, for example /disk6/home, execute the following commands.
# semanage fcontext -a -t home_root_t "/disk6"
# semanage fcontext -a -e /home /disk6/home
# restorecon -R -v /disk6
Add file-context with MLS range s0:c0.c255 for /secure directory (MLS/MCS systems only)
# semanage fcontext -a -t admin_home_t -r s0:c0.c255 "/secure(/.*)?"
# restorecon -R -F -v /secure
β¨ SEE ALSO β¨
selinux(8), semanage(8), restorecon(8), selabel_file(5)
go read those too, they're all part of the friend group πΈ
β¨ AUTHOR β¨
This man page was written by Daniel Walsh <dwalsh@redhat.com> β thank you for your service, king π«‘π
and that's the whole page bestie!! this one's dated 20130617 and lives in the semanage-fcontext(8) manual section β old money, timeless, we stan π
β¨π¦ππ
π¦ where this page comes from
- centos-stream 10: policycoreutils-python-utils 3.11-1.el10 Β· GPL-2.0-or-later
- centos-stream 9: policycoreutils-python-utils 3.6-7.el9 Β· GPL-2.0-or-later
- centos-stream 9: policycoreutils-python-utils 3.6-9.el9 Β· GPL-2.0-or-later
- fedora 44: policycoreutils-python-utils 3.11-2.fc44 Β· GPL-2.0-or-later
- ubuntu stonking: policycoreutils-python-utils 3.11-2 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms