umask 022
Answer
With umask 022, new files are created with mode 644 (rw-r--r--) and new directories with 755 (rwxr-xr-x). The 2s remove write permission for group and others; everything else the program asked for is kept.
What umask 022 gives
- New files
644rw-r--r--- New directories
755rwxr-xr-xumask -Su=rwx,g=rx,o=rx
What to know
A umask lists the bits to remove from new files. Programs normally request 666 for files and 777 for directories, and the kernel clears every bit that is set in the umask: 666 becomes 644 and 777 becomes 755. Files never get execute from the umask; that has to be added with chmod.
022 is the default in /etc/login.defs on Debian and Ubuntu and is the usual umask for root and for system services. Run umask to see yours, or umask -S for the symbolic form (u=rwx,g=rx,o=rx).
Gotchas
- The umask is a bitmask, not a subtraction. With umask 033, files become 644, not 633: the umask can only remove bits that were requested.
- It applies only when a file is created. It does not change existing files, and
chmod 777ignores it completely (only a symbolic chmod with no who-letter, likechmod +x, is filtered). - Setting
umaskin a shell affects only that shell and its children. Services get theirs from their manager; for systemd units setUMask=in the unit file. - A default ACL on the parent directory replaces the umask for files created there.
Related explainers
- umask 002umask 002 creates files as 664 and directories as 775, so your group can edit what you create. When to use...
- umask 077umask 077 creates files as 600 and directories as 700, so nothing you create is visible to other users....
- chmod 644 (rw-r--r--)chmod 644 lets the owner read and write a file while everyone else can only read it. The default for...
- chmod 755 (rwxr-xr-x)chmod 755 gives the owner read, write and execute, and everyone else read and execute. When to use it for...
- chmod +xchmod +x adds execute permission so a script can be run as ./script. Who gets it, why the umask filters it...