Back to the command reference

chmod

FilesFundamentals In simulator In real Linux

Change file permissions

Name: chmod = change mode

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

chmod changes a file's permission bits: who (owner u, group g, others o) may read r, write w and execute x. Modes can be given as three octal digits (e.g. 755) or symbolically (e.g. u+x, go-w).

Only the owner or root may change them. On directories, x means you may enter it and r that you may list it.

Besides rwx there are three special bits: setuid (4000: the program runs as the file’s owner, as passwd does), setgid (2000: on a directory, new files get its group) and sticky (1000: on a directory, people may only delete their own files, as in /tmp). ls -l shows them as s for the owner, s for the group and t for others; a capital S or T means the execute bit under it is missing, usually a mistake.

Syntax

chmod [OPTION]... MODE FILE...

Common options

755Octal: owner rwx, group and others r-x
u+xSymbolic: add execute for the owner (u/g/o/a, +/-/=, r/w/x)
4755 / 2775 / 1777Four digits: the first is the special bits (4 setuid, 2 setgid, 1 sticky; they add up)
u+s / g+s / +tSpecial bits with letters: setuid, setgid, sticky
XExecute only for directories (and files already executable), handy with -R
-R / --recursiveChange files and directories recursively
-v / --verbosePrint a line for every file processed
-c / --changesPrint only the files that actually changed
--reference FILEUse FILE’s mode

Examples

  • $ chmod +x projects/hello.py

    Make a script executable

    Explain
  • $ chmod 600 documents/notes.txt

    Only the owner may read and write

    Explain
  • $ chmod go-w README.md

    Remove write permission for group and others

    Explain
  • $ chmod g+s documents

    Set setgid: files created in this directory from now on get its group (common for team directories)

    Explain
  • $ chmod 1777 downloads

    Like /tmp: everyone may create files, but only delete their own (sticky)

    Explain
  • $ chmod -R u=rwX,go=rX projects

    A whole tree: directories stay enterable; only files that were executable keep x

    Explain

Related