umask: why new files get their rights
Video 114 of 140 in Virtualization Foundations, in the unit “Users, groups, permissions and sudo”. 2:48, with English and Hindi audio.
You create a file and never set its rights, yet it already has some. Who decided that? The answer is a setting called the umask. It affects every file you create, from a plain text note to the output of a program, so it can cause surprises when a file is more open or more closed than you expected. This article explains how the umask trims the starting rights, how to read it and set it, and why a service can create files with a different one.
What you will learn
- A new file starts from a default set of rights, and the umask removes some of them.
- The umask only takes rights away. It never adds a right that a program did not ask for.
- Running umask with no number shows the current value, and setting it changes only the current shell and the programs it starts.
- A service can set its own umask when it starts, so its files may differ from the files you make in a terminal.
- A common umask for team files is 002, which keeps write for the group.
A default, then a trim
Every new file starts from a default. Programs ask for read and write for everyone, and new folders also get execute. Nobody asks for execute on an ordinary file. Folders start from a different default, which is why new folders keep execute while new files do not get it.
The umask is a number that removes rights from that default. It is a mask, so it only takes rights away, and never adds them. What is left after the trim is the mode the new file really gets.
Reading and setting the umask
umaskNote: A typical value on Ubuntu is 0022, but the value on your machine may differ.
umask 027Setting the mask changes only this shell, and the programs it starts. A new file loses write for the group, and all rights for others. A common umask for team files is 002, which keeps write for the group, so the team can edit shared work.
Checking a new file's mode
touch secret.txtstat -c '%a %A %n' secret.txtWith the mask set to 027, the file is 640, and the letters show the same rights: read and write for the owner, and read for the group. The umask is set in shell startup files and in service configuration, so changing it once may not change every program.
Why a service can differ from your terminal
Services can set their own umask when they start. A file made by a service may therefore differ from one you make in a terminal, even on the same machine. When a file is too open, check the umask of the shell or the service that made it.
Try it yourself
You need a Linux terminal and a practice folder where you can create files. Read the mask, change it for this shell, and then check the mode of a new file.
Print the current mask. A typical value on Ubuntu is 0022, but yours may differ.
umaskSet the mask to 027 for this shell only.
umask 027Create a new empty file. It gets the mode that the mask allows.
touch secret.txtCheck the mode of the new file, in numbers and in letters.
stat -c '%a %A %n' secret.txtWhat you should see: The shell prints 640 -rw-r----- secret.txt
Terms
- umask
- A number that removes rights from the default rights of every new file or folder the shell creates.
- Default rights
- The starting set of rights a program asks for, before the umask removes some of them.
Check yourself
Can a umask give a new file a right it did not ask for?
No. The umask is a mask that only removes rights from the starting set, so it never adds one.
Why does a new folder keep execute when a new file does not get it?
Folders start from a different default that includes execute, so the umask trims that default and not the default for files.
Why might a file made by a service differ from one you create in a terminal?
A service can set its own umask when it starts, so its new files can carry different rights from those made in your shell.
Previous: Changing owners and rights: chown and chmod. Next: setuid, setgid and the sticky bit.