Skip to content

Linux Foundations / Manage a Linux host ยท Step 10 of 50

Lesson: UmaskSwitch
โ† Linux Foundations

System administration

Umask

Learn how the umask decides the permissions of newly created files and directories.

When a program creates a file, what permissions does it get? The program asks for some, and your umask removes some. The umask is a list of bits that new files must not have.

Checking your umask

$ umask
0022
$ umask -S
u=rwx,g=rx,o=rx

The plain form shows the bits that are blocked. The -S form shows the opposite view: what is still allowed.

How the mask is applied

Most programs ask for 666 (read and write) when creating a file and 777 when creating a directory. The kernel then clears every bit that is set in the umask.

With a umask of 022, write is removed from group and others:

$ umask 022
$ touch a.txt; mkdir adir
$ ls -ld a.txt adir
-rw-r--r-- 1 maya maya    0 Mar  5 08:00 a.txt
drwxr-xr-x 2 maya maya 4096 Mar  5 08:00 adir

Files do not become executable here because the program never asked for execute. A umask can only take permissions away.

It is tempting to think of this as subtraction, but it clears bits. With umask 027, a new file gets 640: the 7 removes everything from others, and does not produce a negative number.

Changing it

$ umask 077
$ touch secret.txt
$ ls -l secret.txt
-rw------- 1 maya maya 0 Mar  5 08:05 secret.txt

The setting belongs to the current shell and is inherited by programs it starts. It disappears when the shell exits, and it does not change files that already exist.

To make it permanent for your account, add a umask line to a shell startup file such as ~/.profile or ~/.bashrc. System-wide defaults vary: some distributions use 022, others give regular users 002, and the setting may come from /etc/login.defs, PAM or /etc/profile. Services started by systemd have their own UMask= setting. A directory with a default ACL also overrides the umask for files created in it.

Try it

$ umask
$ umask 007
$ touch team.txt; mkdir teamdir
$ ls -ld team.txt teamdir

Work out the expected modes first. Then close that shell, open a new one and run umask again.

Check your understanding

Umask

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.