Índice/Index

jueves, 21 de junio de 2012

How to calculate permissions from umask

Umask (user mask) is a command that defines what default permissions will be established when a file is created by a determined process or user.

First the basis: permissions are represented like this in Unix systems: rwx-rwx-rwx.

Where r - Read
w - Write
x - Execute

And where the first group of rwx belongs to the owner, the second to the group and the third to other users.

Example: rwx-r-x-r-x

In this case the permissions are: the owner has rights to read, write and execute. The group to read and execute. Other users to read and execute.

So now lets see how to obtain the permissions from a umask:

Example: if our umask is of 022, the default permissions for directories will be: 755 (drwx-r-x-r-x) and for files 644 (rw-r---r--).

And how did we obtain this? Now lets see how.
Before making any calculation:

The initial permissions for directories are 777.
The initial permissions for files are 666.

In the next steps will see how this values are applied to obtain the default permissions that the umask will provide using the example of the 022 umask.

First, we make a binary NOT to the umask 022:

Lets start with the first 2 (other users):

NOT 010=101=5

The NOT to the next 2 will also result in a 5 (group).

For the 0 (Owner):

NOT 000=111=7

So the result is 755.

Now that we have the NOT umask will get the default permissions for directories.
We just need to apply an AND binary operation with the 755 that we got before with the initial permissions for directories (777):

Lets start with other users:

101(5) AND 111(7) = 101(5)

Group:

101(5) AND 111(7) = 101 (5)

Owner:

111(7) AND 111(7) = 111 (7)

So the default permissions for directories will be 755 (rwx-r-x-r-x).

And now lets do the same for the files. For files initial permissions are 666. So the AND will be with 755 and 666.

Other users:

101(5) AND 110(6) = 100(4)

Group:

101(5) AND 110(6) = 100(4)

Owner:

111(7) AND 110(6) = 110(6)

So the default permissions for the files will be 644 (rw--r---r--).

Summary: with a umask of 022 we obtain 755 permission for directories and 644 permission for files.

Hope this post helps, if you have any doubt or you think something is wrong just comment. Or also if you think another example is necessary.

No hay comentarios:

Publicar un comentario