Operating System - HP-UX
1827293 Members
1333 Online
109717 Solutions
New Discussion

Trouble understanding umask and permissions on new files

 
SOLVED
Go to solution
Paul Neralich
Occasional Contributor

Trouble understanding umask and permissions on new files

When I login to my HP-UX 10.20 server, "umask" returns a value of 022 (umask -S returns u=rwx,g=rx,o=rx). Therefore, I would expect that when I create a new file using vi, the permission would be -rwxr-xr-x
However, when I create a file with vi it has the permission -rw-r--r--
I thought I understood permissions and umask but obviously there is something I'm missing.
7 REPLIES 7
Sridhar Bhaskarla
Honored Contributor

Re: Trouble understanding umask and permissions on new files

Hi,

It is for the creation of the files and the setting of execution bit is to be done seperately using "chmod" command. With the umask 022, the files get 'created' with the permissions -rw-r--r--.

You don't have much choice there.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sundar_7
Honored Contributor

Re: Trouble understanding umask and permissions on new files

umask only masks the default file and directory permissions

default file permission: -rw-rw-rw
default dir permission: -rwxrwxrwx

if you apply umkas of 022

access permissions of the new file created would ne = 666 - 022 = 644 (rw-r--r--)

for dirs = 777 - 022 = 755 (rwxr-xr-x)
Learn What to do ,How to do and more importantly When to do ?
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Trouble understanding umask and permissions on new files

The "something" is the default mode of the file. In the shell, the default mode of a regular file is 666 and that of a directory is 777. Umask then "subtracts" (not quite an accurate term but close enough for this) from this default mode. If you need to set an executable bit from a shell created file, you must issue an explicit chmod after first creating the file.

If you are developing in C, for example, both the open() and creat() system calls allow you to creat a file with whatever mode you wish in one step.
If it ain't broke, I can fix that.
curt larson_1
Honored Contributor

Re: Trouble understanding umask and permissions on new files

when setting the permissions for a new file, umask works in conjunction with the file creation mask. unix utilities use a file creation mask of rw-rw-rw-. then using your umask of 022, gives final permissions of rw-r--r--.

by default when creating a file you have to do a chmod to make the file executable. just safer to do things that way.
Paul Neralich
Occasional Contributor

Re: Trouble understanding umask and permissions on new files

Thanks guys, I got exactly what I needed.

- Paul N.
Dani Seely
Valued Contributor

Re: Trouble understanding umask and permissions on new files

Hello Paul,
When you have "umask 022" in your startup file (.cshrc or .profile) you will get file permissions of 644 instead of 755. This is a security "feature". Under modern UNIX versions, umask cannot be used to set execute permissions on files, only directories, where execute is
synonymous with "searchable".

With "umask 022" I see people believing that the "default permission" is 777. They are wrong. 777 is "global" read, write, and execute permissions, something you do not generally want to happen. Thus the need to chmod the file to make it executable.
Together We Stand!
Dani Seely
Valued Contributor

Re: Trouble understanding umask and permissions on new files

Hey Paul,
Thought I'd try to do a little explanation of file permissions. Each digit corresponds
to the permissions for user, group, and other ... respectively.

The umask is subtracted from 777 / 666 to give the permission set.

So, since you have a umask 022, 666-022 leaves 644 (rw-r--r--). As r=4, w=2, x=1,
644 is user rw- (4+2) group r-- (4) others r-- (4).

Hope this adds some clarity.
Together We Stand!