Linux Command Reference ยท Permissions and Users
Linux chmod command
Reference for the Linux chmod command: octal and symbolic modes, recursive changes, the capital X trick, setuid, setgid and sticky bits, and common mistakes.
chmod ("change mode") changes the permission bits of files and directories: who may read, write or execute them. Every file has three sets of bits, for its owner (u), its group (g) and everyone else (o), plus three special bits (setuid, setgid and the sticky bit). Only the owner of a file, or root, can change its mode.
You can give the new mode in octal (644) or in symbolic form (u+x, go-w). Octal sets everything at once; symbolic changes only what you name, which is safer in scripts and for recursive changes.
chmod MODE FILE...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...
A symbolic mode is [ugoa]*[+-=][rwxXst]*. u user (owner), g group, o others, a all (the default, filtered by umask). + adds, - removes, = sets exactly.
| Letter / digit | Meaning |
|---|---|
r / 4 |
Read a file; list a directory. |
w / 2 |
Modify a file; create, rename or delete entries in a directory. |
x / 1 |
Execute a file; enter (search) a directory. |
X |
Execute only if it is a directory or already executable for someone. |
s |
setuid (4000) or setgid (2000): run as the file's owner/group; on directories, new files inherit the group (setgid). |
t / 1000 |
Sticky bit on a directory: only the file owner or directory owner may delete or rename entries (like /tmp). |
| Option | Description |
|---|---|
-R, --recursive |
Change files and directories below the named ones. |
-v, --verbose |
Report every file processed. |
-c, --changes |
Report only the files that changed. |
-f, --silent |
Suppress most error messages. |
--reference=RFILE |
Copy the mode from another file. |
--preserve-root |
Refuse to operate recursively on /. |
Returns: 0 if all changes succeeded, non-zero otherwise.
Errors: Operation not permitted when you do not own the file, No such file or directory, and invalid mode for a malformed mode string.
Usage notes
- Octal digits are the sum of read 4, write 2, execute 1, written for user, group, other:
640isrw-r-----,755isrwxr-xr-x. A fourth leading digit holds the special bits (4755,2775,1777). - A symbolic mode with no
ugoaobeys your umask:chmod +w filemay not add group write ifumaskis022. Writea+wto override it. - Never use
chmod -R 777. It makes everything writable by everyone and executable, and hides real ownership problems. Usechmod -R u=rwX,go=rXfor directories of data, and fix the owner withchown. - Directories need
xto be entered. A directory withrbut noxlists names but you cannot open the files; withxbut noryou can open known names but not list them. - Deleting a file needs write permission on its directory, not on the file itself.
- Numeric modes keep setgid on directories. On a directory that already has the setgid bit,
chmod 755 dirleaves it set; clear it withg-sor give five digits (00755) in GNUchmod. - Setuid is ignored on scripts on Linux, and
nosuidmounts disable it entirely. Avoid setuid programs of your own. - Symbolic links:
chmodfollows them and changes the target. A symlink's own mode is irrelevant on Linux. With-Rit does not follow links it finds while recursing. - Special filesystems (FAT, NTFS, some network mounts) do not store Unix bits;
chmodmay do nothing or fail. Mount options decide the apparent modes. - Copy a known-good mode:
chmod --reference=good bad. Show a file's octal mode withstat -c %a file. - Access control lists can grant more than the mode bits show; a
+after the mode inls -l(rw-r--r--+) means ACLs exist.
Related
chown,chgrp,umask,stat,ls- Lessons: File Permissions and Ownership, ACLs and Attributes