How to give read write permission to a folder and its sub folders and files?

I have changed ownership of my localhost file on /var/www/ and its sub folders and given it permission 777. However, whenever I add a new folder in it the new folder does not automatically get that permission. How can I give a folder 777 permission forever so that if I add a new folder or file it gets the same permission?


You can use the recursive option any time. sudo chown -R username /var/www


As others have already mentioned, giving 777 permissions on /var/www is a really bad idea, especially in production.

A better solution would be to give write permissions only to the users who needs to modify the files. One of the ways to do that is:

  • create a new group

  • add the user(s) who needs to modify the data in /var/www to that group

  • recursively change the ownership of /var/www to that group

  • set umask on /var/www so all newly created files are owned by the group we've created.

Another option would be to use ACL, again, to give write permissions only to users who need them.

Here are detailed instructions on serverfault.

Generally, the webserver or other network services or system user accounts should have no write permissions to the files served by the webserver, as this opens a possibility of arbitrary code execution.


You should edit /etc/apache/envvars as root with your editor of choice.

Example: ALT+F2
gksudo gedit /etc/apache2/envvars

Go to the end of the file and add a line umask XXX.

Where umask is the binary opposite of the desired permissions value.
For 774 this would be 003. For 777 bad idea it would be 000.

Save.

Restart apache.

Example: sudo apache2ctl restart

This will only affect files/folders that are newly created by the apache user.

Additional note, read and write is 6 in the user, group, or anyone slot.


Permissions for newly created files are set by umask