"Set it to 777 and everything will work" is one of the most harmful tips on the internet. Linux permissions are simple, and after ten minutes with them you will set 755 and 644 knowingly rather than by guessing. Here is what the numbers and letters mean, which permissions a website, SSH keys and scripts need, and what umask is.
Who can do what
Every file has an owner and a group. Permissions are set for three categories:
- u (user) — the file's owner;
- g (group) — members of the file's group;
- o (others) — everyone else.
Each category has three rights: r — read, w — write, x — execute. For directories the meaning differs a little: r — list the files, w — create and delete files inside, x — enter the directory and access the files in it.
ls -l shows permissions as a string: -rwxr-xr-x. The first character is the type (- file, d directory, l link), followed by three characters each for the owner, the group and others.
How to read the numbers
Each right is a number: r = 4, w = 2, x = 1. A category's permissions are their sum:
| Digit | Rights | Meaning |
|---|---|---|
| 7 | rwx | Everything: read, change, run |
| 6 | rw- | Read and change |
| 5 | r-x | Read and run |
| 4 | r-- | Read only |
| 0 | --- | Nothing |
Three digits are the owner, the group and others. 755 = rwxr-xr-x: the owner can do everything, others can read and run. 644 = rw-r--r--: the owner reads and writes, others only read.
Which permissions to set
| What | Permissions | Why |
|---|---|---|
| Website files | 644 | The web server reads them but does not change them |
| Website directories | 755 | Without x you cannot enter a directory |
| Scripts | 755 or 700 | They need the execute right |
| Configs with passwords (.env, wp-config.php) | 600 or 640 | No outsider should read the passwords |
~/.ssh | 700 | SSH refuses to work if others can access the directory |
Private key ~/.ssh/id_ed25519 | 600 | Only the owner reads the key |
~/.ssh/authorized_keys | 600 | Otherwise sshd may ignore the keys |
It is convenient to fix directories and files separately:
find /var/www/site -type d -exec chmod 755 {} +
find /var/www/site -type f -exec chmod 644 {} +
Why not 777
777 lets any user on the system and any process change the file. On a website, that means a vulnerability in one plugin, or someone else's site on the same server, can replace your files and inject malicious code. If "it does not work without 777", the problem is almost always the owner: the files belong to a different user than the one the web server or PHP runs as. The fix is chown, not permissions for everyone.
Symbolic notation
You can change permissions precisely without recalculating digits:
chmod u+x script.sh # give the owner the right to run chmod g-w file.txt # remove write from the group chmod o= secret.txt # nothing for others chmod -R a+X dir/ # x only for directories and already executable files
The capital X is a useful detail: in a recursive change it gives the right to enter directories but does not make ordinary files executable.
Special bits
- setuid (4) — the program runs with its owner's rights, like
passwd. A stray setuid file owned by root is a ready-made hole. - setgid (2) on a directory — new files get the directory's group; handy for shared folders.
- sticky (1) on a directory — only the owner can delete a file, as in
/tmp(permissions 1777).
To find all setuid files on the system: find / -perm -4000 -type f 2>/dev/null.
umask: default permissions
umask defines which rights are removed from new files. With the usual umask 022, new files get 644 and directories 755. With umask 027, other users cannot see new files at all: 640 and 750.
Permissions calculator
The chmod calculator converts permissions between digits, letters and checkboxes, applies expressions like u+x,g-w the same way GNU chmod does, handles special bits and works out default permissions for a umask. And if access is needed to commands rather than files, there are sudoers rules.