19

I am completely confused between umask and chmod. Both are used to give permissions to the files. But where exactly is the difference and when to use them.

I have read the online documentation but both are looking same to me.

umask: umask is used to set default file permissions. These permissions will be used to all subsequent files during their creation. chmod : used to change file and directory permissions.

As per my understanding if for example file test.doc is created.

By default unix gives the file 022 umask code.

Now when I change it to chmod 666 test.doc I can change the permission level of this file.

Now what if i use umask 666 for the same file.

What difference it happens when I use chmod 666 and umask 666

4

4 Answers 4

15

The difference is that umask entails only new files. As you stated, umask sets the default permissions that a file/directory will have on creation time, but afterwards umask doesn't affect them anymore.

chmod, however, needs the file be created prior to be run.

Therefore, if you run umask, it will have no effect at all on existing files.

2
  • 2
    You can't use umask on a file, it doesn't have a file argument.
    – DavidPostill
    Jan 22, 2016 at 20:37
  • Right, rewrote that part.
    – nKn
    Jan 22, 2016 at 20:38
10

umask is very different from chmod, actually.

  1. An important difference hasn't been mentioned yet: chmod sets, but umask clears (restricts) permission bits. That's why it's called "mask" (as in "bitmask").

  2. As David wrote, umask is a (process-level) configuration setting, so it's not applied on any specific files (as opposed to chmod).

  3. Which brings us to another important point: umask is not limited to files. It's also applied when creating directories. (See also e.g. this answer.)

  4. Also important, that the chmod command itself is not affected by the currently configured umask.

Now, to your example of what would umask 666 do:

It will tell the current process (for example your shell) that any new filesystem objects should be created with the R+W bits (4 + 2 = 6) removed (from whatever permissions implicitly or explicitly requested upon creation). (So, 666 is not a very practical value, since it only allows the X (execute) bits to be set, but for unreadable files...)

E.g.:

$ touch foo; ls -la foo
-rw-r--r-- ... foo   <-- default permissions

$ umask 666
$ touch bar; ls -la bar
---------- ... bar   <-- perms. after the new umask (restriction) is set
$ mkdir foodir; ls -la | grep foodir
d--x--x--x ... dir   <-- not very practical for dirs, either

$ chmod 777 bar; ls -la bar
-rwxrwxrwx .... bar* <-- chmod happily ignores the current umask

$ umask 022
$ touch bong; ls -la bong
-rw-r--r-- ... bong  <-- (so, it seems this was the default umask)

$ chmod 666 bong; ls -la bong
-rw-rw-rw- ... bong  <-- no surprise of any kind here
3

UMASK = chmod 777 - umask permissions

umask 022 => 777 - 022 => chmod 755
umask 077 => 777 - 077 => chmod 700
umask 002 => 777 - 002 => chmod 775
umask 007 => 777 - 007 => chmod 770
umask 027 => 777 - 027 => chmod 750
umask 177 => 777 - 177 => chmod 600

Something like that.

0

The default permission for directories/files is umask 022 then, directories are set by default with permission 755 (drwx-rw-rw) and files are set with 644 (-rw-r--r--). Basically, you subtract the octal value of the permission you want from 777 for a directory and from 666 for a file. So from the example above:

(directory) umask 022 => 777 - 022 = 755

(file) umask 022 => 666 - 022 = 644

You can find the result of the umask octal command in the source when using umask, depending on the octal and if it is a file or a directory.

Source

However, if you chmod 022 <file> for a directory, you should get this: d----w--w- which would not make much sense.

Underneath, you will find a great description of octal value you can retrieve here by Somnath Muluk with great sources as well.

N   Description                      ls   binary    
0   No permissions at all            ---  000
1   Only execute                     --x  001
2   Only write                       -w-  010
3   Write and execute                -wx  011
4   Only read                        r--  100
5   Read and execute                 r-x  101
6   Read and write                   rw-  110
7   Read, write, and execute         rwx  111

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .