How can I give write-access of a folder to all users in linux?

762,846

Solution 1

To best share with multiple users who should be able to write in /var/www, it should be assigned a common group. For example the default group for web content on Ubuntu and Debian is www-data. Make sure all the users who need write access to /var/www are in this group.

sudo usermod -a -G www-data <some_user>

Then set the correct permissions on /var/www.

sudo chgrp -R www-data /var/www
sudo chmod -R g+w /var/www

Additionally, you should make the directory and all directories below it "set GID", so that all new files and directories created under /var/www are owned by the www-data group.

sudo find /var/www -type d -exec chmod 2775 {} \;    

Find all files in /var/www and add read and write permission for owner and group:

sudo find /var/www -type f -exec chmod ug+rw {} \;

You might have to log out and log back in to be able to make changes if you're editing permission for your own account.

Solution 2

Read+Write:

sudo chmod -R a+rw /var/www

Read+Write+Execute:

sudo chmod -R a+rwx /var/www

Solution 3

There's a simpler way to do this, try doing this command.

sudo chmod -R 757 /var/www

Essentially, the chmod command alters permissions and the -R switch affects all users. Then it is simply giving the correct permissions to use.

Solution 4

You can also replicate what jtimberman suggested using access control lists. The setfacl command accepts -s to replace an existing ACL or -m to modify it; -R to make directory ACLs recursive; and -d to make the specified settings the default, which is useful if you're anticipating forthcoming user accounts.

These just set the permissions as you would for the user, group, other, and mask using chmod:

setfacl -m u::rwx, g::r-x, o::---, m:rwx DIRECTORY

And this could be how you'd do it for a specified user or his/her group:

setfacl -m u:USERNAME:rwx, g:USERNAME:r-x DIRECTORY

And of course, the strength is that you can designate any specific user, multiple users, etc., all without having to modify your group settings. And unlike chmod, if you want some groupies to have access to one directory and other groupies to have access only to another, it's actually possible with setfacl. Finally, to view a directory's ACLs, run getfacl:

getfacl DIRECTORY

And you can specify -R to see the ACLs for subdirectories or -d to see the defaults.

Solution 5

The quick & easy answer -

a. Add (-a) your user (user_name) to the group (-G) www-data.

sudo usermod -a -G www-data user_name

b. Give the Group (g) the same (=) permissions as the owning User (u) of /var/www Recursively (-R).

sudo chmod -R g=u /var/www

Explanation: Apache 2 on Debian/Ubuntu sets the User & Group www-data as the Owner of /var/www. The default permissions for the User are "View & Modify Content", however the Group can only "View Content". So adding yourself to the www-data Group and giving it the same permissions as the wwww-data User, is a quick and easy way to get developing. I do this for all my localhost (PC/Laptop) Web Development environments.

Share:
762,846

Related videos on Youtube

Carson Myers
Author by

Carson Myers

Updated on September 17, 2022

Comments

  • Carson Myers
    Carson Myers almost 2 years

    I installed apache2 on Ubuntu just now, and noticed that the /var/www folder is protected. I can just sudo everything but I would rather just give it write access.

    How can I do this?

    I tried sudo chmod 7777 /var/www but it didn't work.

    • kwutchak
      kwutchak almost 15 years
      Is this a publicly accessible server, or does it have no direct connection to the internet? If the former it is important that you consider security decisions - servers on the internet are constantly under attack (have a look in your /var/log/messages or equivalent).
    • Carson Myers
      Carson Myers almost 15 years
      this is just my laptop, it is not accessible from the internet.
  • Carson Myers
    Carson Myers almost 15 years
    that's pretty tedious... I can't just give users access to it, as if it were any other folder?
  • jtimberman
    jtimberman almost 15 years
    That is how you give access to it. It's quicker to copy and paste the commands than try to navigate through a GUI file manager's dialogs to do the same thing. Long term it helps you if you read the manual pages for chmod and chgrp, at least ("man chmod").
  • Carson Myers
    Carson Myers almost 15 years
    perhaps I didn't understand the commands the first time I read it, and your edit makes it much more clear.
  • polson136
    polson136 almost 15 years
    +1 for guid to force apache permissions. works well with umask of 027. If something needs writes access, it's as easy as chmod g+w dir/
  • n0pe
    n0pe almost 13 years
    Could you give some insight on what the command does for the OP?
  • recluze
    recluze over 12 years
    Wow, that's bad! You don't give a 777 with a -R -- especially if the person asking the question is a newbie and doesn't understand the risks.
  • Michael B
    Michael B almost 12 years
    Just wanted to chime in and say this is awesome advice that is very concise and simple to follow while explaining everything you need to know. Well said. Upvoted and starred for future use. :)
  • Autodidact
    Autodidact over 11 years
    @jtimberman The last 2 cmds looks like it should only run once. How to make it idempotent in chef? Actually I'm using these steps in chef!
  • vgoff
    vgoff over 11 years
    True enough @recluze.
  • Blacklight Shining
    Blacklight Shining over 11 years
    -1 for lack of explanation. Besides, chmod $permissions -R $file isn't valid…
  • Jim
    Jim almost 11 years
    Helped me out more than anything else I could find. Thanks.
  • Moataz Elmasry
    Moataz Elmasry over 10 years
    it is options then permissions not the other way around
  • JorgeArtware
    JorgeArtware almost 10 years
    you may want to add that for changes to take effect you need to reload user group assignments, maybe a clean system restart would be better if that's convenient for you
  • detly
    detly almost 10 years
    Why is your group permission command sudo chmod -R g+w and not g+rw or g+rwX?
  • Bwyss
    Bwyss almost 8 years
    sudo chmod -R 757 /var/www
  • Tisch
    Tisch almost 8 years
    so why offer this as a solution, and then immediately discredit it? -1 vote
  • Filip Luchianenco
    Filip Luchianenco over 7 years
    +1 for logout and login again; it worked just after I did that. Thanks
  • Adi Prasetyo
    Adi Prasetyo over 6 years
    please someone experienced suggest an edit with brief explanation about this.
  • Marian
    Marian about 6 years
    Be careful: This really grants access to everybody on the system, which can be dangerous. (I know this is what was asked, but most of the time it is not what you really want.)
  • vbarbarosh
    vbarbarosh over 5 years
    How to make a brand new file have w permission for group?
  • Alex Yu
    Alex Yu almost 3 years
    -R is recursive, it's not "affects all users"