How Unix permissions work
Every file and directory has three sets of permissions: one for its owner, one for its group and one for everyone else. Each set has three switches: read (r), write (w) and execute (x). That’s nine switches in all, which ls -l prints as nine letters like rwxr-xr-x, with a dash for each one that’s off.
For a file, read means you can see what’s in it, write means you can change it and execute means you can run it as a program. For a directory they mean something a little different: read lets you list the names inside, write lets you add, rename and delete files, and execute lets you go into it and open what’s there. A directory nearly always needs execute wherever it has read.
Reading octal modes
Octal packs each set of three switches into one digit. Read is worth 4, write 2 and execute 1, and you add up the ones that are on. So rwx is 4 + 2 + 1 = 7, r-x is 4 + 1 = 5 and r-- is 4. Three digits give owner, group and others, in that order.
| Digit | Letters | Meaning |
|---|---|---|
| 7 | rwx | Read, write and execute |
| 6 | rw- | Read and write |
| 5 | r-x | Read and execute |
| 4 | r-- | Read only |
| 3 | -wx | Write and execute (rare) |
| 2 | -w- | Write only (rare) |
| 1 | --x | Execute only |
| 0 | --- | Nothing |
A fourth digit in front sets the special bits: 4 for setuid, 2 for setgid and 1 for the sticky bit. So 4755 is 755 with setuid, and 1777 is the mode of /tmp.
A worked example
Say you want a config file that you can edit, your team can read and nobody else can see. That’s read and write for the owner (4 + 2 = 6), read for the group (4) and nothing for others (0): chmod 640 app.conf. In letters, rw-r-----. Tick those boxes above and the calculator gives the same answer.
Symbolic changes
Instead of setting the whole mode, you can change parts of it. A symbolic change has who (u owner, g group, o others, a all), an operator (+ add, - remove, = set exactly) and the permissions. Join several with commas.
chmod u+x script.sh: let the owner run it.chmod go-w file: stop the group and others changing it.chmod a=r,u+w file: everyone can read, only the owner can write (644).chmod g=u file: give the group the same permissions as the owner.chmod -R a+X folder: capital X adds execute only to directories and files that someone can already execute, so you can fix a whole tree without making every file runnable.
Type a change into “Change it with chmod syntax” to see what it does to the current mode before you run it.
Common modes
| Octal | Symbolic | Typical use |
|---|---|---|
644 | rw-r--r-- | Normal files: you can edit, everyone can read |
755 | rwxr-xr-x | Scripts, programs and most directories |
600 | rw------- | Private files: SSH keys, .env, passwords |
700 | rwx------ | Private directories, like ~/.ssh |
664 | rw-rw-r-- | Shared files a group can edit |
775 | rwxrwxr-x | Shared directories a group can write to |
400 | r-------- | Read-only for you, hidden from others |
2775 | rwxrwsr-x | Team directory: new files keep its group |
1777 | rwxrwxrwt | Shared drop directory, like /tmp |
777 | rwxrwxrwx | Everyone can do everything. Avoid |
Common mistakes
- Reaching for 777. If a web app can’t write to a folder, give the folder to the app’s user or group with
chownand use 755 or 775. - Using -R with one mode for everything.
chmod -R 755makes every file executable. Set directories and files separately withfind, as shown when you pick Directory above. - Forgetting execute on directories. With 644 on a directory, people can see the file names but can’t open any of them.
- Leaving private keys readable. SSH and many other tools refuse keys that aren’t 600.
Octal is just base 8: the number base converter shows the same mode in binary, where each bit is one switch. Scheduling a script once its permissions are right? Try the cron expression explainer.
Questions people ask
What does chmod 755 mean?
rwxr-xr-x.What’s the difference between 644 and 755?
rw-r--r--) lets the owner edit and everyone read, which suits ordinary files like web pages and config. 755 adds execute for everyone, which a script needs to run and a directory needs before anyone can open it.Is chmod 777 safe?
chown and use 755 or 775 instead.What do the s and t letters mean?
s in the owner’s execute spot is setuid: the program runs as its owner, which is how passwd can update a root-owned file. s in the group spot is setgid. t in the others spot is the sticky bit, which stops people deleting each other’s files in shared directories like /tmp. A capital S or T means the bit is set but execute isn’t.What permissions should SSH keys have?
~/.ssh directory 700. SSH refuses to use a private key that other people can read. Public keys and authorized_keys can be 644, though 600 also works.Why does chmod +x only change some bits for me?
chmod +w only adds write for the owner. Write a+x or ugo+w to be explicit. This calculator treats a missing letter as a and says so.