How to change the umask in Linux and Unix

The umask is used for setting up the default permissions in Linux and Unix. The default umask valus is 0022. The umask representation is diffrent than the chmod octal represetantion. I will explain you why.

The default permission of the directories is 0777 and the default permission for the files is 0666.

The 0022 umask for the files means: 0666 – 0022 = 0644

The 0022 umask for the directories means: 0777 – 0022 = 0755

To find out what a umask value means, do 0666 – umask_value for files and 0777 – umask_value for directories:

Numerical values and what they represent:

  • 0 (zero) : rwx
  • 1 : rw
  • 2 : rx
  • 3 : r
  • 4 : wx
  • 5 : w
  • 6 : x
  • 7 : no permissions

To change the umask, use: umask octal_value

I will set the umask 0077 and create a file and folder. The newdir will have rwx for the user and none for the other and the new file will have only rw for the user and none for the others. The 0077 umaskĀ  is good for a private system. Only the owner can read or modify the file.

$ umask 0077
$ touch file1
$ mkdir dir1
$ ls -l
drwx------ 2 razvan razvan 4096 2012-07-01 03:34 dir1
-rw------- 1 razvan razvan 0 2012-07-01 03:34 file1

The umask is persistent. If you set, for example: umask 0077 it will not modify until the first restart or log off. To set the umask permanently, write it in your shell’s configuration file: ~/.bashrc for bash, or in the /etc/profile file, if you want to set a default umask for the entire system.

$ echo "umask 0077" >> ~/.bashrc

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!