RED HAT ENTERPRISE LINUX

Default Permissions & umask

Display and Set Default File Permissions

College-Level Course Module | RHEL System Administration

Learning Objectives

1
Review permission basics

Octal notation and permission meanings

2
Understand umask

What umask is and how it affects new files

3
Calculate permissions

Determine resulting permissions from umask values

4
Set umask values

Change umask temporarily and permanently

5
Apply security best practices

Choose appropriate umask for different scenarios

Permission Review

Linux permissions control who can read, write, and execute files and directories. Permissions are set for owner, group, and others.

PermissionSymbolOctalFile MeaningDirectory Meaning
Readr4View file contentsList directory contents
Writew2Modify file contentsCreate/delete files in dir
Executex1Run as programEnter directory (cd)
[user@host ~]$ ls -l myfile.txt
-rw-r--r--. 1 user user 100 Jan 20 10:00 myfile.txt
 │├─┤├─┤├─┤
 │ │  │  └── Others: read only (4)
 │ │  └───── Group: read only (4)
 │ └──────── Owner: read + write (6)
 └────────── File type (- = regular file)

Octal Permission Values

OctalBinarySymbolicPermissions
0000---None
1001--xExecute only
2010-w-Write only
3011-wxWrite + Execute
4100r--Read only
5101r-xRead + Execute
6110rw-Read + Write
7111rwxRead + Write + Execute
7 5 5
Owner | Group | Others
=
rwx r-x r-x
Symbolic notation

What is umask?

umask (user file-creation mask) is a value that determines which permissions are removed from the default permissions when creating new files and directories.

Default Permissionsumask = Actual Permissions

# Display current umask
[user@host ~]$ umask
0022

# Display in symbolic form
[user@host ~]$ umask -S
u=rwx,g=rx,o=rx

# The umask REMOVES permissions, it doesn't grant them
# umask 022 removes write for group and others
Key concept: umask specifies which permissions to take away, not which to give. A umask of 022 removes write permission from group and others.

Base Permissions

Before applying umask, the system starts with base permissions that differ for files and directories.

Files: 666

Base: rw-rw-rw-

Read and write for everyone.
No execute by default (security).

Directories: 777

Base: rwxrwxrwx

Full access for everyone.
Execute needed to enter directory.

Why the difference? Files do not get execute permission by default for security - a new text file should not be runnable. Directories need execute permission to allow cd.

Calculating File Permissions

For files: Start with base 666, subtract umask.

Base file permissions: 666 (rw-rw-rw-)
umask value: − 022 (----w--w-)
Resulting permissions: 644 (rw-r--r--)
# Create a file with umask 022
[user@host ~]$ umask
0022
[user@host ~]$ touch newfile.txt
[user@host ~]$ ls -l newfile.txt
-rw-r--r--. 1 user user 0 Jan 20 10:00 newfile.txt
# 644 = Owner: rw, Group: r, Others: r

Calculating Directory Permissions

For directories: Start with base 777, subtract umask.

Base directory permissions: 777 (rwxrwxrwx)
umask value: − 022 (----w--w-)
Resulting permissions: 755 (rwxr-xr-x)
# Create a directory with umask 022
[user@host ~]$ umask
0022
[user@host ~]$ mkdir newdir
[user@host ~]$ ls -ld newdir
drwxr-xr-x. 2 user user 6 Jan 20 10:00 newdir
# 755 = Owner: rwx, Group: rx, Others: rx

Common umask Values

umaskFile ResultDir ResultUse Case
022 644 (rw-r--r--) 755 (rwxr-xr-x) Standard user default - readable by all
027 640 (rw-r-----) 750 (rwxr-x---) Group collaboration - no others access
077 600 (rw-------) 700 (rwx------) Private - owner only access
002 664 (rw-rw-r--) 775 (rwxrwxr-x) Shared group - group can write
007 660 (rw-rw----) 770 (rwxrwx---) Group only - no others
000 666 (rw-rw-rw-) 777 (rwxrwxrwx) No restrictions (dangerous!)
RHEL defaults: Regular users get umask 0022. Root gets umask 0022. Some organizations set stricter defaults like 0027 or 0077.

Setting umask Temporarily

Use the umask command to change the value for your current shell session. The change lasts until you log out.

