Linux umask Command

User File Creation Mask - 10 Practical Examples with Detailed Explanations

Example 1

Display Current umask

$ umask
0022
Displays the current umask value in octal format. The umask (user file creation mask) determines default permissions for newly created files and directories. The leading 0 indicates octal notation. This is the most basic and commonly used umask command to check current settings.
Default umask: 0022 is typical for regular users, 0077 for root on some systems
Example 2

Display umask in Symbolic Form

$ umask -S
u=rwx,g=rx,o=rx
Shows umask in symbolic notation using the '-S' option, displaying which permissions are allowed (not masked). This format shows permissions that WILL be set, unlike the octal form which shows what's being removed. Much easier to understand for humans than octal notation.
Reading Output: u=user, g=group, o=others. This shows allowed permissions, not the mask
Example 3

Set umask to 022 (Standard)

$ umask 022 $ touch testfile.txt $ ls -l testfile.txt
-rw-r--r-- 1 user user 0 Oct 18 10:45 testfile.txt
Sets umask to 022, the most common setting for regular users. This creates files with 644 permissions (666 - 022 = 644) and directories with 755 permissions (777 - 022 = 755). Owner can read/write, group and others can only read. Standard for shared systems where files should be readable by everyone.
Calculation: Files: 666 - 022 = 644 (rw-r--r--), Directories: 777 - 022 = 755 (rwxr-xr-x)
Example 4

Set umask to 077 (Private)

$ umask 077 $ touch private.txt $ ls -l private.txt
-rw------- 1 user user 0 Oct 18 10:45 private.txt
Sets umask to 077 for maximum privacy. Only the owner has any permissions, group and others have no access at all. Files get 600 permissions (rw-------) and directories get 700 (rwx------). Essential for security-conscious environments and handling sensitive data like SSH keys or credentials.
Security Best Practice: Use umask 077 when creating sensitive files or in untrusted environments
Example 5

Set umask to 002 (Group Writable)

$ umask 002 $ touch shared.txt $ mkdir shared_dir $ ls -l
-rw-rw-r-- 1 user group 0 Oct 18 10:45 shared.txt drwxrwxr-x 2 user group 4096 Oct 18 10:45 shared_dir
Sets umask to 002, allowing group write access. Files get 664 permissions and directories get 775. Perfect for collaborative environments where team members in the same group need to modify each other's files. Common in development teams with shared projects.
Collaboration: Ideal for project directories where team members need full access
Example 6

Temporarily Change umask for One Command

$ (umask 077; touch secure.key) $ ls -l secure.key
-rw------- 1 user user 0 Oct 18 10:45 secure.key
Changes umask temporarily within a subshell (parentheses). The umask change only affects commands within the subshell, then reverts to the original value. Perfect for creating specific files with restricted permissions without affecting the rest of your session. The semicolon separates commands within the subshell.
Best Practice: Use this pattern when creating sensitive files like private keys
Example 7

Set umask in Shell Configuration

$ echo "umask 022" >> ~/.bashrc $ source ~/.bashrc
# umask 022 is now set for all new login shells
Makes umask permanent by adding it to shell configuration file (~/.bashrc for bash, ~/.zshrc for zsh). The umask command executes every time you start a new shell, ensuring consistent default permissions. Essential for maintaining security policies across sessions and reboots.
Configuration Files: ~/.bashrc (bash), ~/.zshrc (zsh), ~/.profile (login shells)
Example 8

Set umask with Symbolic Notation

$ umask u=rwx,g=rx,o=
$ umask 0027 $ touch symbolic_file.txt $ ls -l symbolic_file.txt -rw-r----- 1 user user 0 Oct 18 10:45 symbolic_file.txt
Sets umask using symbolic notation instead of octal. Specifies allowed permissions directly: user gets rwx, group gets rx, others get nothing. This translates to umask 027. Symbolic form is often easier to understand and less error-prone than calculating octal values.
Translation: u=rwx,g=rx,o= means umask 027 (removes write for group, all for others)
Example 9

Understanding umask Calculation

