Umask

From Leo's Notes
Last edited on 30 December 2021, at 01:12.

A umask is used to set the default unix permissions of files. Recall that Unix permissions are defined by 3 octal values denoting the permissions for the owner, group, and other users on the system. A umask is a bit-wise mask that is applied (either against 666 for files or 777 for directories) to generate the default permission octal values.

By default, the umask is typically 022 and results in files with 644 and directories with 755.

Here is a table outlining what each umask digit will result in. The binary value is given which shows how the mask is used to calculate the final permission. Notice that for file permissions, the even values are the same as their previous odd value (because the least significant bit is masked out, as files do not have execute permissions).

Umask Digit File Permission

6 or 110b binary

Directory Permission

7 or 111b binary

0 (000b) Read + Write

110b - 000b = 110b (rw-)

Read + Write + Execute

111b - 000b = 111b (rwx)

1 (001b) Read + Write

110b - (110b & 001b) = 110b (rw-)

Read + Write

111b - (111b & 001b) = 110b (rw-)

2 (010b) Read

110b - (110b & 010b) = 100b (r--)

Read + Execute

111b - (111b & 010b) = 101b (r-x)

3 (011b) Read

110b - (110b & 011b) = 100b (r--)

Read

111b - (111b & 011b) = 100b (r--)

4 (100b) Write

110b - (110b & 100b) = 010b (-w-)

Write + Execute

111b - (111b & 100b) = 011b (-wx)

5 (101b) Write

110b - (110b & 101b) = 010b (-w-)

Write

111b - (111b & 101b) = 010b (-w-)

6 (110b) None

110b - (110b & 110b) = 000b (---)

Execute

111b - (111b & 110b) = 001b (--x)

7 (111b) None

110b - (110b & 111b) = 000b (---)

None

111b - (111b & 111b) = 000b (---)