Change umask value in Linux

umask stands for user file-creation mode mask. By default, the base permission for directories is 777 (rwxrwxrwx) and for files are 666 (rw-rw-rw) and kernel uses the umask value to define the permission during the initial creation of files and directories to enhance the security on the system.  

Default Umask Value

The default umask value for root or some other system’s users are 0022 depends upon the value defined inside /etc/profile file and for the other user it’s 0002.

How Kernel Decides Permission on the basis of umask value

In linux system, the permissions are represented by number.

OCTAL VALUE

PERMISSION

4

Read

2

Write

1

Execute

 

Formula To Calculate permission

Files = Base Permission – umask value

Directories = Base Permission – umask value

 Whenever the user creates a files or directories the umask value will be deducted from the base permission of files & directories. For e.g. the root user umask value is 0022 where the 1st ‘0’ represents the special permission that the files & directories can have that we technically call as sticky bit and rest of the 3 values represent the permissions.

        Example Of umask on Files

Now suppose a normal user whose umask value is 0002 creates a file which has a base permission of 666 and the user has a umask value of 0002 then it means the permission on a file after creation will be 664.

[oracle@LAPTOP-U2OPV1ET ~]$ umask

0002

[oracle@LAPTOP-U2OPV1ET ~]$ touch test

[oracle@LAPTOP-U2OPV1ET ~]$ ls -ltr test

-rw-rw-r-- 1 oracle oracle 0 Sep 16 18:57 test

                                                 Example Of umask on Directories

When the normal user whose umask value is 0002 creates any directory which has a base permission of 777 and the umask value is 0002 then the permission will be 775 at the time of directory creation.

 [oracle@LAPTOP-U2OPV1ET ~]$ mkdir database

[oracle@LAPTOP-U2OPV1ET ~]$ ls -ld database/

drwxrwxr-x 2 oracle oracle 4096 Sep 16 18:59 database/

                                                 How To Check current Umask value

[root@oratest1 ~]# umask

0022

                                     Change umask value at a session level or temporary basis

[oracle@LAPTOP-U2OPV1ET ~]$ umask

0002

[oracle@LAPTOP-U2OPV1ET ~]$ umask 0022

[oracle@LAPTOP-U2OPV1ET ~]$ umask

0022

[oracle@LAPTOP-U2OPV1ET ~]$ mkdir UK_DIARIES

[oracle@LAPTOP-U2OPV1ET ~]$ ls -ld UK_DIARIES/

drwxr-xr-x 2 oracle oracle 4096 Sep 16 19:09 UK_DIARIES/

NOTE 1: This umask value will only be application until we logout or close our current session.          

NOTE 2: If any new user will login or we open a new session then the umask value will be the permanent one.

                                   Change umask value on permanent basis

The umask value of all the user depends upon the value set by the system inside /etc/profile file.we can change its value as per our requirement by modifying the IF clause highlighted in RED.

STEP 1: Open /etc/profile and search for umask

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

# Current threshold for system reserved uid/gids is 200

# You could check uidgid reservation validity in

# /usr/share/doc/setup-*/uidgid file

if [ $UID -gt 199] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then

    umask 002

else

    umask 022

fi

NOTE: Check for the umask value in if-else clause and you can find that all the users whose UID is greater than 199 will get umask value of 002 and sometimes some software require the umask value of 022 especially in the case of cluster configuration.

STEP 2: To change the value of umask on a permanent basis increase the UID’s value as per your user’s UID

 For e.g. if your user UID is 1200 then change if [ $UID -gt 199], 199 into 1200 so that the else clause will get execute and your user’s permission will set to 0022

STEP 3: How To Change Practically

[root@oratest1 ~]# cat /etc/passwd | grep -i umask_user

umask_user:x:24500:24500::/home/umask_user:/bin/bash

 [root@oratest1 ~]# su - umask_user

[umask_user@oratest1 ~]$ umask

0002

NOTE: The umask_user id is 24500 and as per IF condition as represented in

STEP 1, if the userid is greater than 199 then it’ll get a umask value of 0002.

STEP 4: make change in /etc/profile as per umask_user userid

 #if [ $UID -gt 199] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then

if [ $UID -gt 24501 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then

    umask 002

else

    umask 022

fi

NOTE: Since our userid for umask_user is 24500. So, Replace the old 199 value with 24501 highlighted in RED and save the file.

STEP 5: Login with umask user and validate the new umask value

 [root@oratest1 ~]# su - umask_user

Last login: Sun Sep 15 13:13:54 IST 2024 on pts/1

[umask_user@oratest1 ~]$ umask

0022




Post a Comment

Previous Post Next Post