# Check current umask
[user@host ~]$ umask
0022

# Set more restrictive umask
[user@host ~]$ umask 077
[user@host ~]$ umask
0077

# Create a file with new umask
[user@host ~]$ touch private.txt
[user@host ~]$ ls -l private.txt
-rw-------. 1 user user 0 Jan 20 10:00 private.txt
# Only owner can read/write (600)

# Set using symbolic notation
[user@host ~]$ umask u=rwx,g=rx,o=
[user@host ~]$ umask
0027
Session only: This change is lost when you log out or open a new terminal. Use profile files for permanent changes.

Setting umask Permanently

Add the umask command to shell startup files to make it permanent. The location depends on whether you want it for one user or all users.

# For a single user: add to ~/.bashrc
[user@host ~]$ echo "umask 027" >> ~/.bashrc

# For a single user: or add to ~/.bash_profile
[user@host ~]$ echo "umask 027" >> ~/.bash_profile

# For all users: add to /etc/profile (requires root)
[root@host ~]# echo "umask 027" >> /etc/profile

# For all users: or create file in /etc/profile.d/
[root@host ~]# echo "umask 027" > /etc/profile.d/custom-umask.sh

# Apply changes to current session (or log out and in)
[user@host ~]$ source ~/.bashrc
File precedence: /etc/profile runs first (system-wide), then ~/.bash_profile or ~/.bashrc (user-specific). Later settings override earlier ones.

umask in /etc/login.defs

/etc/login.defs sets default umask for new user accounts and is read by login processes.

# View current setting in login.defs
[root@host ~]# grep -i umask /etc/login.defs
UMASK           022

# To change system default, edit this file
[root@host ~]# vim /etc/login.defs
# Change: UMASK    027

# Also affects su - sessions
# This is the fallback if not set elsewhere
login.defs controls:
  • Default umask for logins
  • Password aging defaults
  • UID/GID ranges
  • Home directory creation
Override order:
  • /etc/login.defs (lowest)
  • /etc/profile and /etc/profile.d/
  • ~/.bash_profile, ~/.bashrc (highest)

Understanding the Math

Important: umask uses bitwise AND NOT, not simple subtraction. For typical values this looks like subtraction, but edge cases differ.

Actual operation: result = base AND (NOT umask)

Base (666)
110 110 110
NOT umask (NOT 022)
111 101 101
Result (AND)
110 100 100
= 644
Practical note: Simple subtraction works for most common values. The bit operation matters when umask has a bit set that the base does not have.

Special Permission Bits

The leading digit in a 4-digit umask controls special permissions: setuid, setgid, and sticky bit.

BitValueFile EffectDirectory Effect
Setuid4Run as file owner(no effect)
Setgid2Run as file groupNew files inherit group
Sticky1(no effect)Only owner can delete files
# 4-digit umask format: SUGO (Special, User, Group, Others)
[user@host ~]$ umask
0022     # Leading 0 = no special bits masked

# Special bits are rarely set via umask
# They're usually set explicitly with chmod when needed

# Example: setgid directory for group collaboration
[root@host ~]# chmod g+s /shared/project
[root@host ~]# ls -ld /shared/project
drwxrwsr-x. 2 root developers 6 Jan 20 10:00 /shared/project
#     ^ setgid bit (s instead of x)

Practical Examples

# Scenario 1: Create private files
[user@host ~]$ umask 077
[user@host ~]$ touch secret.txt
[user@host ~]$ ls -l secret.txt
-rw-------. 1 user user 0 Jan 20 10:00 secret.txt

# Scenario 2: Create group-collaborative directory
[user@host ~]$ umask 002
[user@host ~]$ mkdir team-project
[user@host ~]$ ls -ld team-project
drwxrwxr-x. 2 user user 6 Jan 20 10:00 team-project
# Group members can add files

# Scenario 3: Process files with specific permissions
[user@host ~]$ (umask 077; touch temp-sensitive-data.txt)
[user@host ~]$ umask   # Still original value
0022
# Subshell umask didn't affect parent shell

# Scenario 4: Script that creates secure files
#!/bin/bash
umask 077
touch /var/log/myapp/secure.log
# Rest of script...

Checking Effective Permissions

