umask, as the man page says, stands for User file creation mask which is used for determining the default permission for a new file creation. The new file creation could either be a file creation through a normal process or a file copy. umask command is a shell built-in meaning it is an internal command.
The three file permission attributes are read, write and execute. These 3 are mapped to octal values as shown below:read - 4
write - 2
execute - 1
In UNIX, the default file creation value is 666. 6 is 4+2(read + write). Permission 666 means 6 for the User, 6 for the group and 6 for others. Hence, a new file creation by default is meant to have read and write permission for User, group and others. This is the place where the umask comes into the picture. It is a kind of filter wherein we can choose to retain or block some of the default permissions from being applied on the file.
Say, the umask value is 0022. umask is by default displayed in Octal form, and hence the first 0 in the umask value is the indication for octal value. So, the actual umask is 022. This value together with the default file value(666) decides the final permission to be given to the file.
Assume we create a file say "file1". The permissions given for this file will be the result coming from the substraction of the umask from the default value :
umask : 022
Result : 644
644 is the permission to be given on the file "file1". 644 means read and write for the User(644), read only for the group(644) and others(644).
$ touch file1 $ ls -l file1 -rw-r--r-- 1 guru None 0 Apr 22 18:53 file1The same rule is applied while creating a directory as well.
Let us have some FAQ:
1. What is umask?
umask is a number which defines the default permissions which are not to be given on a file. A umask of 022 means not to give the write permission to the group(022) and others(022) by default.
2. How to find out the umask value?
$ umask 0022The option -S gives in more readable format.
$ umask -S u=rwx,g=rx,o=rxThis means umask, at the max, allows all permissions for the user, read and execute alone for the group and others.
3. How to set the umask value?
$ umask 033The same can be given in this below form as well:
$ umask u=rwx,g=r,o=r4. How to set this umask permanently for a user?
To set this value permanently for a user, it has to be put in the appropriate profile file which depends on the default shell of the user.
5. Does this umask come into picture while copying a file?
Yes, during the copying of a file also umask is used. The new file created using the copy command also follows the same umask rules as above.
6. Can we retain the permission of the source file while copying a file?
Yes, we can retain the permission of the source file using the cp command. As shown in the example 4 of one of our earlier articles on cp command, the mode of the source file can be retained.
7. Who can set the umask value?
It can be set by the root user which will be applicable across the system. Also, a given user can override the umask value by having his own setting in his/her profile file.