umask: How the Default Mode of a New File Is Decided
The umask is a mask of permission bits removed from every file and directory a process creates. It only ever subtracts, it applies at creation time and never to chmod, and the usual value of 022 is what makes new files 644 and new directories 755.
At a glance
- What it does
- Clears the bits it names from the mode a process requests when creating a file or directory
- Base mode for a new file
- 666. This is why a newly created file is never executable
- Base mode for a new directory
- 777
- umask 022
- New files 644, new directories 755. The common default
- umask 002
- New files 664, new directories 775. Used with per-user private groups
- umask 077
- New files 600, new directories 700. Nothing is readable by anyone else
- Applies to chmod
- No. chmod with an explicit mode ignores the umask entirely
In detail
A process asks the kernel for a mode when it creates a file, and the kernel removes whatever bits the umask names before writing it to disk. The mask is subtractive only: a umask can never grant a permission that the creating program did not ask for, which is why a umask of 000 still does not produce executable files.
That last point catches people. Programs that create ordinary files request 666, not 777, so the execute bits are never in play and a new file arrives without them regardless of the mask. Directories are created with a request of 777, which is why the same umask of 022 yields 755 for a directory and 644 for a file.
The value is inherited by every child process, which is what makes it a useful default and a poor security control. Setting it in a shell profile affects that shell and everything launched from it, but a service started by the init system uses whatever the service manager sets, so tightening your own shell does nothing for a daemon writing files at three in the morning.
022 is the usual system default and gives group and world read access to everything you create. 077 is the right choice on a shared machine where nothing you write should be visible to other accounts. 002 goes with the private group scheme, where every user has a personal primary group so the group write bit is harmless until a file is deliberately given a shared group.
It applies only at creation. Copying a file with cp creates a new file, so the umask applies and the original mode is lost unless you pass -p. Restoring from an archive that stores permissions replays the stored modes instead. This is the mechanism behind most cases of a secret file arriving in production at 644.
Examples
new file 666 & ~022 = 644 -rw-r--r--
new directory 777 & ~022 = 755 drwxr-xr-x
new file 666 & ~077 = 600 -rw-------
new directory 777 & ~077 = 700 drwx------
new file 666 & ~002 = 664 -rw-rw-r--
new directory 777 & ~002 = 775 drwxrwxr-x$ umask
0022
$ umask -S
u=rwx,g=rx,o=rx
$ umask 077
$ touch private.txt
$ ls -l private.txt
-rw------- 1 alice alice 0 private.txt$ ls -l secret.env
-rw------- 1 alice alice secret.env
$ cp secret.env /srv/app/
$ ls -l /srv/app/secret.env
-rw-r--r-- 1 alice alice secret.env # umask applied
$ cp -p secret.env /srv/app/
-rw------- 1 alice alice secret.env # mode preservedThings that catch people out
- A umask cannot add permissions. Setting it to 000 does not make new files executable, because the creating program never requests the execute bit for a regular file.
- Services do not inherit your shell's umask. Set it in the service definition if a daemon is writing files with the wrong mode, not in a login profile.
- cp without -p applies the umask to the copy, so a 600 secret becomes 644 in its new location. Use install -m to set the mode explicitly instead.
- The umask is per process and inherited at fork. Changing it in one terminal has no effect on anything already running elsewhere.
Try it: Chmod Calculator
Tick the nine permission boxes, or type an octal or symbolic value, and get the three digit and four digit octal, the ls -l rendering and the exact chmod command back.
Chmod Calculator
LOCAL · Browser-OnlyCalculate Unix file permissions with an interactive chmod builder
Permission Matrix
| Entity | Read (4) | Write (2) | Execute (1) | Octal | Symbolic |
|---|---|---|---|---|---|
| Owner (u) | 7 | rwx | |||
| Group (g) | 5 | r-x | |||
| Others (o) | 5 | r-x |
Special Bits
Advanced permission flags for executables and directories
Generated Permissions
755rwxr-xr-x-rwxr-xr-xchmod 755 filenamechmod u=rwx,g=rx,o=rx filenamePermission Breakdown
Owner (u)
7rwx- - Read files / list directory contents
- - Write / modify / delete
- - Execute / traverse directory
Group (g)
5r-x- - Read files / list directory contents
- - Execute / traverse directory
Others (o)
5r-x- - Read files / list directory contents
- - Execute / traverse directory
Common Permissions Reference
| Octal | Symbolic | Meaning |
|---|---|---|
777 | rwxrwxrwx | Full access for everyone |
755 | rwxr-xr-x | Owner full, others read and execute |
750 | rwxr-x--- | Owner full, group read and execute |
700 | rwx------ | Owner full access only |
666 | rw-rw-rw- | Read and write for everyone |
644 | rw-r--r-- | Owner read/write, others read only |
600 | rw------- | Owner read/write only |
555 | r-xr-xr-x | Read and execute for everyone |
444 | r--r--r-- | Read only for everyone |
400 | r-------- | Owner read only |
Related File permissions
All file permissions →chmod 644: The Default File Mode and When to Change It
644 gives the owner read and write and everyone else read only. Why it is the default for files under a 022 umask, and the cases where it is wrong.
chmod 755: The Standard Mode for Directories and Programs
755 gives the owner full control and everyone else read and execute. Why it is right for directories and executables and wrong for ordinary data files.
chmod 600: Owner Only Read and Write for Keys and Secrets
600 gives the owner read and write and nobody else anything. The mode SSH, PostgreSQL and other tools insist on for files holding credentials.
chmod 664: Group Writable Files for Shared Work
664 lets the owner and the group edit a file while everyone else can only read it. The default on systems using a 002 umask and per-user groups.
Symbolic chmod Notation: u, g, o, a With Plus, Minus and Equals
Symbolic mode changes one class at a time instead of setting all nine bits. How plus, minus and equals differ, and what the capital X form is for.
Four Digit Octal Modes Against Three Digit Ones
The leading digit of a four digit mode carries setuid, setgid and the sticky bit. When 0755 and 755 differ, and what GNU chmod does to directories.
Tools for this
- Chmod Calculator Free online chmod calculator - calculate Unix file permissions with an interactive chmod builder