# Always verify permissions after creating files
[user@host ~]$ touch testfile.txt
[user@host ~]$ ls -l testfile.txt
-rw-r--r--. 1 user user 0 Jan 20 10:00 testfile.txt

[user@host ~]$ mkdir testdir
[user@host ~]$ ls -ld testdir
drwxr-xr-x. 2 user user 6 Jan 20 10:00 testdir

# Check numeric permissions
[user@host ~]$ stat -c "%a %n" testfile.txt testdir
644 testfile.txt
755 testdir

# Detailed stat output
[user@host ~]$ stat testfile.txt
  File: testfile.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Access: (0644/-rw-r--r--)  Uid: ( 1000/   user)   Gid: ( 1000/   user)
...
Best practice: After setting a new umask, create test files and directories to verify the permissions are what you expect.

Security Considerations

Too Permissive (umask 000)

  • Files: 666 (world writable!)
  • Dirs: 777 (world writable!)
  • Anyone can read/modify your files
  • Major security vulnerability
  • Never use in production

Too Restrictive (umask 077)

  • Files: 600 (owner only)
  • Dirs: 700 (owner only)
  • May break group collaboration
  • May prevent web servers from reading
  • Good for sensitive data
Balance needed: The right umask depends on the use case. System defaults (022) balance security and usability. Adjust based on actual needs.
# Service accounts often need stricter umask
# Example: database service creates files only it should access
# In service configuration or startup script:
umask 077

umask for Different Users

# System accounts (services) - often very restrictive
# Set in service unit files or startup scripts
[Service]
UMask=0077    # systemd unit file syntax

# Regular users - standard default
# /etc/profile or /etc/login.defs
UMASK 022

# Shared group users - allow group write
# ~/.bashrc for specific users
umask 002

# High-security users - private by default
# ~/.bashrc for security-sensitive accounts
umask 077

# PAM can also set umask based on user/group
# /etc/pam.d/login or /etc/security/limits.conf
[root@host ~]# grep pam_umask /etc/pam.d/login
session    optional     pam_umask.so umask=0027

Troubleshooting

# Problem: Files created with wrong permissions
# Check current umask
[user@host ~]$ umask
0077    # Aha! That's why files are private

# Find where umask is being set
[user@host ~]$ grep -r "umask" ~/.bash* ~/.profile /etc/profile /etc/profile.d/ 2>/dev/null
/etc/profile.d/security.sh:umask 077

# Problem: Can't figure out why permissions are what they are
# Calculate expected vs actual
[user@host ~]$ umask
0022
# Expected file: 666 - 022 = 644
# Expected dir:  777 - 022 = 755

# Problem: Application creates files with unexpected permissions
# Some apps set their own umask internally
# Check app documentation or source code

# Problem: cron jobs create files with wrong permissions
# Set umask at the start of the cron script
#!/bin/bash
umask 022
# rest of script

Best Practices

Do

  • Understand your organization's policy
  • Use 022 for normal shared systems
  • Use 077 for sensitive data handling
  • Set umask in scripts that create files
  • Test umask changes before deploying
  • Verify permissions after file creation
  • Document non-standard umask settings
  • Configure services with appropriate umask

Do Not

  • Use 000 (no protection)
  • Assume all systems have same umask
  • Forget umask only affects new files
  • Rely on umask for security alone
  • Change system defaults without testing
  • Ignore umask in security audits
  • Forget services have separate umask
  • Set overly restrictive for collaboration
Remember: umask is just one part of file security. Combine with proper ownership, ACLs, SELinux, and principle of least privilege.

Key Takeaways

1

umask: Removes permissions from defaults. Files start at 666, directories at 777. umask specifies what to subtract.

2

Calculate: File = 666 − umask, Directory = 777 − umask. Common: 022→644/755, 077→600/700.

3

Set: Temporary: umask 027. Permanent: add to ~/.bashrc or /etc/profile.d/.

4

Verify: Use ls -l or stat to confirm permissions. Test before production deployment.

LAB EXERCISES

  • Display current umask in octal and symbolic forms
  • Calculate expected permissions for umask 027
  • Create files with different umask values and verify
  • Set temporary umask and observe file creation
  • Configure permanent umask in ~/.bashrc
  • Use subshell technique for one-off permission changes

Next: Special Permissions (setuid, setgid, sticky bit)