Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
chmod MODE FILE...

Try it

Bash Octal and symbolic modes

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.

Bash Directories, recursion and special bits

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

Terminal
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: 640 is rw-r-----, 755 is rwxr-xr-x. A fourth leading digit holds the special bits (4755, 2775, 1777).
  • A symbolic mode with no ugoa obeys your umask: chmod +w file may not add group write if umask is 022. Write a+w to override it.
  • Never use chmod -R 777. It makes everything writable by everyone and executable, and hides real ownership problems. Use chmod -R u=rwX,go=rX for directories of data, and fix the owner with chown.
  • Directories need x to be entered. A directory with r but no x lists names but you cannot open the files; with x but no r you 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 dir leaves it set; clear it with g-s or give five digits (00755) in GNU chmod.
  • Setuid is ignored on scripts on Linux, and nosuid mounts disable it entirely. Avoid setuid programs of your own.
  • Symbolic links: chmod follows them and changes the target. A symlink's own mode is irrelevant on Linux. With -R it does not follow links it finds while recursing.
  • Special filesystems (FAT, NTFS, some network mounts) do not store Unix bits; chmod may 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 with stat -c %a file.
  • Access control lists can grant more than the mode bits show; a + after the mode in ls -l (rw-r--r--+) means ACLs exist.