On Linux all files and directories are created with a default set of permissions. The umask utility 1
allows you to view or set the base or the default permissions given when a new file is created in Linux. 2
it is used by mkdir, touch and other commands that create in new files and directories. 3
Just to be clearer, I'm going to create a new file and a new directory. 4
So mkdir mydir; touch myfile 5
The file and the directory were created with some permissions. 6
It’s 0775 for the directory and 0664 for the file. Now the question is: 7
why these specific permissions and not other ones? 8
And the answer is that umask was used to create the file and the directory with those exact permissions. 9
Let's see how it works. 10
It's considered that the default creation permissions are 0666 for files 11
and 0777 for directories. Take these values as they are. Now the current permissions 12
for the newly created files and directories are calculated in the following manner: 13
the umask value is subtracted from the default permissions,and the result will be the current permissions. 14
To view the current umask value simply run umask without any arguments: umask 15
So its value is 0002 16
Let's substract the umask value from the default values. 17
And we have for directories 0777-umask value 18
- 0002 = 0775 and for the files 0666 - 0002 = 19
0664. 20
So the current permissions will be 0664 for the newly created files and 0775 21
for the newly created directories. 22
These are the exact permissions of the file and the directory I've just created. 23
You see the permissions. 24
Let's suppose that from this point on, I want all newly created files to have 644 as 25
permissions. 26
That means 27
0666 the file's default value- umask = 0644. 28
This is a simple equation and we have to determine the desired umask value. 29
And doing the math will find out that the desired umask value must be 0022. 30
You can change the current session umask value by running umask, followed by the desired value, 31
so yumask and the desired value 0022 32
I'm creating a new file and directory. 33
mkdir mydir1 34
And touch myfile1 35
And the permissions are the expected ones 36
644 for the file and 755 for the directory. 37
Note that changing the umask value has the effect only on the files that will be created from that 38
point on; the permissions of the existing files are not affected. 39
And note that umask affects only the current shell session of the current user. If you want 40
to make the umask value permanent for a specific user, then you should edit the user's shell configuration 41
file such as 42
.bashrc Just open the file with your preferred editor and at the end write umask a whitespace and 43
its desired value like this 44
I'm going to the end by pressing capital G 45
I'm pressing O to enter the insert mode and I'm writing umask and then the new value. 46
If this is how you set umask permanently. 47
Though note that it's not common to change the default umask value.