umask
What is umask?
On Unix, umask (short for “user file creation mask”) is a setting you can use to control the default permissions of new files and dirctories.
For example, running umask from your terminal should give you either 022 or 0002.
Some systems like Ubuntu show 4 digits whereas others like macOS tend to show 3 digits. In any case, you can ignore the first number of the 4-digit version or assume the three-digit version always has a 0 at the start too.
Understanding base permissions for files and directories
By default, files and directories you create via commands like touch or mkdir will probably have some base permissions which will most likely be:
- Files: 666
- Owner: 6 (Read, Write)
- Group: 6 (Read, Write)
- Others: 6 (Read, Write)
- Directories: 777
- Owner: 7 (Read, Write, Execute)
- Group: 7 (Read, Write, Execute)
- Others: 7 (Read, Write, Execute)
These base permissions are chosen by the program creating the file / directory (e.g., the touch or mkdir programs) when they use system calls like open() or mkdir(). The above base permissions of 666 (for files) and 777 (for directories) are almost always the same for all systems.
Understanding how umask affects permissions of new files and directories
A umask setting of 022 means that if you create a new file, the file’s permissions will be 666 (base permissions) – 022 (umask settings) = 644 (final permissions) which is:
- Owner: 6 (Read, Write)
- Group: 4 (Read)
- Others: 4 (Read)
The umask setting of 022 means that if you create a new directory, the directory’s permissions will be 755 because 777 (base permissions for directories) – 022 (your umask setting) = 755 (final permissions):
- Owner: 7 (Read, Write, Execute)
- Group 5 (Read, Execute)
- Others 5 (Read, Execute)
Common umask values
| umask | Default file permissions | Default directory permissions | Notes |
|---|---|---|---|
| 022 | 644 (rw-r–r–) | 755 (rwxr-xr-x) | Default on most systems |
| 002 | 664 (rw-rw-r–) | 775 (rwxrwxr-x) | Group members can write |
| 077 | 600 (rw——-) | 700 (rwx——) | Only owner has access |