Difference between revisions of "Umask"

From ArchWiki
Jump to: navigation, search
m (whitespace fixes to make explanation easier to understand)
(text restructured; try to explain the bit-wise operations)
Line 16: Line 16:
 
Save and close the file. Changes will take effect after next login.
 
Save and close the file. Changes will take effect after next login.
  
But what is {{ic|0022}} and {{ic|0002}}?
+
==Meaning of the umask value==
  
The default umask {{ic|0002}} is used for regular users. With this mask, default directory permissions are {{ic|775}}, and default file permissions are {{ic|664}}.
+
The base permissions for directories are {{ic|0777}} ({{ic|rwxrwxrwx}}) and for files they are {{ic|0666}} ({{ic|rw-rw-rw}}).
  
The default umask for the root user is {{ic|0022}}, and as a result, default directory permissions are {{ic|755}}, and default file permissions are {{ic|644}}.
+
The default umask {{ic|0002}} is used for regular users. That means the write bit for all other users ({{ic|2}} in the last place) is cleared on new files and directories. This mask results in default permissions of {{ic|775}} for directories and {{ic|664}} for files.
  
For directories, the base permissions are {{ic|0777}} ({{ic|rwxrwxrwx}}) and for files they are {{ic|0666}} ({{ic|rw-rw-rw}}).
+
The default umask for the root user is {{ic|0022}}. That means the write bit for the group ({{ic|2}} in the 3rd place) and all other users (({{ic|2}} in the 4th place)) are cleared. This mask results in default permissions of {{ic|755}} for directories and {{ic|644}} for files.
  
 
To calculate directory permissions for a umask value of {{ic|022}} (root user):
 
To calculate directory permissions for a umask value of {{ic|022}} (root user):
  Default permission:   777
+
  Base permission:     777
 
  Subtract umask value: 022 (-)
 
  Subtract umask value: 022 (-)
 
  Directory permission: 755
 
  Directory permission: 755
  
 
To calculate file permissions for a umask value of {{ic|022}} (root user):
 
To calculate file permissions for a umask value of {{ic|022}} (root user):
  Default permission:   666
+
  Base permission:     666
 
  Subtract umask value: 022 (-)
 
  Subtract umask value: 022 (-)
 
  File permission:      644
 
  File permission:      644
  
 
The following example explains the steps needed to set a umask value that will result in permission values {{ic|700}} for directories and {{ic|600}} for user files. The idea very simply is that only the user will be allowed to read or write the file, or to access the contents of the directory.
 
The following example explains the steps needed to set a umask value that will result in permission values {{ic|700}} for directories and {{ic|600}} for user files. The idea very simply is that only the user will be allowed to read or write the file, or to access the contents of the directory.
  Default permissions:   777 / 666
+
  Base permission:       777 / 666
  Subtract umask value:  077 (-)
+
  Subtract umask value:  077 / 077 (-)
 
  Resulting permissions: 700 / 600
 
  Resulting permissions: 700 / 600
  

Revision as of 22:42, 6 February 2014

The user file-creation mode mask (umask) is used 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.

Setting the UMASK

You can setup the umask value in /etc/bashrc or /etc/profile for all users. By default, most Linux distros set it to 0022 (022) or 0002 (002).

Open /etc/profile (global) or ~/.bashrc file

# vi /etc/profile

or

$ vi ~/.bashrc

Append/modify the following line to setup a new umask:

umask 022

Save and close the file. Changes will take effect after next login.

Meaning of the umask value

The base permissions for directories are 0777 (rwxrwxrwx) and for files they are 0666 (rw-rw-rw).

The default umask 0002 is used for regular users. That means the write bit for all other users (2 in the last place) is cleared on new files and directories. This mask results in default permissions of 775 for directories and 664 for files.

The default umask for the root user is 0022. That means the write bit for the group (2 in the 3rd place) and all other users ((2 in the 4th place)) are cleared. This mask results in default permissions of 755 for directories and 644 for files.

To calculate directory permissions for a umask value of 022 (root user):

Base permission:      777
Subtract umask value: 022 (-)
Directory permission: 755

To calculate file permissions for a umask value of 022 (root user):

Base permission:      666
Subtract umask value: 022 (-)
File permission:      644

The following example explains the steps needed to set a umask value that will result in permission values 700 for directories and 600 for user files. The idea very simply is that only the user will be allowed to read or write the file, or to access the contents of the directory.

Base permission:       777 / 666
Subtract umask value:  077 / 077 (-)
Resulting permissions: 700 / 600
$ umask 077
$ touch file.txt
$ mkdir directory
$ ls -ld file.txt directory

Output:

drwx------ 2 vivek vivek 4096 2007-02-01 02:21 directory
-rw------- 1 vivek vivek    0 2007-02-01 02:21 file.txt
Sample umask values and permissions
umask value 	User 	Group 	Others
0000 		all 	all 	all
0007 		all 	all 	none
0027 		all 	read 	none

For more information, see man bash and man umask.

See Also

http://www.cyberciti.biz/tips/understanding-linux-unix-umask-value-usage.html (the source of this article)