$ umask 027 $ touch file.txt $ mkdir directory $ ls -l
-rw-r----- 1 user user 0 Oct 18 10:45 file.txt drwxr-x--- 2 user user 4096 Oct 18 10:45 directory # Files: 666 - 027 = 640 (rw-r-----) # Dirs: 777 - 027 = 750 (rwxr-x---)
Demonstrates how umask calculations work. Files start with base permissions 666 (rw-rw-rw-), directories with 777 (rwxrwxrwx). The umask value is subtracted from these base values to get final permissions. Note that the execute bit is automatically set for directories but not files.
Formula: Final Permissions = Base Permissions - umask Value
Example 10

Verify umask Effect with Script

$ cat > test_umask.sh << 'EOF' #!/bin/bash echo "Testing different umask values:" for mask in 022 027 077 002; do echo -e "\n=== umask $mask ===" (umask $mask touch "test_${mask}.txt" mkdir "test_${mask}_dir" ls -ld test_${mask}*) done EOF $ chmod +x test_umask.sh $ ./test_umask.sh
=== umask 022 === -rw-r--r-- test_022.txt drwxr-xr-x test_022_dir === umask 027 === -rw-r----- test_027.txt drwxr-x--- test_027_dir === umask 077 === -rw------- test_077.txt drwx------ test_077_dir === umask 002 === -rw-rw-r-- test_002.txt drwxrwxr-x test_002_dir
Creates a script to demonstrate how different umask values affect file and directory permissions. Each umask is tested in a subshell, creating files and directories to show the resulting permissions. Excellent for understanding and teaching umask behavior.
Learning Tool: Run this script to visualize how umask affects permissions
Bonus

Common umask Values Reference Table

umask | Files | Directories | Use Case ------|----------|-------------|---------------------------------- 0022 | 644 | 755 | Standard user (files readable by all) | rw-r--r--| rwxr-xr-x | Default on most Linux systems ------|----------|-------------|---------------------------------- 0002 | 664 | 775 | Group collaboration | rw-rw-r--| rwxrwxr-x | Team projects, shared development ------|----------|-------------|---------------------------------- 0027 | 640 | 750 | Group readable, not world readable | rw-r-----| rwxr-x--- | Semi-private with group access ------|----------|-------------|---------------------------------- 0077 | 600 | 700 | Private (owner only) | rw-------| rwx------ | Security-sensitive files, SSH keys ------|----------|-------------|---------------------------------- 0000 | 666 | 777 | World writable (DANGEROUS!) | rw-rw-rw-| rwxrwxrwx | Never use except special cases ------|----------|-------------|---------------------------------- Permission Bit Values: Read (r) = 4 Write (w) = 2 Execute (x) = 1 Calculation Examples: umask 022: Files: 666 - 022 = 644 (rw-r--r--) Dirs: 777 - 022 = 755 (rwxr-xr-x) umask 077: Files: 666 - 077 = 600 (rw-------) Dirs: 777 - 077 = 700 (rwx------) umask 002: Files: 666 - 002 = 664 (rw-rw-r--) Dirs: 777 - 002 = 775 (rwxrwxr-x)
Comprehensive reference table showing common umask values, their resulting file and directory permissions, and appropriate use cases. Essential reference for system administrators and security-conscious users.
Reference

umask Command Quick Reference

Basic Usage: umask Display current umask (octal) umask -S Display umask in symbolic form umask -p Display in format reusable as input Setting umask: umask 022 Set umask to 022 (octal) umask u=rwx,g=rx,o=rx Set using symbolic notation umask u=rwx,g=rx,o= Remove all others permissions Temporary Changes: (umask 077; command) Change umask for one command (umask 077 && command) Alternative syntax Permanent Changes: echo "umask 022" >> ~/.bashrc For bash echo "umask 022" >> ~/.zshrc For zsh echo "umask 022" >> ~/.profile For login shells Understanding Calculations: Base file permissions: 666 (rw-rw-rw-) Base directory permissions: 777 (rwxrwxrwx) Formula: result = base - umask Common Patterns: # Create secure file (umask 077; touch ~/.ssh/id_rsa) # Set for group collaboration umask 002 # Create world-readable files umask 022 # Maximum security umask 077 Security Recommendations: - Use umask 077 for sensitive files (keys, passwords) - Use umask 022 for general files (standard default) - Use umask 002 for collaborative environments - Never use umask 000 (world writable) - Set umask in shell config files for consistency - Use temporary umask for specific secure operations Testing umask: # See what permissions will be created umask 027 touch test.txt ls -l test.txt rm test.txt
Complete reference for umask command usage including syntax, calculations, common patterns, and security recommendations for proper permission management.