umask Command Guide for Default File Permissions
umask controls the permissions that new files and directories get — the default you can’t see until after you create them.
What is umask, and Why Use It?
When you create a file, Linux starts with maximum permissions and then subtracts something. That subtraction is the umask (“user mask”). The same value decides the permissions of every file and directory you create, for your whole shell session.
It’s invisible most of the time — until a file you created shows up rw-r--r-- when you wanted rw-rw-r--, or a script you wrote can’t be executed because it came out rw-r--r--.
Why Learn umask?
- You create files with predictable permissions instead of being surprised by them.
- Scripts and services set their umask so their output has the right ownership and access.
- Security defaults matter — a too-permissive umask (like
000) makes every new file world-readable. - It explains why “the file was created with weird permissions” happens at all.
umaskis not a permission on a file — it’s the default subtracted from every newly created file. Change it once per session and every file you create follows it.
1. See Your Current umask
umask
Example:
$ umask
0022
- The value is shown as an octal number, usually four digits (the leading
0is often dropped):022. 022is the classic default: new files end up644, new directories755.
2. Understand the Math
The umask is subtracted from the defaults:
| Created item | Starting value | Minus umask 022 | Result |
|---|---|---|---|
| File | 666 (rw-rw-rw-) | 022 | 644 (rw-r–r–) |
| Directory | 777 (rwxrwxrwx) | 022 | 755 (rwxr-xr-x) |
Example:
$ touch testfile
$ ls -l testfile
-rw-r--r-- 1 alice alice 0 Dec 4 09:30 testfile
- Files never get the execute bit from umask alone (executables must be marked with
chmod). - Each umask digit masks one permission class: first = owner, second = group, third = others.
3. Set a umask for the Session
umask 022
umask 002
Example:
# Group can write new files too (useful for shared project dirs)
umask 002
# Everything you create from now on is owner-only
umask 077
- The change lasts until you close the shell (or set it again).
umask 002is the common choice for collaborative directories: new files are664, new directories775.
4. Set a Persistent umask
To make it survive reboots and new shells, put it in the shell startup files:
echo 'umask 002' >> ~/.bashrc
Example:
# For all users on the system (as root):
echo 'umask 027' >> /etc/profile
~/.bashrccovers interactive shells;/etc/profilecovers login shells system-wide.- Services usually set their own umask in their unit files or init scripts.
5. Check the umask in Scripts
Scripts should set their umask explicitly so their output is predictable:
#!/bin/bash
umask 077
echo "secrets" > private.txt
Example:
$ ./secure.sh
$ ls -l private.txt
-rw------- 1 alice alice 8 Dec 4 09:31 private.txt
- Without an explicit umask, the script inherits whatever the caller had —
private.txtcould end up world-readable. - Setting it at the top of the script makes behavior independent of the environment.
6. Common umask Values
| umask | New file | New dir | Meaning |
|---|---|---|---|
022 | 644 | 755 | Classic default; others read-only |
002 | 664 | 775 | Group-writable; collaboration |
027 | 640 | 750 | Group read, no others |
077 | 600 | 700 | Owner only — private |
000 | 666 | 777 | Everything open (avoid!) |
Example:
# Owner-only defaults for a private workspace
umask 077
077is a sensible default for anything sensitive — keys, notes, credentials.
Common Pitfalls
- Confusing umask with permissions.
umasknever changes an existing file; it only affects newly created ones. To fix old files, usechmod. - Assuming umask applies globally. It’s per-shell-session. Each terminal, cron job, and service has its own.
- Setting
umask 000. Every file you create becomes world-readable and (for directories) world-writable — a security hole. - Forgetting files never get
xfrom umask. Executables must be marked explicitly withchmod +x. - Editing the wrong startup file.
~/.bashrccovers interactive shells only; login shells and services need/etc/profileor their own config.
Related Pages
| Page | Why it’s related |
|---|---|
| chmod | Fix permissions on files umask already created |
| chown | Ownership works together with umask defaults |
| touch | Creating a file is where the umask applies |
| mkdir | New directories get the umask’d 755/775/700 |
| id | Check your identity before setting defaults |
umask Cheatsheet
| Command | Meaning |
|---|---|
umask | Print the current umask |
umask 022 | Classic default — others read-only |
umask 002 | Group-writable — collaboration |
umask 077 | Owner-only — private files |
umask 000 | Everything open (avoid) |
echo 'umask 002' >> ~/.bashrc | Make it persistent for your shells |
ls -l newfile | Verify what the umask produced |