Who a permission applies to
Every file and directory on a Unix-like system (Linux, macOS, the BSDs) has an owner, a group, and a set of permission bits. The bits come in three sets of three. The first set applies to the user who owns the file, the second to members of the file's group, and the third to others, meaning everyone else. When you access a file, the system picks exactly one set: the owner set if you own it, otherwise the group set if you are in its group, otherwise the others set. The root account bypasses most of these checks.
What r, w and x mean
The three letters mean different things for files and directories, and that difference causes a lot of confusion.
- r (read)
- On a file: read its contents. On a directory: list the names inside it.
- w (write)
- On a file: change its contents. On a directory: create, delete, or rename entries inside it. Deleting a file depends on the directory's w bit, not the file's.
- x (execute)
- On a file: run it as a program or script. On a directory: enter it and reach the entries inside by name. Without x on a directory, r alone lets you see names but not open the files.
Numeric (octal) notation
Each set of three bits is one octal digit, with read worth 4, write worth 2, and execute worth 1. You add the values you want: read plus write is 6, and read plus execute is 5. A mode such as 754 is therefore three digits for user, group, and others. Octal is base 8; the number bases page covers how it relates to binary. Every possible digit:
| Digit | Binary | Permissions |
|---|---|---|
| 0 | 000 | --- |
| 1 | 001 | --x |
| 2 | 010 | -w- |
| 3 | 011 | -wx |
| 4 | 100 | r-- |
| 5 | 101 | r-x |
| 6 | 110 | rw- |
| 7 | 111 | rwx |
Symbolic notation
Symbolic mode changes bits relative to what is already set, which is handy when you only want to adjust one thing. It has three parts: who (u, g, o, or a for all), an operator (+ to add, - to remove, = to set exactly), and the letters.
chmod u+x script.shadds execute for the owner. A file at 644 becomes 744, shown as-rwxr--r--.chmod go-w sharedremoves write from group and others. A file at 775 becomes 755.chmod u=rw,g=r,o= notes.txtsets the exact mode 640, shown as-rw-r-----.chmod 600 id_ed25519uses numeric mode to replace all bits at once.
Common modes
The strings below were produced by Python's stat.filemode, which formats a mode the same way ls -l does. The leading character is - for a regular file and d for a directory.
| Mode | As a file | As a directory | Typical use |
|---|---|---|---|
644 | -rw-r--r-- | drw-r--r-- | Normal files: owner edits, everyone reads |
755 | -rwxr-xr-x | drwxr-xr-x | Programs, scripts, and most directories |
600 | -rw------- | drw------- | Private files such as SSH keys |
700 | -rwx------ | drwx------ | Private directories and scripts |
664 | -rw-rw-r-- | drw-rw-r-- | Files shared for editing within a group |
775 | -rwxrwxr-x | drwxrwxr-x | Directories shared within a group |
777 | -rwxrwxrwx | drwxrwxrwx | Anyone can do anything; rarely correct |
400 | -r-------- | dr-------- | Read-only for the owner, such as some key files |
umask and default permissions
New files do not get a fixed mode. A program asks for a starting mode, usually 666 for files and 777 for directories, and the process's umask removes bits from that request. Each 1 bit in the umask clears the matching permission bit. It is a mask, not a subtraction, although with typical values the result looks the same. Run umask in a shell to see your current value.
| umask | New file (from 666) | New directory (from 777) |
|---|---|---|
022 | 644 -rw-r--r-- | 755 drwxr-xr-x |
002 | 664 -rw-rw-r-- | 775 drwxrwxr-x |
027 | 640 -rw-r----- | 750 drwxr-x--- |
077 | 600 -rw------- | 700 drwx------ |
With the common umask 022, files start as 644 and directories as 755: group and others can read but not write. Note that a umask cannot add execute to a new file; programs that create files normally do not request it.
setuid, setgid and the sticky bit
A fourth, leading octal digit holds three special bits: setuid is 4, setgid is 2, and sticky is 1. The setuid bit on an executable makes it run with the file owner's privileges. The setgid bit does the same for the group; on a directory it makes new entries inherit the directory's group. The sticky bit on a directory lets users delete only their own files inside it, which is why shared temporary directories such as /tmp use mode 1777.
| Mode | As a file | As a directory |
|---|---|---|
4755 | -rwsr-xr-x | drwsr-xr-x |
2755 | -rwxr-sr-x | drwxr-sr-x |
2775 | -rwxrwsr-x | drwxrwsr-x |
1777 | -rwxrwxrwt | drwxrwxrwt |
4644 | -rwSr--r-- | drwSr--r-- |
These bits replace the x in the listing. A lowercase s or t means the execute bit is also set. An uppercase S, as in 4644, means the special bit is set but execute is not, which usually signals a mistake.
Reading an ls -l line, field by field
-rwxr-x--- 1 alice devs 2048 Mar 3 09:15 deploy.sh
-is the file type: a regular file. Adwould mean a directory and anla symbolic link.rwxis the user set: alice can read, write, and run it.r-xis the group set: members of devs can read and run it but not change it.---is the others set: everyone else gets nothing. Together that is mode 750.1is the hard link count,alicethe owner, anddevsthe group.2048is the size in bytes, followed by the modification time and the name.
A trailing + or @ after the permission string, on some systems, signals extra access control lists or attributes beyond these basic bits.