Red Hat Training

A Red Hat training course is available for RHEL 8

Chapter 26. Managing the umask

You can use the umask utility to display, set, or change the current or default value of the umask.

26.1. Displaying the current value of the umask

You can use the umask utility to display the current value of the umask in symbolic or octal mode.

Procedure

  • To display the current value of the umask in symbolic mode, use:

    $ umask -S
  • To display the current value of the umask in the octal mode, use:

    $ umask
    Note

    When displaying the umask in octal mode, you may notice it displayed as a four digit number (0002 or 0022). The first digit of the umask represents a special bit (sticky bit, SGID bit, or SUID bit). If the first digit is set to 0, the special bit is not set.

26.2. Displaying the default bash umask

There are a number of shells you can use, such as bash, ksh, zsh and tcsh. Those shells can behave as login or non-login shells. The login shell is typically invoked by opening a native or a GUI terminal.

To determine whether you are executing a command in a login or a non-login shell, use the echo $0 command.

Example 26.1. Determining if you are working in a login or a non-login bash shell

  • If the output of the echo $0 command returns bash, you are executing the command in a non-login shell.

    $ echo $0
    bash

    The default umask for the non-login shell is set in /etc/bashrc configuration file.

  • If the output of the echo $0 command returns -bash, you are executing the command in a login shell.

    # echo $0
    -bash

    The default umask for the login shell is set in /etc/profile configuration file.

Procedure

  • To display the default bash umask for the non-login shell, use:

    $ grep umask /etc/bashrc

    The output returns:

    # By default, we want umask to get set. This sets it for non-login shell.
           umask 002
           umask 022
  • To display the default bash umask for the login shell, use:

    $ grep umask /etc/profile

    The output returns:

    # By default, we want umask to get set. This sets it for login shell
           umask 002
           umask 022

26.3. Setting the umask using symbolic values

You can use the umask utility with symbolic values (a combination letters and signs) to set the umask for the current shell session

You can assign the following permissions:

  • Read (r)
  • Write (w)
  • Execute (x)

Permissions can be assigned to the following levels of ownership:

  • User owner (u)
  • Group owner (g)
  • Other (o)
  • All (a)

To add or remove permissions you can use the following signs:

  • + to add the permissions on top of the existing permissions
  • - to remove the permissions from the existing permission
  • = to remove the existing permissions and explicitly define the new ones

    Note

    Any permission that is not specified after the equals sign (=) is automatically prohibited.

Procedure

  • To set the umask for the current shell session, use:

    $ umask -S <level><operation><permission>

    Replace <level> with the level of ownership you want to set the umask for. Replace <operation> with one of the signs. Replace <permission> with the permissions you want to assign. For example, to set the umask to u=rwx,g=rwx,o=rwx, use umask -S a=rwx.

    See User file creation mode for more details.

    Note

    The umask is only valid for the current shell session.

26.4. Setting the umask using octal values

You can use the umask utility with octal values (numbers) to set the umask for the current shell session.

Procedure

  • To set the umask for the current shell session, use:

    $ umask octal_value

    Replace octal_value with an octal value. See User file creation mode mask for more details.

    Note

    The umask is only valid for the current shell session.

26.5. Changing the default umask for the non-login shell

You can change the default bash umask for standard users by modifying the /etc/bashrc file.

Prerequisites

  • root access

Procedure

  1. As root, open the /etc/bashrc file in an editor of your choice.
  2. Modify the following sections to set a new default bash umask:

        if [ $UID -gt 199 ] && [ “id -gn” = “id -un” ]; then
           umask 002
        else
           umask 022
        fi

    Replace the default octal value of the umask (002) with another octal value. See User file creation mode mask for more details.

  3. Save the changes and exit the editor.

26.6. Changing the default umask for the login shell

You can change the default bash umask for the root user by modifying the /etc/profile file.

Prerequisites

  • root access

Procedure

  1. As root, open the /etc/profile file in an editor of your choice.
  2. Modify the following sections to set a new default bash umask:

    if [ $UID -gt 199 ] && [ “/usr/bin/id -gn” = “/usr/bin/id -un” ]; then
        umask 002
    else
        umask 022
    fi

    Replace the default octal value of the umask (022) with another octal value. See User file creation mode mask for more details.

  3. Save the changes and exit the editor.

26.7. Changing the default umask for a specific user

You can change the default umask for a specific user by modifying the .bashrc for that user.

Procedure

  • Append the line that specifies the octal value of the umask into the .bashrc file for the particular user.

    $ echo 'umask octal_value' >> /home/username/.bashrc

    Replace octal_value with an octal value and replace username with the name of the user. See User file creation mode mask for more details.

26.8. Setting default UMASK for newly created home directories

You can change the permissions that specify the UMASK for home directories of newly created users by modifying the /etc/login.defs file.

Procedure

  1. As root, open the /etc/login.defs file in an editor of your choice.
  2. Modify the following section to set a new default UMASK:

    # The permission mask is initialized to this value. If not specified,
    # the permission mask will be initialized to 022.
    UMASK 077

    Replace the default octal value (077) with another octal value. See User file creation mode mask for more details.

  3. Save the changes and exit the editor.