umask
umask
Within the Linux operating system, 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
-S | Accept a symbolic representation of a mask, or return one. |
mask | If a valid mask is specified, the umask is set to this value. If no mask is specified, the current umask value is returned. |
And umask
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 that 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. So it's not really subtraction, but it's a similar concept, and thinking of it as subtraction can help to understand it.
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.
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).
Examples
To view your system's current umask value, enter the command:
umask
This will return your system's umask as a four-digit octal number, for example:
0002
The first zero is a special permission digit and can be ignored; for our purposes, 0002 is the same as 002.
To view this as a symbolic representation, use the -S flag:
umask -S
Which will return the same value symbolically, for example:
u=rwx,g=rwx,o=rx
The u stands for user, g stands for group, and o stands for other. So if we create a new file, it will have the default permissions 664, which is 666 (the default permissions for files) masked by 002 (our umask value).