tallge

Reference / File permissions (chmod)

File permissions with chmod: octal, symbolic, special bits

Every file has three sets of three bits: for the owner, for the group and for everyone else. chmod sets them either as an octal number or with symbolic changes.

Reading ls -l

-rwxr-x---  1 deploy www  4096 ... run.sh
│└┬┘└┬┘└┬┘
│ │  │  └── others: ---  nothing
│ │  └───── group:  r-x  read and execute
│ └──────── owner:  rwx  read, write, execute
└────────── type:   - file, d directory, l symbolic link
DigitBitsSymbolicMeaning
0000---no access
1001--xexecute only
2010-w-write only
3011-wxwrite and execute
4100r--read only
5101r-xread and execute
6110rw-read and write
7111rwxeverything

Each octal digit is the sum of read 4, write 2 and execute 1. The three digits are owner, group and others, in that order.

What the bits mean on a directory

BitOn a fileOn a directory
rread the contentslist the names in it
wchange the contentstogether with x: create, delete and rename entries in it, whatever the files’ own modes
xrun it as a programenter it and reach anything inside by name

Deleting a file needs write permission on the directory, not on the file. A directory with r but no x lets you list names but not open anything in it. With x but no r you can open files whose names you already know.

Common modes

ModeSymbolicTypical use
644rw-r--r--ordinary files, web content
600rw-------private keys, files with passwords; ssh refuses keys that are more open
640rw-r-----config readable by a service group
755rwxr-xr-xdirectories, scripts and binaries everyone may run
750rwxr-x---directories for one group
700rwx------~/.ssh and other private directories
775rwxrwxr-xshared project directories, usually with setgid
400r--------read-only secrets
777rwxrwxrwxalmost never right; use group ownership instead

Symbolic form

TaskCommand
Make a script executable for its ownerchmod u+x deploy.sh
Remove write for group and otherschmod go-w file
Set others to nothing, exactlychmod o= file
Read for everyonechmod a+r file
Several changes at oncechmod u=rwx,g=rx,o= dir
Directories 755, files 644 (755 if already executable), in one treechmod -R u=rwX,go=rX dir
Copy the owner’s bits to the groupchmod g=u file
Show modes in octalstat -c "%a %n" *

Capital X adds execute only to directories and to files that already have execute for someone. It is the safe way to fix a whole tree, because chmod -R 755 would make every file executable.

Special bits

BitOctalOn a fileOn a directoryIn ls -l
setuid4000runs with the owner’s privileges, as passwd doesignored on Linuxrws in the owner triple
setgid2000runs with the group’s privilegesnew files inherit the directory’s grouprws in the group triple
sticky1000ignored on Linuxonly the owner of the file, the owner of the directory or root may delete or rename it, as in /tmprwt in the others triple

The special bits form a fourth leading digit: chmod 2775 shared/, chmod 1777 /tmp. A capital S or T in ls -l means the special bit is set but the execute bit under it is not, which is usually a mistake. The kernel ignores setuid on scripts.

umask: the default for new files

New files start from 666 and new directories from 777, and every bit set in the umask is cleared. The common umask 022 gives files 644 and directories 755. A umask of 027 gives 640 and 750, and 077 gives 600 and 700.

TaskCommand
Show the current umaskumask
Show it symbolicallyumask -S
Set it for this shellumask 027

A service's umask comes from its unit or init script, not from your shell. In systemd, set UMask=0027 in the unit.

Ownership and ACLs

TaskCommand
Change owner and groupsudo chown deploy:www file
Change recursivelysudo chown -R deploy:www /srv/app
Change only the groupchgrp www file
Show ACL entriesgetfacl file
Give one more user read accesssetfacl -m u:backup:r file

A + after the mode in ls -l (-rw-r-----+) means an ACL adds rules that the nine bits do not show.

Sources