Sobes.tech
Junior

What is umask?

sobes.tech AI

Answer from AI

Umask (short for user file-creation mode mask) is a command in Unix/Linux-like operating systems that sets the default access rights mask for newly created files and directories. It is a set of bits that are disabled from full permissions (777 for directories, 666 for files) when an object is created.

Representation of umask:

  • Umask is usually represented as an octal code of three digits, for example, 022. Each digit corresponds to permissions for the owner, group, and others.
  • Mask bits are inverted relative to the desired permissions. For example, to disable write permission (2) for the group, the corresponding bit is set in the umask.

Calculating the resulting permissions:

The resulting access rights for a new file/directory are calculated as:

Result = Original_Permissions AND (NOT umask)

Where:

  • Original_Permissions for a file = 666 (rw-rw-rw-)
  • Original_Permissions for a directory = 777 (rwxrwxrwx)
  • NOT umask is the bitwise NOT of the umask value. When using an octal code, each digit of the umask is subtracted from 7.

Example: umask = 022

  • For a file: 666 AND (NOT 022) = 666 AND 755 = 644 (rw-r--r--)
  • For a directory: 777 AND (NOT 022) = 777 AND 755 = 755 (rwxr-xr-x)

Viewing and setting umask:

  • Viewing the current umask:

    # Displays umask in octal
    umask
    
    # Displays umask in symbolic form
    umask -S
    
  • Setting a new umask:

    # Sets umask in octal
    umask 027
    
    # Sets umask in symbolic form
    umask u=rwx,g=rx,o=r
    

Umask is an important tool for ensuring basic file system security, establishing reasonable default access rights for new objects.