nixCraft Poll

Topics

What is umask and how to setup default umask under Linux?

Posted by Vivek Gite [Last updated: March 8, 2007]

Anil ask a question (via email):

What is umask and how is it determined on a Linux system?

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 .

Procedure to setup default umask

You can setup umask in /etc/bashrc or /etc/profile file for all users. By default most Linux distro set it to 0022 (022) or 0002 (002).
Open /etc/profile (global) or ~/.bashrc file
# 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.

But what is 0022 and 0002?

The default umask 0002 used for normal user. With this mask default directory permissions are 775 and default file permissions are 664.

The default umask for the root user is 0022 result into default directory permissions are 755 and default file permissions are 644.

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

To calculate file permission for 022 (root user):

Default Permissions: 777
Subtract umask value: 022 (-)
Allowed Permissions: 755

To calculate directory permission for 022 umaks (root user):

Default Permissions: 666
Subtract umask value: 022 (-)
Allowed Permissions: 644

The following example explains the steps needed to set umask for permissions 700 for user files. The idea very simply only user is allowed to read or write file.

Default Permissions: 777
Subtract umask value: 077 (-)
Allowed Permissions: 700

$ umask 077
$ touch file.txt
$ ls -l file.txt

Output:

-rw------- 1 vivek vivek 0 2007-02-01 02:21 file.txt

Sample umask values and permission

umask value User Group Others
0000 all all all
0007 all all none
0027 all r/w none

For more information read man page of bash:
man bash
help umask

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Dmitry Says:

    Reply from text:
    —————————-
    To calculate file permission for 022 (root user):
    Default Permissions: 777

    To calculate directory permission for 022 umaks (root user):
    Default Permissions: 666

    ————————————
    It’s wrong, isn’t it?
    The right answer is:

    To calculate _file_ permission for 022 (root user):
    Default Permissions: _666_
    etc.
    To calculate _directory_ permission for 022 umaks (root user):
    Default Permissions: _777_
    etc.

  2. WGriffin Says:

    The title says “How to setup default umask”
    You never mention how to actually set the default up.

  3. nixcraft Says:

    WGriffin,

    Opps. Post has been updated. Thanks for heads up!

  4. RAHUL Says:

    Yes Guy !

    Its an excellent site for newbie.
    I like it most…

    I think its help all Linuxx user.

  5. ahmed Says:

    thanks. very helpful

  6. posicionamiento Says:

    thanks guys….
    I like such short tutorials …;)
    Cheers

  7. niez Says:

    Hi there!

    I’ve tried changing umask on my GNU/Linux box (Gentoo). Is’s look like 0000 and 0111 is the same mask. What’s going on?

  8. Raghu Says:

    Default (initial) Run level for root?

  9. Sunny Thakur Says:

    Hi,
    I have done this thing 3 times….but not any affect on umask default permissions.
    i used :
    # umask 0077 command for temporarily umask….

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.