Table of Contents

UMask

When user create a file or directory under Linux or UNIX, she create it with a default set of permissions. In most case the system defaults may be open or relaxed for file sharing purpose. For example, if a text file has 666 permissions, it grants read and write permission to everyone. Similarly a directory with 777 permissions, grants read, write, and execute permission to everyone.

Default umask Value

The user file-creation mode mask (umask) is use to determine the file permission for newly created files. It can be used to control the default file permission for new files. It is a four-digit octal number. A umask can be set or expressed using:

Procedure To Setup Default umask

[todo vi /etc/login.defs] [todo vi /etc/init.d/rc]

You can setup umask in the /etc/bashrc or /etc/profile file for all users.

By default most Linux distros set it to 0022 (022) or 0002 (002). Open /etc/profile or ~/.bashrc file, enter:

vi /etc/profile

or

vi ~/.bashrc

Append/modify following line to setup a new umask:

umask 022

Save and close the file. Changes will take effect after next login. All UNIX users can override the system umask defaults in their /etc/profile file, ~/.profile (Korn / Bourne shell) ~/.cshrc file (C shells), ~/.bash_profile (Bash shell) or ~/.login file (defines the user’s environment at login).

Explain Octal umask Mode 022 And 002

If the default settings are not changed, files are created with the access mode 666 and directories with 777. In this example:

In short,

How to Calculate umasks

The octal umasks are calculated via the bitwise AND of the unary complement of the argument using bitwise NOT. The octal notations are as follows:

Octal valuePermission
0read, write and execute
1read and write
2read and execute
3read only
4write and execute
5write only
6execute only
7no permissions

Use the above table to calculate file permission. For example, if umask is set to 077, the permission can be calculated as follows:

BitTargeted atFile permission
0Ownerread, write and execute
7GroupNo permissions
7OthersNo permissions

To set the umask 077 type the following command at shell prompt:

umask 077
mkdir dir1
touch file
ls -ld dir1 file

Sample outputs:

drwx------ 2 peter peter 4096 2011-03-04 02:05 dir1
-rw------- 1 peter peter    0 2011-03-04 02:05 file

Calculating The Final Permission For FILES

Simply subtract the umask from the base permissions to determine the final permission for file as follows:

666 – 022 = 644

Calculating The Final Permission For DIRECTORIES

Simply subtract the umask from the base permissions to determine the final permission for directory as follows:

777 – 022 = 755

How to Set umask Using Symbolic Values?

The following symbolic values are used:

The following command will set umask to 077 i.e. a umask set to u=rwx,g=,o= will result in new files having the modes -rw——-, and new directories having the modes drwx——:

umask u=rwx,g=,o=
mkdir dir2
touch file2
ls -ld dir2 file2

Sample umask Values and File Creation Permissions

If umask value set toUser permissionGroup permissionOthers permission
000allallall
007allallnone
027allread / executenone

all = read, write and executable file permission

Limitations of the umask

umask and level of security

The umask command be used for setting different security levels as follows:

umask valueSecurity levelEffective permission (directory)
022Permissive755
026Moderate751
027Moderate750
077Severe700

For more information about the umask read the man page of bash or ksh or tcsh shell:

man bash
help umask
man chmod