Unable to write in folder in which my user has write access

6,533

Solution 1

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

Solution 2

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?

Share:
6,533

Related videos on Youtube

machineaddict
Author by

machineaddict

Updated on September 18, 2022

Comments

  • machineaddict
    machineaddict over 1 year

    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?

  • machineaddict
    machineaddict over 9 years
    I did that and still cannot write to that folder pastebin.com/WamMnweF. For me this is very strange.
  • AlexGreg
    AlexGreg over 9 years
    hmm do a sudo grpck to check if there's a problem with your groups
  • machineaddict
    machineaddict over 9 years
    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.
  • AlexGreg
    AlexGreg over 9 years
    well, switching your default group to your user (newgrp www-data) and copying after it works
  • machineaddict
    machineaddict over 9 years
    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?
  • AlexGreg
    AlexGreg over 9 years
    no..with newgrp command you login to that group..after finishing doing what you need to do just type exit and you'll return to your default group
  • machineaddict
    machineaddict over 9 years
    So basically, I'm logged-in in only one group at a time? I really need to know WHY I did that in order to work.
  • machineaddict
    machineaddict over 9 years
    Could you give me an example for my case?