The umask utility is used to control the file-creation mode mask, which determines the initial value of file permission bits for newly created files. The behaviour of this utility is standardized by POSIX and described in the POSIX Programmer's Manual. Because umask affects the current shell execution environment, it is usually implemented as built-in command of a shell.
1 Meaning of the mode mask
The mode mask contains the permission bits that should not be set on a newly created file, hence it is the logical complement of the permission bits set on a newly created file. If some bit in the mask is set to 1, the corresponding permission for the newly created file will be disabled. Hence the mask acts as a filter to strip away permission bits and helps with setting default access to files.
The resulting value for permission bits to be set on a newly created file is calculated using bitwise material nonimplication (also known as abjunction), which can be expressed in logical notation:
R: (D & (~M))
For example, let us assume that the file-creation mode mask is 027. Here the bitwise representation of each digit represents:
- 0 stands for the user permission bits not set on a newly created file
- 2 stands for the group permission bits not set on a newly created file
- 7 stands for the other permission bits not set on a newly created file
With the information provided by the table below this means that for a newly created file, for example owned by User1 user and Group1 group, User1 has all the possible permissions (octal value 7) for the newly created file, other users of the Group1 group do not have write permissions (octal value 5), and any other user does not have any permissions (octal value 0) to the newly created file. So with the 027 mask taken for this example, files will be created with 750 permissions.
|3||011||write and execute|
|5||101||read and execute|
|6||110||read and write|
|7||111||read, write and execute|
2 Display the current mask value
To display the current mask, simply invoke umask without specifying any arguments. The default output style depends on implementation, but it is usually octal:
When the -S option, standardized by POSIX, is used, the mask will be displayed using symbolic notation. However, the symbolic notation value will always be the logical complement of the octal value, i.e. the permission bits to be set on the newly created file:
$ umask -S
3 Set the mask value
You can set the umask value through the umask command. The string specifying the mode mask follows the same syntactic rules as the mode argument of chmod.
Most Linux distributions set a default value of 022, including Parabola or 002 in /etc/profile or in the default shell configuration files, e.g. /etc/bashrc.
If you need to set a different value, you can either directly edit such file, thus affecting all users, or call umask from your shell's user configuration file, e.g. ~/.bashrc to only change your umask, however these changes will only take effect after the next login. To change your umask during your current session only, simply run umask and type your desired value. For example, running umask 077 will give you read and write permissions for new files, and read, write and execute permissions for new folders.