$_ CLI In Depth
Learning progress 0%
lesson permissions users sudo

umask & Default Permissions

6 min read Module 5: Users, Permissions & sudo Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:28

Default modes are not zero

When a program creates a regular file with the open system call, it passes a requested mode. For a new file the usual request is 0666, which means read and write for all three classes. For a new directory the usual request is 0777. The kernel does not use those values as they are. It first removes the bits that are set in the umask of the process. The result is the mode that the file actually gets.

This means the programs you run do not decide the final mode on their own. The request is a starting point, and the umask is the filter. Two users running the same program can end up with different modes for the same kind of file, because their shells have different umasks.

The arithmetic

The umask is an octal number with the same layout as a mode. A bit that is set in the umask is removed from the requested mode. The operation is a bitwise and with the complement of the umask. In practice you can subtract each digit. For a file requested at 666, a umask of 022 leaves 644. For a directory requested at 777, the same umask leaves 755.

umaskNew file (from 666)New directory (from 777)
022644755
027640750
077600700

The table covers the three umasks that you will meet most often. A umask of 022 is common on desktop systems, because the owner gets full control and everyone else can read. A umask of 027 keeps group members able to read but blocks everyone else. A umask of 077 makes every new file and directory private to the owner.

Why new files are not executable

The default request for a file is 0666, which contains no execute bits at all. The umask can only remove bits, and it cannot add them. So a new regular file is never executable, even with a umask of 000. You must add execute with chmod, which is the reason a freshly written script fails with Permission denied until you change its mode. Directories are different, because their default request includes execute, so a new directory is searchable by default.

Setting the umask

The umask builtin prints the current value, and it can set a new one for the current shell and every program that shell starts afterward. Typing umask alone shows the value in octal. Typing umask 027 changes it for the rest of that shell session. Because the setting is per process, it does not change any file that already exists, and it does not affect other terminals.

BASH3 lines
umask
umask 027
umask

The first command shows the current value. The second sets a stricter one, and the third shows the new value. The change lasts until the shell exits. A permanent setting belongs in a startup file, which later modules cover, but for experiments the builtin is enough.

Testing with a subshell

A subshell lets you try a umask without changing your own shell. Parentheses run the enclosed commands in a child shell that inherits the current settings and then exits. Any umask change made inside the parentheses disappears when the subshell ends, so your prompt keeps its normal value.

BASH2 lines
(umask 077; touch private.txt)
stat -c "%a %n" private.txt

The file is created with mode 600, and the outer shell still has its original umask. This pattern is useful in scripts that must create sensitive files, and it is the pattern the lab for this unit uses when it checks the results.

Tip

If a file you create has the wrong mode, do not reach for chmod 777. Check the umask with the umask builtin first. Most surprises come from a umask that was set in a startup file, not from the program that created the file.

Umask for groups and for other accounts

The usual umask values show different trade-offs. A umask of 022 leaves group and others with read access to what you create, which suits a public document folder. A umask of 002 also gives group write access, which is common in team setups where every member of a shared group must edit the same files. The value 007 is a strict choice for a group-only area, because it removes every right from others while keeping the owner and group.

Choose the umask once, at the level where it belongs, and then leave it alone. Changing it in the middle of a session makes it hard to predict the result of a later command, and it is easy to forget that a subshell change ended. Most surprises come from two different umask values in use at the same time, such as one value in an open terminal and a different value inside a script that runs later.

Programs that request their own modes

Not every program leaves the mode entirely to the umask. Some programs request an explicit mode, such as 0600 for a private key, and the umask still removes its bits from that request. A umask can make a file more private than the program asked for, but it can never make the file less private than the request. So a restrictive umask also protects files created by programs that request permissive modes without thinking about privacy.

Programs that create temporary or secret files usually request 0600 directly, and they rely on the umask only as a second layer. That design is sound, because a careless umask cannot expose the file, and a careless request cannot expose it either. When you write your own script, do the same: ask for the mode you want, and let the umask narrow it further if you choose.

Hands-on: predict and check

Before you run anything, predict the mode of a new file under umask 027. Then create the file inside a subshell and check the mode with stat. The lab for this unit asks you to create one file and one directory under umask 027, record their modes, and then create a private file under umask 077. The checks read the real modes, so the values must come from the umask arithmetic and not from typing numbers that you expect.

Reading, changing and defaulting permission bits

Visualizer

A mode is nine bits in three triplets. Decode one, change it with chmod, then see how umask produces the defaults for new files and directories.

1 / 4
-type
rowner
w
-
rgroup
-
-
-other
-
-
mode
-rw-r-----

The first character is the file type: - regular file, d directory, l symlink. The next nine are three triplets: owner, group and other, each with r, w and x.

f="$(mktemp -d /tmp/perm.XXXX)/p.txt"; touch "$f"; chmod 640 "$f"; stat -c "%a %A %n" "$f"
Terminal lab · 2 steps · real Linux in your browser

umask & Default Permissions

Create files and directories under two umask values, then check the modes that the kernel actually produced.

01
Create files under umask 027
Inside a subshell, set the umask to 027, then create the file /workspace/um/a.txt and the directory /workspace/um/d. Save the modes of both objects in /workspace/um/modes.txt, file mode on line 1 and directory mode on line 2.
Show hint for step 1
Put the umask change and both commands inside parentheses, so the setting ends with the subshell.
02
Create a private file under umask 077
Inside a subshell, set the umask to 077 and create the file /workspace/um/private.txt. Save its octal mode in /workspace/um/private-mode.txt.
Show hint for step 2
A umask of 077 removes every bit from group and other.
Check yourself Question 1 of 4
Concept Check What the umask does recall

What does the umask do?

Recap & Key Takeaways

A program requests a default mode, usually 0666 for files and 0777 for directories, and the umask removes its set bits from that request.
The umask can only remove bits, so new regular files never get execute bits by default.
Use the umask builtin to view or change the value, and run (umask 077; command) in a subshell to test a setting without changing your shell.
The umask affects only files created after the change, and it does not change any existing file.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue