2.10.10. umask

Linux for Programmers and Users, Section 5.22.4

umask

Sets the umask value to the specified number or reports the current umask value.

SYNOPSIS

umask [NUMBER]

The umask number controls the default permissions of newly created files. Rather than the number specifying the permission, the umask number indicates restrictions on the file permissions.

Files are created with the open() system call to the operating system. One of the parameters of the open() is the permissions of the file. Not all programs attempt to create files with the same permissions. Most programs want to create files that can be read and written to – permission 0666. But a compiler that creates executable programs wants to create files that can be executed also – permission 0777. The umask is used with any program that creates new files to prevent undesired permissions from being granted.

Umask values are usually 0002 or 0022 - restrict write permission by others or group and others.

$ umask
0002

Consider the following experiment:

$ for i in `jot 1 7`
>do
>n=000$i
>umask $n
>touch t$i
>ls -ld t$i
>rm t$i
>done
-rw-rw-rw- 1 tim tim 0 Aug 19 15:27 t1
-rw-rw-r-- 1 tim tim 0 Aug 19 15:27 t2
-rw-rw-r-- 1 tim tim 0 Aug 19 15:27 t3
-rw-rw--w- 1 tim tim 0 Aug 19 15:27 t4
-rw-rw--w- 1 tim tim 0 Aug 19 15:27 t5
-rw-rw---- 1 tim tim 0 Aug 19 15:27 t6
-rw-rw---- 1 tim tim 0 Aug 19 15:27 t7