I was trying to open a file and write to it with PHP at /var/wwwfolder but it wasn't working so I did

sudo chmod 777 /var/www

now I want to set the permissions for /var/www to the default.
what are the default permissions for /var/www?


5 Answers 5


The default permission for /var/www itself is a pretty standard one: owner root:root and mod 755.

As for anything inside /var/www, that is one of the rare directories where you have the privilege of deciding for yourself what to put in it and what permissions everything in it should have. But what makes the most sense is:

  • Most files should be writable by whichever user or group is going to be writing to them most. You can set them to be owned by your user account. Or set up a custom group for your developers. Or if the files will be modified rarely and you want good security, you can go with root:root and just sudo in on the rare occasions they'll be modified.

  • Most files should not be world-writable. So, 644 for files, and 755 for directories is appropriate (or 664 and 775 if you want to give a group write access).

  • It is not recommended to set any of it to be writable by the web server, ie www-data, except for any specific files your web scripts to be able to write to. If so, it's better to set the user or group of those files to www-data than to make them world-writable. Note that any time the www-data user can write to any file within the web root, whether it's by setting the user or group on those files, or making them world-writable, it's a potential security problem. World-writable is just the worse of the two.

  • 2
    Upvoted as most conclusive answer here
    – Chris
    Aug 19, 2014 at 12:35
  • 2
    Now, this is a really good answer. :)
    – Banago
    Jan 11, 2015 at 12:35
  • to improve the answer, consider adding commands to set the appropriate parmissions May 20, 2018 at 22:08
  • Is there a recursive function that can change all folders to 755 and files to 644 inside var/www? Oct 16, 2018 at 1:04
  • Yep: stackoverflow.com/questions/18817744/… (the second answer is more elegant than the accepted one IMO) Oct 16, 2018 at 3:44

The permissions on this folder are:

chmod 755 /var/www/

and the files inside the folder are:

chmod 644 /var/www/file

Make sure the group is www-data on '/var/www'.

sudo chgrp www-data /var/www

Make it writable

sudo chmod 775 /var/www

set group id for subfolders

sudo chmod g+s /var/www

add your username to the group

sudo useradd -G www-data [USERNAME]
usermod -a -G www-data [USERNAME]

give yourself ownership

sudo chown [USERNAME] /var/www/
  • 10
    That is not the default permissions for /var/www, and it's a very bad idea security-wise to make the whole web root writable by www-data. Only do this when web scripts need write access to particular files, and even so only do it to those particular files, not to the entire web root. And the OP did not say he needed to do this, anyway, so this should not be a recommendation at all. Jul 9, 2014 at 2:03
  • 3
    Also it's a very bad idea to add yourself to the www-data group and treat that group in that manner. Where did you read to do this? It's particularly bad advice, almost maliciously so. Create your own groups when you need to give a group access to something: don't re-use the unprivileged groups that are intended for internal use by daemons. Jul 9, 2014 at 2:04
  • 1
    @neon_overload what do you recommend Dec 7, 2014 at 8:58
  • 1
    Exactly, what would your recommend @neon_overload?
    – Banago
    Jan 10, 2015 at 15:45
  • 1
    @Banago personnaly I recommend a +1 :)
    – kamil
    Jan 10, 2015 at 16:47
sudo adduser $USER www-data
sudo chown root:root /var/www

sudo chown -R $USER:www-data /var/www/*
sudo chmod -R 755 /var/www
  • 4
    Depending on the contents, it may be harmful to use the -R flag here. Since the OP did not use the -R flag, we should not recommend it to correct the wrong permissions they created. The -R flag is rarely helpful!
    – Zanna
    Nov 14, 2019 at 7:08

for what it's worth (I may be wrong-more testing to do) make sure that the files that you are transferring are "owned" by the user that logs in to BOTH machines. so for /var/www/website the webdir on the source machine is owned by kevin and the user on the dest machine was me too. i will do further testing once I have time. Thanks

  • 1
    This answer seems opinionated to me... May 26, 2021 at 16:12

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.