The umask command provides the facility to manage the privileges for the directories and files. It seems to be complicated, but actually, It is just like the chmod command. The umask command is applied to change the default privileges on the folders and files that are newly created, while chmod is applied to the files and folders that already exist.
This write-up will explore the usage of the umask command in Linux. The content for the post is:
- How to Use the umask Command in Linux?
- Setup and Use the Default umask value
- Calculation of the umask Value For a File
- Calculation of the umask Value For a Directory
Let’s get into the basics of the umask command.
How to Use the umask Command in Linux?
The umask is a utility represented in the number of bits used to set the privileges for newly created files. The umask represents the user-file-creation mod mask. It can be used to manage the default permissions. To check the umask value, just type the umask in the terminal and hit enter button:
$ umask
In the above output, the first 0 represents the sticky bit (special permission). The remaining three bits are the representation of the octal values. These three octal values (002) represent permissions for the owner, group members, and everyone else.
Permission Table With Octal and Binary Values
The table for permissions with Octal and Binary values is given below:
Permissions | Octal values | Binary For Octal Values |
---|---|---|
No permissions. | 0 | 000 |
Execute permission (-x) | 1 | 001 |
Write permission (-w) | 2 | 010 |
Execute and write permission (-w, -x) | 3 | 011 |
Read permission only (-r) | 4 | 100 |
Read and execute permission (-r, -x) | 5 | 101 |
Read and write permission (-r, -w) | 6 | 110 |
Read, write and execute permissions (-r, -w, -x) | 7 | 111 |
Setup and Use the Default umask value
Users can also change the default umask value by simply typing the umask and then the value in the terminal:
$ umask 444
Let’s check the updated umask value:
$ umask
The umask value has been set to 0444.
Calculation of the umask Value For a File
As per the definition of the umask command, umask permission will be applied to files and directories that are newly created. By default, all newly created files have the permission of 666. So to calculate the umask value for the file, subtract it from the umask value. Let’s calculate the mask value for the file.
Full permissions = file permission - umask permission #Formula to Calculate
Full permissions = 666-444 =222
If we check the binary for each octal number (2), 2 represents 010 which means only write permission will be given to the owner, group members, and everyone else.
To verify this concept, let’s check the permission by creating a new file in Linux:
$ touch abc.txt
$ ls -l abc.txt
Only write permission is available for all users.
Note: Linux doesn’t give executable permission to the files by default. If in any case, the execute permission bit is 1, then its octal value is promoted by one (1 to 2, 2 to 3).
Calculation of the umask Value For a Directory
When a new directory is created, 777 permission is given to the directory. To calculate the umask value for the directory, do the same step as we did for the file.
Full permissions = Directory permission - umask permission #Formula to Calculate
Full permissions = 777-444 =333
If we check the binary for each octal number (3), 2 represents 011 which means write and execute permission will be given to the owner, group members, and everyone else.
To verify this concept, check the permission by creating a new directory in Linux:
$ ls -ld abc
In “d-wx-wx-wx”, d stands for the directory, and wx means to read and write permission is given.
This is all about the umask command.
Conclusion
In Linux, the umask command is a utility to change the privileges of the files and directories. Users can change the umask value to change the permission. This post has briefly demonstrated the usage of the umask command in Linux.