Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

Can you explain exactly how umask works in Unix and Linux?

Dave, I know you can't solve trivial questions for everyone, but if you could have someone help me with the umask command and the content on pp. 107-109 of your book Teach Yourself Unix is 24 Hours, I'd appreciate it. I've taken two hours to try to figure out how in the world umask 277 produces a read-only file. It seems to me it would be write only. Can you shed some light on this, please?

Dave's Answer:

Thanks for your note. Your quesiton isn't trivial (but if it were, I'd suggest you pop over to the fun site Trivial.Net, but that's another story entirely!)

In terms of umask, you need to look at things backwards...

When you see a umask of 277 it's the same as a pattern of -x- xxx xxx, right?

Now, since it's a umask you need to INVERT that to figure out how it turns into an actual permission string. That makes it x-x --- ---.

To make this a bit more complex, remember that the touch command shown in the book example that's stumped you can only give you read or write permissions, so that’s kinda/sorta another mask.

All that’s left is r-- --- --- and that’s exactly what permission you get.

Here's the actual example out of the book that you're asking about:

$ umask 277
$ touch testfile.277
$ ls -l testfile.277
-r--------  1 taylor  staff  0 16 Dec 22:44 testfile.277
$
Got it now?


Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

The octal level for Linux starts at 666. The magic numbers to remember are-

4 2 1
r w x

4+2+1 = 7 = rwx

4+0+1 = 5 = r-x

hence

4+2+0 = 6 = rw-

If you have a file with -rwx rwx rwx then you must have the octals 777 to match.

If your file is -rwx r-x r-x then the octals that match are 755

Now that we understand that we look at umask.

We start with a base figure of
666
-022
644 = rw- r-- r--

You will see your default umask level or setting in the /etc/profile file ... most of the time.

Some sysadmins will alter it to something stricter to help ensure more privacy between users ie 066 or 026

kjteoh

Posted by: kjteoh at August 5, 2005 2:15 PM

kjteoh ,thank you for good explanation!You shoud write book learn Linux in 12 hours ;)

Posted by: Sasha at May 7, 2007 3:55 PM

This is again regarding umask. Suppose i want my new file to have 755 pemissions. What should be the umask in this case now and most importantly how to calculate it?

Posted by: Abrar Ahmad at September 4, 2007 3:36 AM

umasks are always the opposite of what you want, so if you want to end up with 755 then you need to use 022 as the mask. 0+7 = 7, 5 + 2 = 7, etc.

Posted by: Dave Taylor at September 4, 2007 6:45 AM

Let me tell you what I am exactly doing on my system.

# umask 0022
# touch test
# vi test2

And this is the long listing of my files

-rw-r--r-- 1 root root 44 Sep 4 19:32 test
-rw-r--r-- 1 root root 6 Sep 4 19:32 test2

As you can see i am still unable to achieve desired default permissions for my newly created files which should be rwxr--r--(755)

I quickly went through an online reference for this but truly speaking i need somebody to explain it to me with an example.

http://en.wikipedia.org/wiki/Umask

Posted by: Abrar Ahmad at September 4, 2007 8:09 AM

By default, programs set the minimum logical permissions when it comes to execute: if you "vi test" it'll be a read-write file you create, it'll never create an executable file. Other than manually using "chmod" the only programs I know that create *executable* files are compilers and the like.

Posted by: Dave Taylor at September 4, 2007 8:37 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]