umask - Unix

This is a discussion on umask - Unix ; Hi there, I am trying to understand 'umask. What I understand is that umask is used to specify the initial permissions for a file to be created. I ran umask in a shell and got '0022' as output. I changed ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: umask

  1. umask

    Hi there,

    I am trying to understand 'umask.
    What I understand is that umask is used to specify the initial permissions
    for a file to be created.
    I ran umask in a shell and got '0022' as output.
    I changed the umask to 0777.
    And then I created a file using 'touch test'.
    I expected 'touch' to be readable,writable and executable by all.
    but when I try to read the file using 'cat test' , it says
    'permission denied'.
    Also here is the 'ls -l touch' output,
    ---------- 1 saurabh saurabh 0 Sep 25 12:59 test

    Can someone please explain me the above.

    Saurabh

  2. Re: umask

    saurabh writes:

    > Hi there,
    >
    > I am trying to understand 'umask.
    > What I understand is that umask is used to specify the initial permissions
    > for a file to be created.
    > I ran umask in a shell and got '0022' as output.
    > I changed the umask to 0777.
    > And then I created a file using 'touch test'.
    > I expected 'touch' to be readable,writable and executable by all.
    > but when I try to read the file using 'cat test' , it says
    > 'permission denied'.
    > Also here is the 'ls -l touch' output,
    > ---------- 1 saurabh saurabh 0 Sep 25 12:59 test
    >
    > Can someone please explain me the above.


    The default permissions for created files are 0777 & ~umask. In other
    words, umask defines the permission bits NOT to be set.

    --
    Måns Rullgård
    mans@mansr.com

  3. Re: umask

    >What I understand is that umask is used to specify the initial permissions
    >for a file to be created.


    umask is used to specify the initial permissions TO TURN OFF on a file that
    is created. The permissions specified in the open call are also used, with
    the resulting permissions being:

    open_call_permissions & (~ umask)

    where & is bitwise AND and ~ is unary bitwise complement.

    >I ran umask in a shell and got '0022' as output.


    That means "turn OFF group write and world write".
    It's fairly common.

    >I changed the umask to 0777.


    That means "turn OFF all permissions". It's unusual and somewhat
    self-defeating. The equivalent to what you wanted is 000.

    >And then I created a file using 'touch test'.
    >I expected 'touch' to be readable,writable and executable by all.


    Don't expect that. touch specifies 0666, not 0777 in the open() call
    for permissions, and umask turns *OFF* permissions, not turning them
    on. Don't give execute permission to something not expected to be
    an executable (or a directory).

    >but when I try to read the file using 'cat test' , it says
    >'permission denied'.
    >Also here is the 'ls -l touch' output,
    >---------- 1 saurabh saurabh 0 Sep 25 12:59 test
    >
    >Can someone please explain me the above.


    Common values for umask are:

    000 wide-open permissions
    002 turn off world write
    022 turn off world and group write
    007 turn off world read/write/execute
    077 turn off group and world read/write/execute
    027 turn off group and world write and all world permissions

    Common values for the open permissions requested are:

    777 for an executable file
    666 for a non-executable file
    600 for a non-executable file that the application knows is private
    (e.g. temporary files holding email)


  4. Re: umask

    On September 27, 2008 15:08, in comp.unix.programmer, saurabh
    (saurabhenjoys@gmail.com) wrote:

    > Hi there,
    >
    > I am trying to understand 'umask.
    > What I understand is that umask is used to specify the initial permissions
    > for a file to be created.


    No. It does not.

    The initial permissions for the created file are specified by the code that
    calls the creat() or open() function. umask has nothing to do with these
    initial permissions

    What the umask does is *turn off* permissions. It does not turn them on.
    When you specify a umask of 0022, you tell the system that /if/ the creat()
    call set the group-write (0020) bit, turn it off, and /if/ the creat() call
    set the other-write (0002) bit, turn it off.

    > I ran umask in a shell and got '0022' as output.


    OK, so the default is to turn off the group-write and other-write
    permissions /if/ they were set by the creat() or open() call when creating
    the file.

    > I changed the umask to 0777.


    So, you have now told the system to turn off *all* permission bits when
    creat()ing or open()ing a file

    > And then I created a file using 'touch test'.
    > I expected 'touch' to be readable,writable and executable by all.


    Why? Remember, for every bit you turn *on* in umask, you are *turning off*
    the corresponding bit in all subsequent file creation calls.

    > but when I try to read the file using 'cat test' , it says
    > 'permission denied'.


    Of course. Because you *turned off* the "owner-read" bit with your umask

    > Also here is the 'ls -l touch' output,
    > ---------- 1 saurabh saurabh 0 Sep 25 12:59 test


    See? You said
    umask 0777
    which instructs the system to *turn off*
    User read, write, execute (7)
    Group read, write, execute (7)
    Other read, write, execute (7)
    permission bits.

    >
    > Can someone please explain me the above.


    Done.

    > Saurabh


    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------



  5. Re: umask

    Måns Rullgård wrote:
    > saurabh writes:
    >
    >> Hi there,
    >>
    >> I am trying to understand 'umask.
    >> What I understand is that umask is used to specify the initial
    >> permissions for a file to be created.
    >> I ran umask in a shell and got '0022' as output.
    >> I changed the umask to 0777.
    >> And then I created a file using 'touch test'.
    >> I expected 'touch' to be readable,writable and executable by all.
    >> but when I try to read the file using 'cat test' , it says
    >> 'permission denied'.
    >> Also here is the 'ls -l touch' output,
    >> ---------- 1 saurabh saurabh 0 Sep 25 12:59 test
    >>
    >> Can someone please explain me the above.

    >
    > The default permissions for created files are 0777 & ~umask.


    Not quite, this is true only for directories, for everything else it is
    0666 & ~umask.

    IOW: for directories the execute bits may get set (depending on umask), for
    everthing else they won't ever get set (no matter what the umask is).

    > In other
    > words, umask defines the permission bits NOT to be set.


    Bye, Jojo



  6. Re: umask

    In article ,
    "Joachim Schmitz" wrote:

    > Måns Rullgård wrote:
    > > saurabh writes:
    > >
    > >> Hi there,
    > >>
    > >> I am trying to understand 'umask.
    > >> What I understand is that umask is used to specify the initial
    > >> permissions for a file to be created.
    > >> I ran umask in a shell and got '0022' as output.
    > >> I changed the umask to 0777.
    > >> And then I created a file using 'touch test'.
    > >> I expected 'touch' to be readable,writable and executable by all.
    > >> but when I try to read the file using 'cat test' , it says
    > >> 'permission denied'.
    > >> Also here is the 'ls -l touch' output,
    > >> ---------- 1 saurabh saurabh 0 Sep 25 12:59 test
    > >>
    > >> Can someone please explain me the above.

    > >
    > > The default permissions for created files are 0777 & ~umask.

    >
    > Not quite, this is true only for directories, for everything else it is
    > 0666 & ~umask.
    >
    > IOW: for directories the execute bits may get set (depending on umask), for
    > everthing else they won't ever get set (no matter what the umask is).


    Actually, it's not dependent on the type of thing being created, it
    depends on the program doing the creating. For example, ld(1) (and by
    extension, cc when it's creating the executable) defaults to 0777. Most
    other programs that create ordinary files use 0666, since they presume
    they're creating data files rather than executables.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  7. Re: umask

    On Sat, 27 Sep 2008 14:53:56 -0500, Gordon Burditt wrote:

    > umask is used to specify the initial permissions TO TURN OFF on a file that
    > is created. The permissions specified in the open call are also used, with
    > the resulting permissions being:
    >
    > open_call_permissions & (~ umask)
    >
    > where & is bitwise AND and ~ is unary bitwise complement.


    Oh And what I thought was just the opposite,I thought it is used to turn
    them on.
    Thanks a lot all of you.
    I understood it.


  8. Re: umask

    saurabh wrote:
    > On Sat, 27 Sep 2008 14:53:56 -0500, Gordon Burditt wrote:
    >
    >> umask is used to specify the initial permissions TO TURN OFF on a file that
    >> is created. The permissions specified in the open call are also used, with
    >> the resulting permissions being:
    >>
    >> open_call_permissions & (~ umask)
    >>
    >> where & is bitwise AND and ~ is unary bitwise complement.

    >
    > Oh And what I thought was just the opposite,I thought it is used to turn
    > them on.
    > Thanks a lot all of you.
    > I understood it.
    >

    Don't worry, you are far from the first to make that mistake, and will
    be far from the last!

    I tend to scribble things on bits of paper when working out what to set
    it to.

    Cheers,
    Gary B-)

    --
    __________________________________________________ ____________________________
    Armful of chairs: Something some people would not know
    whether you were up them with or not
    - Barry Humphries

+ Reply to Thread