1

Before I say anything, I know how permission for groups and users works.

My setup is:

a. my user is monkey and is in the group www-data

groups monkey

monkey : monkey adm cdrom sudo dip www-data plugdev lpadmin sambashare

b. the root folder of apache is /home/monkey/var/www and has the permissions 0775 with owner www-data and group www-data

ls -la /home/monkey/var/www

total 8 drwxrwxr-x 2 www-data www-data 4096 Aug 12 10:37 . drwxr-xr-x 3 root root 4096 Aug 12 10:37 ..

c. But I'm unable to write in /home/monkey/var/www/

monkey@ubuntu:~$ cp robots.txt var/www/robots.txt

cp: cannot create regular file âvar/www/robots.txtâ: Permission denied

The robots.txt file was created under monkey user.

What am I missing?

3

your parent folder permissions (var set to 755 and owner of root) limits your child folder. you need to change your permissions to var or change the owner to www-data:www-data and perms to 775

EDIT

after some digging, the user needs to switch group with newgrp <group-name> before doing any operation. to return to the default group on that shell, type exit or Ctrl+D

  • I did that and still cannot write to that folder pastebin.com/WamMnweF. For me this is very strange. – machineaddict Aug 12 '14 at 8:24
  • hmm do a sudo grpck to check if there's a problem with your groups – AlexGreg Aug 12 '14 at 8:38
  • It returned nothing. But this problem I have on ANY linux distribution or computer. I know it for about 6 years and I just decided to ask about it. I think anyone can replicate it. – machineaddict Aug 12 '14 at 8:42
  • well, switching your default group to your user (newgrp www-data) and copying after it works – AlexGreg Aug 12 '14 at 8:45
  • It worked. Please put the command in your answer. I don't understand why it doesn't work for the first time the group was added to the user. I even rebooted. So, do I have to issue newgrp command everytime I login? – machineaddict Aug 12 '14 at 8:51
1

User has a primary group and can have several additional groups. User primary group is set /etc/passwd, ie:

www-data:x:30:40:www-data:/var/www:/usr/sbin/nologin

says user 'www-data' is a member of group id 40.

You can add user to additional groups in /etc/group, but when you create a file, by default system will set user primary group as an owner, not one of additional groups. Some Unixes check for directory group, and if user is a member of this group, new file has a group owner taken from directory, not from user.

So when you create a file, by default you do it as a user 'monkey', group 'monkey'. And since folder is owned by:

  • user 'www-data', not by 'monkey' - user permissions are not used
  • group 'www-data', not by 'monkey' - group permissions are also not used
  • 'other' permissions are used, and t

Using newgr command you can switch your primary group to one of the additional groups, so effectively set group owner for new file creation.

Using umask command you can tune permissions used for new file creation.

Does it help now?

  • Could you give me an example for my case? – machineaddict Aug 13 '14 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.