← Full reference

chmod

File and directory permissions — numeric (644) and symbolic (u+x), plus setuid, setgid and the sticky bit

chmod changes access permissions: who can read, write and execute a file. Permissions are set for three classes — the owner (u), the group (g) and everyone else (o). Only the file's owner or root can change them.

Syntax

chmod [options] mode file…
chmod [options] --reference=sample file…

The mode is written as a number (640) or with letters (u+x,g-w). To see the current permissions: ls -l file or stat -c '%a %A' file.

Reading permissions

BitNumberFor a fileFor a directory
r4read the contentssee the list of names
w2modify the contentscreate, delete, rename files inside
x1executeenter (cd) and access files by name

The digit for a class is the sum of the bits: 7 = rwx, 6 = rw-, 5 = r-x, 4 = r--, 0 = ---. The three digits go in the order owner, group, others: 640 = rw-r-----.

An ls -l string like -rwxr-x---: the first character is the type (- file, d directory, l link), then three characters each for owner, group and others. A + at the end (-rw-r--r--+) means the file has an ACL — extra permissions visible in getfacl.

Numeric mode

NotationMeaning
644rw-r--r-- — a regular file
600rw------- — secrets, keys
755rwxr-xr-x — directories and programs
700rwx------ — a private directory
4755the leading digit is special bits: 4 setuid, 2 setgid, 1 sticky, or their sum
0--------- — no permissions for anyone (except root)

Symbolic mode

Format: [who][operator][what], several separated by commas: u+x,go-w.

PartValues
whou owner, g group, o others, a all; no letter — all, but taking umask into account
operator+ add, - remove, = set exactly these (clear the rest)
whatr, w, x; X — x only for directories and for files where someone already has x
special bitss — setuid (u+s) or setgid (g+s); t — sticky (+t)
copy a classg=u — the group gets the same permissions as the owner; o=g etc.

Special bits

BitOn a fileOn a directory
setuid u+s / 4xxxthe program runs with the permissions of the file's owner (passwd, sudo)not used
setgid g+s / 2xxxthe program runs with the permissions of the file's groupnew files and subdirectories get the directory's group
sticky +t / 1xxxnot usedonly a file's owner can delete or rename it (as in /tmp)

In ls -l these bits take the place of x: s/t — the bit is set together with x, S/T — the bit is set but x isn't.

Options

OptionWhat it does
-Rrecursively through a directory (doesn't touch symbolic links inside)
-v / -creport every file / only those whose permissions changed
-fdon't print error messages (the exit code is still non-zero)
--reference=samplecopy the permissions from another file
--preserve-rootrefuse to operate recursively on / (no protection by default)

Examples

# a script: everything for the owner, read and execute for others
chmod 755 deploy.sh

# a private config: only the owner can read and write it
chmod 600 ~/.ssh/config

# add execute for the owner without touching anything else
chmod u+x script.sh

# remove write from the group, remove everything from others
chmod g-w,o= report.txt

# the group gets the same permissions as the owner
chmod g=u shared.txt

# a project directory: 755 for directories, 644 for files — in one command
chmod -R u=rwX,go=rX ~/project

# same with find — when files and directories need different permissions
find /srv/app -type d -exec chmod 750 {} +
find /srv/app -type f -exec chmod 640 {} +

# a shared team directory: the group can write, new files inherit the group
chmod 2775 /srv/shared

# a directory for everyone where you can't delete other people's files
chmod 1777 /srv/upload

# copy permissions from a sample and show what changed
chmod -c --reference=/etc/hosts /etc/hosts.new

# view permissions as a number and as letters
stat -c '%a %A %n' /etc/passwd /tmp

Pitfalls

  • chmod -R 644 on a directory removes the x bit from subdirectories, and a regular user can no longer enter them. For recursive changes use X, or find -type f/-type d separately.
  • A directory without x is useless: file names are visible (with r), but you can't open a file inside — "Permission denied".
  • You can delete a file without permissions on it. Deleting is a write to the directory: with w on the directory, a file with mode 000 gets deleted. The protection is the sticky bit on the directory.
  • A mode without a class respects umask. With umask 027, chmod +x adds the bit only for the owner and group, and chmod -w doesn't touch bits masked by umask. Write the class explicitly: a+x, u+x.
  • setgid on a directory isn't cleared by a three-digit number. chmod 755 on a directory with mode 2775 leaves 2755. Clear it explicitly: chmod g-s directory or chmod 00755.
  • A capital S or T in ls -l means setuid/setgid/sticky is set but the x bit under it isn't. Usually a mistake.
  • chown clears setuid/setgid. Run chown first, then chmod.
  • Symbolic links. chmod on a link changes the permissions of the target file; the link itself always has 777, which affects nothing.
  • root ignores r and w. To check that a file is locked down, test as a regular user, not from a root shell.

See also

chown · umask · acl · stat · ls · find · useradd