15

I have a directory that has the following permissions set:

drwxr-s---. user group folder

On the desktop, I access this folder and right click to create a new file call foo.txt. Then using the terminal, I created another file using the command $ touch bar.txt.

When I check the permissions for these files, I have:

-rw-r--r--. user group foo.txt
-rw-rw-r--. user group bar.txt

I was expecting -rw-r-----. user group. How did the extra write permission for group and read permission for others come about?

1 Answers1

19

setguid

There are 2 forces here at work. The first is the setgid bit that's enabled on the folder, folder.

drwxr-s---. user group folder

That's the s in the pack of characters at the beginning of this line. They're grouped thusly:

d - directory
rwx - read/write/execute bits for user
r-s - read/execute/setuid bits for group
--- - nothing for other users

The r-s means that any files or directories created inside this folder will have the group automatically set to the group group.

That's what caused the files foo.txt and bar.txt to be created like so:

-rw-r--r--. user group foo.txt
-rw-rw-r--. user group bar.txt

permissions & umask

The permissions you're seeing are another matter. These are governed by the settings for your umask. You can see what your umask is set to with the command umask:

$ umask
0002

NOTE: these bits are also called "mode" bits.

It's a mask so it will disable any of the bits related to permissions which are enabled. In this example the only bit I want off is the write permissions for other.

0 - skipping for this conversation
0 - value of user bits
0 - value of group bits
2 - value of other bits

The representation of the "bits" in this command are in decimal form. So a 2 equates to 010 in binary form, which is the write bit. A 4 (100) would mean you want read disabled. A 7 (111) means you want read/write/execute all disabled. Building it up from here:

$ umask 007

Would disable the read/write/execute bits for other users.

So then what about your files?

Well the umask governs the permissions that will get set when a new file is created. So if we had the following umask set:

$ umask 007

And started touching new files, we'd see them created like so:

$ touch newfile1.txt newfile2.txt
$ ls -l |grep newfile
-rw-rw----   1 saml saml        0 Nov  3 22:34 newfile1.txt
-rw-rw----   1 saml saml        0 Nov  3 22:34 newfile2.txt

If we changed it to something else, say this:

$ umask 037
$ ls -l |grep newfile
-rw-rw----   1 saml saml        0 Nov  3 22:36 newfile1.txt
-rw-rw----   1 saml saml        0 Nov  3 22:36 newfile2.txt
-rw-r-----   1 saml saml        0 Nov  3 22:35 newfile3.txt
-rw-r-----   1 saml saml        0 Nov  3 22:35 newfile4.txt

It won't have any impact on files that we've already created though. See here:

$ umask
0037
$ touch newfile1.txt newfile2.txt
$ ls -l | grep newfile
-rw-rw----   1 saml saml        0 Nov  3 22:37 newfile1.txt
-rw-rw----   1 saml saml        0 Nov  3 22:37 newfile2.txt
-rw-r-----   1 saml saml        0 Nov  3 22:35 newfile3.txt
-rw-r-----   1 saml saml        0 Nov  3 22:35 newfile4.txt

So then what's going on with the file browser?

The umask is what I'd called a "soft" setting. It is by no means absolute and can be by-passed fairly easily in Unix in a number of ways. Many of the tools take switches which allow you to specify the permissions as part of their operation.

Take mkdir for example:

$ umask
0037

$ mkdir -m 777 somedir1
$

$ ls -ld somedir1
drwxrwxrwx 2 saml saml 4096 Nov  3 22:44 somedir1

With the -m switch we can override umask. The touch command doesn't have this facility so you have to get creative. See this U&L Q&A titled: Can files be created with permissions set on the command line? for just such methods.

Other ways? Just override umask. The file browser is most likely either doing this or just completely ignoring the umask and laying down the file using whatever permissions it's configured to do as.

slm
  • 369,824
  • I thought umask is supposed to remove privileges, not add. Or am I missing something? – Question Overflow Nov 04 '13 at 08:32
  • @QuestionOverflow - you mask controls what permissions are allowed when files and directories are created. Hence its name. It masks bits from being used during the creation process. – slm Nov 04 '13 at 08:34
  • My umask reads 0002. If umask does set permissions instead of remove permissions, then the execute bit would be set for both user and group when I do touch, but it is not. – Question Overflow Nov 04 '13 at 08:41
  • @QuestionOverflow - that's a limitation of the touch command. See the link I included in my A. This link: http://unix.stackexchange.com/questions/47178/can-files-be-created-with-permissions-set-on-the-command-line – slm Nov 04 '13 at 08:48
  • Just to clarify a few points based on the comments: 1) permissions of new file is always determined by umask + the program used to create the file, 2) permissions are never inherited from parent directory. – Question Overflow Nov 04 '13 at 09:05
  • @QuestionOverflow - (1) + (2) are both correct. I would just state (1) as "..determined by umask and/or the program used to create the file..." – slm Nov 04 '13 at 09:09
  • Impressively through reply. acl might be worth a mention. – Faheem Mitha Nov 04 '13 at 09:18
  • @FaheemMitha - yeah I was thinking of mentioning it but didn't want to go overboard. – slm Nov 04 '13 at 09:19
  • Well, thanks a lot for taking the time to reply. I really appreciate. I have read ACL somewhere, but it seems to be directory specific only. – Question Overflow Nov 04 '13 at 09:31
  • @QuestionOverflow - ACLs - Access Control Lists can be used on directories and files too (I believe). They're kind of beyond scope for this Q, I'd google ACL linux, you'll find dozens of tutorials if you're curious. – slm Nov 04 '13 at 09:42