Umask: Difference between revisions

From Hackepedia
Jump to navigationJump to search
attempt at explanation of umask
 
Rewrite.
 
Line 1: Line 1:
umask is a mask to the default permissions that a file is written. Consider this if you write a file its default mode is 0666.  This means anyone can delete it, write to it and read from it. If you had a umask of 077, the bits would be masked when you write the file. Here is the formula for determining the mode after umask is applied:  '''mode &= ~(umask)'''Let's look at this in detail what does it mean?  mode is mode (bitwise) AND unary inverse of umask (every bit set to 0 becomes 1 and vice versa).  So 0666 AND 0700 is 0600, and that is what your final mode is. So a umask of 077 is very safe, noone but the user gets any permissions.  What if you want to restrict only writing to a file created by group and others?  It would be a umask of 022, so 0666 AND 0755 and you have 0644.
umask is a mask agaist the default [[permissions]] used when a file is created.
 
If you write a file, its default mode is 0666.  This means anyone can read or write it (actually removing a file requires directory write access, which is independent of the permissions on a specific file.
 
If you had a umask of 077, ''all'' permissions for "group" and "other" will be masked (e.g. disallowed) when a new file is created.
 
Here is the formula for determining the mode after umask is applied:  <code>mode & !umask</code>In "English," this means that the you take the umask, apply a unary inverse (e.g. a logical <code>NOT</code>), then bitwise <code>AND</code> this value against the default mode of the new file.
 
So, given a default mode of 666, and a umask of 027, the following math is performed (behind the scenes!):
 
666 & !027 = 640
 
Which is <code>rw-r----</code>Working out the binary, we have this:
 
  110110110  (default mode 666)
  000010111 (umask of octal 027, in binary)
 
  Negate the umask:
  !000010111 = 111101000
 
  Bitwise AND the negated umask against the default mode
    110110110
  & 111101000
  -----------
    110100000 = 640
 
Simple, right? ;-)
 
Just remember that the umask is used to set the permissions that you want ''prevent'' from getting set by default, and you'll be okay.
 
Common mask settings:
{|border=2 cellpadding="2"
|Umask
|Default file permissions
|Notes
|-
|077
|600 (rw-------)
|Very restrictive, good for root, and paranoid users
|-
|027
|640 (rw-r-----)
|Common for users who want to let certain people read files
|-
|002
|660 (rw-rw-r--)
|Good for collaboration and active sharing of files.
|-
|}

Latest revision as of 14:37, 12 January 2006

umask is a mask agaist the default permissions used when a file is created.

If you write a file, its default mode is 0666. This means anyone can read or write it (actually removing a file requires directory write access, which is independent of the permissions on a specific file.

If you had a umask of 077, all permissions for "group" and "other" will be masked (e.g. disallowed) when a new file is created.

Here is the formula for determining the mode after umask is applied: mode & !umask. In "English," this means that the you take the umask, apply a unary inverse (e.g. a logical NOT), then bitwise AND this value against the default mode of the new file.

So, given a default mode of 666, and a umask of 027, the following math is performed (behind the scenes!):

666 & !027 = 640

Which is rw-r----. Working out the binary, we have this:

 110110110  (default mode 666)
 000010111  (umask of octal 027, in binary)
 
 Negate the umask:
 !000010111 = 111101000
 
 Bitwise AND the negated umask against the default mode
   110110110
 & 111101000
 -----------
   110100000 = 640 
 

Simple, right? ;-)

Just remember that the umask is used to set the permissions that you want prevent from getting set by default, and you'll be okay.

Common mask settings:

Umask Default file permissions Notes
077 600 (rw-------) Very restrictive, good for root, and paranoid users
027 640 (rw-r-----) Common for users who want to let certain people read files
002 660 (rw-rw-r--) Good for collaboration and active sharing of files.