Permissions and owners

The chcon command

Changes the SELinux label of a file or folder by hand.

Syntax

sudo chcon -t <tipo> <ruta>Try it in the terminal →

Most useful options

-t, --type= <tipo>SELinux type to assign, such as httpd_sys_content_t
--reference= <archivo>Copies the label of another file
-R, --recursiveApplies the change to the folder and all its contents
-v, --verboseShows each file it processes
-u, --user= <usuario>Changes the user part of the label
-l, --range= <rango>Changes the MLS/MCS range of the label

Copy-ready examples

$ sudo chcon -t httpd_sys_content_t /var/www/html/index.htmlassigns a file the httpd web content type.
$ sudo chcon --reference=/var/www/html/index.html /var/www/html/nueva.htmlcopies the label of a working file to a new one.
$ sudo chcon -Rv -t httpd_sys_content_t /var/www/htmlapplies the type to the whole folder, showing each file.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
chcon: can't apply partial context to unlabeled file 'nota.txt'The file has no previous label and -t only gives part of it. It usually happens when SELinux is not active or the filesystem does not store labels.Check the mode with getenforce, or pass the full context instead of only the type.
chcon: -R --dereference requires either -H or -LWith -R, symbolic links are handled with -H, -L or -P. If you use --dereference, -H or -L is required.Add -H or -L together with -R and --dereference, or drop --dereference.
chcon: -R -h requires -PWhen -R and -h are used together, the program requires -P.Add -P: sudo chcon -R -h -P -t httpd_sys_content_t /var/www/html.

Typical mistakes

  • chcon changes the label of that file, not the rule: restorecon puts it back to what the policy dictates.
  • To make the change last, create the rule with semanage fcontext and then run restorecon.
  • Giving a service a type that is too broad just to make it work can leave it more exposed. Check the warnings before keeping it that way.

Differences from similar commands

  • restorecon: resets the label the policy dictates. chcon sets it by hand.
  • chmod: changes Unix permissions. chcon changes the SELinux label, and the two are independent.
  • semanage: stores a rule in the policy that applies to everything that matches, not to one file.

FAQ

Is it the same as chmod?

No. chmod changes the Unix permissions (rwx); chcon changes the SELinux label, which is a separate layer.

Is it permanent?

Not as a rule: restorecon puts the label back to the policy's. To make it last, use semanage fcontext.

Can I copy the label from another file?

Yes, with --reference=file, which takes the context of the file you name.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course