Umask in Linux

linux

What is it?

Umask in Linux is a Linux command that is used to assign the default file permission sets that will be assigned when a file or a directory will be created by a user.
For example, all files created by a specific user created a file, by default a specific file permission gets assigned to it such as r w – r – – r – – . This is because of the umask value that is set for the user.

How to find the umask set for a user?

Running the below command helps to find the umask value set for a user
[root@localhost ~]# umask
0022

Where is the umask value configured?

The umask value for all users in general is configured in either:
/etc/profile
/etc/bashrc
This value is overridden by values configures in below file in user’s home folder:
~/.bash_profile
~/.bashrc

How to calculate the permissions system will be assigning to files and folders based on the umask value?

One umask value configured determines the permissions that will be set for both files and directories created by a user.
If the umask value is 002, the newly created file’s permission will be the obtained by reducing each bit of 666 by each bit of 002. That is:
6-0 = 6
6-0 = 6
6-2 = 4

Therefore the default file permission will be r w – r w – r- –

If the umask value is 002, the newly created folder’s permission will be the obtained by reducing each bit of 777 by each bit of 002. That is:
7-0 = 7
7-0 = 7
7-2 = 5

Therefore the default file permission will be r w x r w x r- x