Rob's web

umask

On Linux and other Unix-like operating systems, new files are created with a default set of permissions. Specifically, a new file's permissions may be restricted in a specific way by applying a permissions "mask" called the umask. The umask command is used to set this mask, or to show you its current value.

Syntax

umask [-S] [mask]

Options

-SAccept a symbolic representation of a mask, or return one.
maskIf a valid mask is specified, the umask is set to this value. If no mask is specified, the current umask value is returned.
# umask
0022

So how does the umask actually work?

The umask masks permissions by restricting them by a certain value.

Essentially, each digit of the umask is "subtracted" from the OS's default value to arrive at the default value you define. It's not really subtraction; technically, the mask is negated (its bitwise compliment is taken) and this value is then applied to the default permissions using a logical AND operation. The result is that the umask tells the operating system which permission bits to "turn off" when it creates a file.

In Linux, the default permissions value is 666 for a regular file, and 777 for a directory. When creating a new file or directory, the kernel takes this default value, "subtracts" the umask value, and gives the new files the resulting permissions.

umask digitDefault file
permissions
Default directory
permissions
0rwrwx
1rwrw
2rrx
3rr
4wwx
5ww
6xx
7(no permission
allowed)
(no permission
allowed)

So if our umask value is 022, then any new files will, by default, have the permissions 644 (666 - 022). Likewise, any new directories will, by default, be created with the permissions 755 (777 - 022).