Open In App

Umask command in Linux with examples

The umask command in Linux is used to set default permissions for files or directories the user creates.

How does the umask command work?

How to calculate umask value?

Syntax:



$umask

[The above command will give the following output]

umask command in Linux without parameters (output)

pratyay@pratyay-ROG-Strix-G531GT:~/Study/Linux/CommandTrials/umask$ umask
0002

For a better understanding of umask working, we need to understand octal mode security settings. The three rwx permissions (Read-Write-Execute) values are converted into three-bit binary values and represented by a single octal value as shown in the following table:



Permissions Octal Value Binary Value Description
0 000 No permission
–x 1 001 only permission to execute 
-w- 2 010 only permission to write
-wx 3 011 permission to write and execute
r– 4 100 only permission to read
r-x 5 101 permission to read and execute
rw- 6 110 permission to read and write
rwx 7 111 permission to do all three, i.e. read, write and execute

Simplification:

Let’s understand the above table with an example: Let’s explain the previous output we got using umask, 0002

How to set and update the default umask value?

We can set and update the default umask value using the command umask followed by a parameter, which should be an integer ranging from 000-777. The syntax for updating the umask value is the same as setting the umask value.

Setting the umask value:

We can use the umask command to set the default permissions with which the files/directories will be created.

Syntax

$umask 543

umask command in Linux terminal (Setting default umask value)

How to calculate umask values for files and directories?

Here, when we execute the command, the values are not directly allocated as 5 for the owner, 4 for the group members and 3 for the others, but the value we pass as an argument is subtracted from the max/full permission set. There are two full permission sets:

Note: The files cannot be given execution permissions by default as it can cause a security concern, and Linux systems are pretty much known for their amazing security, so that wouldn’t be good.

So, once we have set the umask value to 543, let’s see what happens when we make a directory(7-7-7) and a file(6-6-6)

Making a directory:

Making a directory with custom set umask

Making a file:

Making a file using custom set umask

Trying to open the file without access

Opening file as admin

 

 

 

 

 

 

So, in this way, it is possible to use umask command in order to set default permissions for files and directories. It should be noted that the default permissions for files and directories are different as files do not provide the option to execute by default.

What is the difference between chmod and umask?

Thus, we need umask command in order to set the default access permissions for files and directories which will be created in the future, and we need the chmod command in order to change the access permissions for files that have been already created and are present in the system.


Article Tags :