How the numbers work
Each digit is one group of people: the file's owner, its group, and everyone else. Inside each digit, read is worth 4, write 2 and execute 1, and you add them up. So 7 is read + write + execute, 6 is read + write, 5 is read + execute and 4 is read only.
755 therefore means the owner can do everything, and the group and everyone else can read and execute. In symbolic form that's rwxr-xr-x.
What execute means for folders
On a directory, read lets you list its contents, write lets you create and delete files in it, and execute lets you enter it and reach the files inside. A folder needs execute to be usable at all, which is why directories are usually 755 or 700, and files 644 or 600.
The special bits
- setuid (4xxx): a program runs as the file's owner instead of the person who started it. passwd uses this to update system files.
- setgid (2xxx): on a program, it runs with the file's group; on a directory, new files inherit the directory's group, which is handy for shared project folders.
- Sticky (1xxx): on a directory, only a file's owner can delete or rename it. /tmp is 1777 for this reason.
- In ls -l output these show as s or t in the execute position, or S or T when execute isn't set.
Common chmod values
- 777 (rwxrwxrwx): everyone can read, write and execute. Avoid it, especially on web servers.
- 775 (rwxrwxr-x): owner and group can write; shared project folders.
- 755 (rwxr-xr-x): directories, scripts and programs.
- 750 (rwxr-x---): like 755 but hidden from everyone outside the group.
- 700 (rwx------): private directories such as ~/.ssh.
- 664 (rw-rw-r--): files a group edits together.
- 644 (rw-r--r--): ordinary files, web pages, images, configs.
- 640 (rw-r-----): config files with secrets that a service group needs to read.
- 600 (rw-------): private keys, .env files, credentials.
- 400 (r--------): read-only for the owner, e.g. an AWS .pem key.
Symbolic chmod commands
Besides numbers, chmod accepts letters. Pick who (u for owner, g for group, o for others, a for all), an operator (+ to add, - to remove, = to set exactly) and the permissions (r, w, x).
- chmod u+x deploy.sh: let the owner run the script.
- chmod go-w file: remove write access for group and others.
- chmod a=r file: everyone can read, nobody can write or execute.
- chmod -R u=rwX,go=rX dir: capital X adds execute only to folders and files that are already executable, which is the safe way to fix a whole tree.