umask and file permissions simplified

In this article, we would cover how file permissions depend on umask. When we create a file or directory, the permissions which will be assigned depend on the umask(i.e. user mask).

When we write, 777 as permissions for an object then it means all the permissions (i.e. read, write and execute) are granted. The number here represents file permissions.

for read its 4,
write -> 2 and,
execute -> 1.

So, if we file has got read and write permissions granted, then in letters it would be –> rw-

and, while in numbers it would be 4+2=6

The default value of umask is different for each user. To find the default value for a normal user, open a terminal and issue the following –

umask

This would return with –

0002

But, when we do the same for a root user –

sudo -i
umask

Output –

0022

We need to ignore the first zero. Also, user mask uses octal values to set file permissions. After ignoring the first zero, the next digit sets the file permissions for a user. Moving to its right, it would set permissions for a group. And, digit at extreme right helps us set file permissions for others.

umask basically masks the permissions for a file or directory. But, if we consider the full permission set with umask as 000. Then, we get 777 for directory and 666 for files.

Now, consider the umask values for a normal user – 002. So, for a directory it would be 775 and a file inherits 664.

775 would suggest that user and groups have got all the three permissions (i.e. read, write and execute) granted. Whereas, for others its just read and execute.

Understand how it works with the help of an example. We would consider only file permissions. Same rules would apply for directories as well.

Create a directory using touch command-line utility.

touch file_example

Use ls to see file details.

ls -l

It would return with –

-rw-rw-r-- 1 techpiezo techpiezo 0 Apr 22 22:11 file_example

The file permission for file_example is 664. By default umask for a user in Ubuntu is 002. Now, if we want file permission to be 644. Then, this can be done using chmod command-line utility. But, we can also achieve similar results using umask. In a terminal, issue the following –

umask 022

Thereafter, create a new file using touch command-line utility –

touch ufile_ex

and, do ls -l. It would return with –

-rw-rw-r-- 1 techpiezo techpiezo 0 Apr 22 22:11 file_example
-rw-r--r-- 1 techpiezo techpiezo 0 Apr 22 22:14 ufile_ex

see the difference between file permissions for file_example and ufile_ex.

But, once we close/exit the shell. umask reverts to default values. To make it permanent we need to append ~/.bashrc configuration.

nano ~/.bashrc

And, append the file with the following –

umask xxx

where, replace x with appropriate values. For instance, if we want umask to have 022. Then, it should be –

umask 022

To make changes in the current shell –

source ~/.bashrc

Now, create a new file yourself and see whether things work or not.

In conclusion, we have discussed how file permissions depend on umask. In next article, we would discuss the difference between umask and chmod with examples.

Similar Posts