Check the Current umask
umask
umask -S
The first command prints a number such as 0022; the leading zero stands for the special bits and can be ignored here. The second prints the same value as the permissions that remain, for example u=rwx,g=rx,o=rx.
What the Mask Does
A program that creates a file asks for a mode. For ordinary files that is usually 666 (read and write for everyone), for directories 777 (read, write, and enter for everyone). The kernel then removes every bit that is set in the umask. The umask never adds a permission and never changes files that already exist; for those you use chmod, as described in chmod recursive.
This is also why new files never get the execute bit: the program does not ask for it, so there is nothing for the umask to keep. A script you write needs chmod +x before you can run it directly.
Common Values Worked Out
| umask | New files | New directories |
|---|---|---|
022 | 644, rw-r--r-- | 755, rwxr-xr-x |
002 | 664, rw-rw-r-- | 775, rwxrwxr-x |
027 | 640, rw-r----- | 750, rwxr-x--- |
077 | 600, rw------- | 700, rwx------ |
- 022: others can read your files but not change them. A common default.
- 002: your group can also write. Useful for a team that shares files through a group.
- 027: the group can read, others get nothing. A good value for service accounts.
- 077: only the owner has access. Suits an account that handles secrets.
Bits, Not Subtraction
The umask removes bits; it does not subtract numbers. With a umask of 033, a new file starts from 666 and loses write for group and others plus execute, which it did not have: the result is 644, not 633. Think of each digit as "take these permissions away", and the results above follow.
Change It for the Current Shell
umask 027
touch report.txt
mkdir reports
ls -ld report.txt reports
The new value applies to this shell and to every program started from it, until you close the session.
Make It Permanent for a User
Add the line umask 027 to ~/.bashrc for interactive shells or to ~/.profile for login shells. The system-wide default comes from the UMASK line in /etc/login.defs, which the pam_umask module applies at login, and some distributions also set it in shell startup files such as /etc/bashrc. Distributions differ here: where each user has a personal group of the same name, some systems give regular users 002 and root 022. Check with umask after a fresh login instead of assuming.
Set It for a systemd Service
Services do not read shell profile files, so a umask in .bashrc has no effect on them. Set it in the unit with a drop-in:
sudo systemctl edit myapp.service
Add these lines in the editor that opens, save, and restart the service:
[Service]
UMask=0027
sudo systemctl restart myapp.service
Without this setting, systemd services run with a umask of 0022. Drop-ins and the other unit options are explained in systemd unit files.
umask and Shared Directories
For a directory that several users write to, the umask alone is not enough: new files must also get the right group. Combine a umask of 002 with the SGID bit on the directory, as shown in sticky bit, SUID, and SGID. When a directory has a default ACL, that ACL takes the place of the umask for files created inside it; see setfacl and getfacl.