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
| Bit | Number | For a file | For a directory |
|---|---|---|---|
r | 4 | read the contents | see the list of names |
w | 2 | modify the contents | create, delete, rename files inside |
x | 1 | execute | enter (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
| Notation | Meaning |
|---|---|
644 | rw-r--r-- — a regular file |
600 | rw------- — secrets, keys |
755 | rwxr-xr-x — directories and programs |
700 | rwx------ — a private directory |
4755 | the 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.
| Part | Values |
|---|---|
| who | u owner, g group, o others, a all; no letter — all, but taking umask into account |
| operator | + add, - remove, = set exactly these (clear the rest) |
| what | r, w, x; X — x only for directories and for files where someone already has x |
| special bits | s — setuid (u+s) or setgid (g+s); t — sticky (+t) |
| copy a class | g=u — the group gets the same permissions as the owner; o=g etc. |
Special bits
| Bit | On a file | On a directory |
|---|---|---|
setuid u+s / 4xxx | the program runs with the permissions of the file's owner (passwd, sudo) | not used |
setgid g+s / 2xxx | the program runs with the permissions of the file's group | new files and subdirectories get the directory's group |
sticky +t / 1xxx | not used | only 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
| Option | What it does |
|---|---|
-R | recursively through a directory (doesn't touch symbolic links inside) |
-v / -c | report every file / only those whose permissions changed |
-f | don't print error messages (the exit code is still non-zero) |
--reference=sample | copy the permissions from another file |
--preserve-root | refuse 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 644on a directory removes thexbit from subdirectories, and a regular user can no longer enter them. For recursive changes useX, orfind -type f/-type dseparately.- A directory without
xis useless: file names are visible (withr), 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
won the directory, a file with mode000gets deleted. The protection is the sticky bit on the directory. - A mode without a class respects umask. With umask
027,chmod +xadds the bit only for the owner and group, andchmod -wdoesn'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 755on a directory with mode2775leaves2755. Clear it explicitly:chmod g-s directoryorchmod 00755. - A capital
SorTinls -lmeans setuid/setgid/sticky is set but thexbit under it isn't. Usually a mistake. chownclears setuid/setgid. Runchownfirst, thenchmod.- Symbolic links.
chmodon a link changes the permissions of the target file; the link itself always has777, which affects nothing. - root ignores
randw. To check that a file is locked down, test as a regular user, not from a root shell.