$linuxjunkies
>

chcon(1)

Change the SELinux security context of files and directories.

UbuntuDebianFedoraArch

Synopsis

chcon [OPTION]... CONTEXT FILE...
chcon [OPTION]... [-u USER] [-r ROLE] [-t TYPE] [-l LEVEL] FILE...

Description

chcon changes the SELinux security context attributes of files and directories. The security context consists of user, role, type, and level components that control access permissions under SELinux.

You can specify the context directly as a single argument, or specify individual components (user, role, type, level) separately. Changes are applied recursively with the -R flag.

Note: Context changes do not persist across filesystem relabeling. Use semanage fcontext to make permanent changes.

Common options

FlagWhat it does
-R, --recursivechange context recursively for directories and their contents
-v, --verboseoutput information about every file processed
-u USERset the SELinux user component
-r ROLEset the SELinux role component
-t TYPEset the SELinux type component
-l LEVELset the SELinux level/category component
-h, --no-dereferenceaffect symbolic links themselves, not their targets
--reference=RFILEuse context from another file instead of specifying it
-P, --preserve-rootfail rather than operate recursively on the root directory

Examples

change context to a specific SELinux context on a single file

chcon system_u:object_r:user_home_t:s0 /home/user/file.txt

recursively change context for a web server directory and all contents

chcon -R system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

change only the type component of the security context

chcon -t user_home_t /home/user/mydir

apply the same context from one location to another file or directory

chcon --reference=/var/www/html /home/user/backup

set multiple context components individually

chcon -u user_u -r user_r -t user_home_t /home/user/documents

recursively change context with verbose output to track changes

chcon -v -R system_u:object_r:var_t:s0 /var/myapp/

Related commands