16

If I understand correctly file permissions have an associated 3 digit number which specify read/write/execute permission.

The umask value is a default 'mask' which is subtracted from the default value. So for a umask value of 0022 the default value for something that would be 777 would become 755?

Is this correct and if so, what is the first 0 in the umask value?

muru
  • 72,889
  • Did you mean to say the associated 4 digit number which specifies the stickybit/user/group/other permissions? – thrig Mar 18 '17 at 15:10
  • @thrig no I was not aware of the stickybit value? Maybe my understanding of how this works is completely off. – Philip Kirkbride Mar 18 '17 at 15:10
  • A sticky is used when multiple users need access to a shared directory. It prevents users from deleting files belonging to other users. Take a look at your /tmp directory for example. Do ls -ld /tmp and you'll see a lowercase 't' appended to the other permissions block representing the sticky. drwxrwxrwt. 10 root root 180 Mar 19 02:10 /tmp – Alxs Mar 18 '17 at 15:47
  • 2
    I'm not clicking the accept as duplicate button because I'm specifically interested in the first digit of the four and feel Alxs answer address this better than the answer about umask calculations. – Philip Kirkbride Mar 18 '17 at 17:13
  • By convention, numbers that are prefixed by 0 are understood to be octal. So, 0022 means 022 octal notation. Since umasks are octal anyway, setting it to 0022 is not necessary. – countermode Mar 18 '17 at 20:25

2 Answers2

21

The first digit 0 is not in use in your example. The umask reads from right to left and trailing zeros are ignored. It can however be used to set special permissions, such as sticky bit, Set GUID, Set UID as shown below.

0755  —- None of the special bits set
1755  —- Sticky bit set
2755  —- SGID bit set
4755  —- SUID bit set

You are correct that a umask of 0022 the will mask a default 777 (directory) permission to become 755 on newly created directories.

The octal numbering works similar to the first three sets: user, group, world/other.

The read/write/execute rwx values are represented in octal form with the corresponding values which can total a maximum of 7:

4 - Read 
2 - Write 
1 - Execute

So for 0755: 0 is ignored. 7 (4+2+1) equals read, write, and execute for the user/owner. And 5 (4+1) equals read and execute for the group, and the remaining 5 (also 4+1) gives read and execute permissions to other/world.

Alxs
  • 2,200
  • 3
  • 21
  • 31
  • 1
    What do you mean by "umask reads from right to left"? It is a numeric value like any other. – VPfB Mar 18 '17 at 15:45
  • 1
    It means that trailing zeros (on the left) are ignored. you can set umask 22 and you'll get the same umask as if you do umask 0022 or umask 00000022 etc. – Alxs Mar 18 '17 at 15:50
  • That "right to left" might be just a wrong term (027 is not read seven-two-zero), but I think that umask cannot be used to set any bits (only to clear them). Further umask has no effect at all on the sticky bit and SUID/SGID. I quoted the Linux system call manpage in my answer where it is stated. – VPfB Mar 18 '17 at 16:46
  • @VPfB That is correct, umask cannot set bits, it can only prevent bits from being set. Re 'right to left' have a look at how base-2 increments. I didn't down vote you btw - I didn't even have down vote privileges when your answer was voted down. – Alxs Mar 18 '17 at 16:55
2

I would say the leading 0 comes from the C language (syntax for octal numbers) and has no other meaning. From man 2 umask, i.e the underlying C library call:

umask() sets the calling process's file mode creation mask (umask) to mask & 0777 (i.e., only the file permission bits of mask are used), and returns the previous value of the mask.


The umask is not simply substracted, but is processed bitwise. Bits set in the umask are cleared in the resulting file mode.

VPfB
  • 801
  • 1
    Please leave a short note when downvoting. Do you see an error? – VPfB Mar 18 '17 at 17:44
  • 1
    I am not the downvoter, but I'll note that the OP's question sounds as if he is referring to the shell umask, not the system call. In bash, the leading zero in the umask builtin is not used to indicate octal processing, which is assumed as stated in the man page: "If mode begins with a digit, it is interpreted as an octal number..." – user4556274 Mar 18 '17 at 18:16
  • 1
    @user4556274 Thank you for your feedback. It looks like I have failed to express my thoughts clearly in English. I wanted to answer the question "What is the first number in umask value?" with stating that the 0 in umask 0XXX is just a habit coming from C and nothing else. To prove that It has no effect I have included quote that all (octal) digits except the last three are ignored. The umask system call documentation is the authoritative source. All umask commands and shell builtins are just "wrappers". – VPfB Mar 18 '17 at 18:57