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.
| umask | New file (from 666) | New directory (from 777) |
|---|---|---|
| 022 | 644 | 755 |
| 027 | 640 | 750 |
| 077 | 600 | 700 |
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.
umask
umask 027
umaskThe 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.
(umask 077; touch private.txt)
stat -c "%a %n" private.txtThe 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.
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.