9

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

  • 1
    umask sets an environment variable which automatically sets file permissions for newly created files. chmod changes the permissions of existing files. – DavidPostill Jan 22 '16 at 20:40
  • @DavidPostill. No, umaskdoes not set "an environment variable" What it does do is set the file mode creation mask of the current shell execution environment. – fpmurphy Mar 11 '18 at 5:20
  • @fpmurphy1 Thanks for the clarification :) – DavidPostill Mar 11 '18 at 8:25
10

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
    You can't use umask on a file, it doesn't have a file argument. – DavidPostill Jan 22 '16 at 20:37
  • Right, rewrote that part. – nKn Jan 22 '16 at 20:38
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.