I’m running a webserver and FTP server, wherein /var/www is bound to /home/user/www.

I set both directories to chmod 777 (which is fine since it’s for testing only).

I can upload files into /home/user/www, but whenever I create a new directory, I always have to run chmod 777 on that folder.

Otherwise, when I try to browse it, I get the error message

You don't have permission to access /test/ on this server.

Is there a way I could make all sub-folders inside /var/www be accessible by anyone? Or could their permissions be automatically set to 777? It’s annoying that I have to type chmod 777 every time.


This is bad practice, but hopefully you are just using this for development, or you have another good reason. You can specify the permissions when you create a directory using the -m option:

mkdir -m 777 dirname

Or you can set the permissions recursively.

sudo chmod -R 777 /var/www

Before using either of these, really consider if you want your filesystem to be so accessible.

Edit: As mentioned by Rinzwind here is a better way of accomplishing what you want.

Check what group owns your /var/www directory and add your user to that group.

sudo adduser yourusername group

The group is probably www-data.

Then you will be OK with setting your permissions to 775.

| improve this answer | |
  • 4
    This does what you have asked for but please read this first Why should /var/www not have chmod 777. It is really not recommended practice. – Warren Hill Jun 3 '13 at 14:56
  • 9
    Please do not help people with a method that is bad practice. I would prefer if you explained how to do it: by adding his user to a group www-data or apache ;) – Rinzwind Jun 3 '13 at 15:03
  • 5
    Education comes first. Helping people screw up their machine is not the Linux way. So I strongly disagree with you. – Rinzwind Jun 3 '13 at 15:39
  • 2
    The asker did say this is only for testing. I can definitely understand wanting your development side to be easily accessible so you can work more quickly. I am going to give him the benefit of the doubt, that he is not foolish enough to do this with a production server. – Dan Jun 3 '13 at 16:18
  • 1
    @dan08 There are better ways to test, including using your home directory. See askubuntu.com/questions/46331/… and kimbriggs.com/computers/computer-notes/linux-notes/… . There are secure ways to accomplish these goals ;) – Panther Jun 3 '13 at 17:44

Files and directories in Unix may have three types of permissions: read (r), write (w), and execute (x). Each permission may be on or off for each of three categories of users: the file or directory owner; other people in the same group as the owner; and all others. To change the mode of a file, use the chmod command. The general form is chmod X@Y file1 file2 ...

chmod a-w file (removes all writing permissions)
chmod o+x file (sets execute permissions for other (public permissions))
chmod u=rx file        (Give the owner rx permissions, not w)
chmod go-rwx file      (Deny rwx permission for group, others)
chmod g+w file         (Give write permission to the group)
chmod a+x file1 file2  (Give execute permission to everybody)
chmod g+rx,o+x file    (OK to combine like this with a comma)

u = user that owns the file
g = group that owns the file
o = other (everyone else)
a = all (everybody)

r = read aces to the file
w = write access
x = execute (run) access 
| improve this answer | |

Public service announcement:

Don't ever use chmod 777 to fix problems

  • It's a major security risk if you run any services available to the public, especially web applications (eg PHP).

    The OS's security model assumes that many services (such as your web server) run with reduced privileges, to prevent them being able to modify files. Setting 777 on files breaks this design.

    A remote user could write to or upload files and then trick the server (or some other process on your system) into reading or executing them. Scripts or software may have flaws that allow this. It's very difficult to lock down every single way this could happen if there are world-writable directories.

  • Used in certain system directories (/usr, /etc, /var, and so on), it can break your system in surprising ways.

    Some essential system files need special permissions such as setuid/setgid permissions in order to run. For example, sudo. Avoid changing any file permissions on directories and files set up by the system itself.

  • There's no way to undo it and get back all the old permissions.

    That is, if you had files and folders with various different permissions before, there's no way to go back to those specific permissions - only to change them all to the same thing, which may lose any specific permission settings that were needed on specific files.

  • There is always a more appropriate way of achieving what it is you want to achieve.

In this case, it looks like you only want your web server to be able to read a directory. Giving world-writable permission is way more than you need to do.

Track down why the web server can't read that directory (hint: it's probably because your home directory isn't world-readable. World-readable is a lot safer than world-writable - set your home directory to something like 755, or move that www dir outside your home into somewhere like /var/www or /srv).

| improve this answer | |
cd /var/www
find -type d ! -perm 777 -exec chmod 777 {} \;

for the ftp creating all files with different permissions, you might want to look for the umask of ftpd, how that daemon is started

Take a look to this site https://linuxaria.com/article/linux-shell-understanding-umask-with-examples

| improve this answer | |
  • 1
    +1 for this is the only answer that updates ONLY folders and not files – Jan Myszkier Dec 6 '18 at 9:03

If you would like to copy permissions and or ownership from another file that you're satisfied with, you can do so using sudo chmod --reference=path/to/file/to/reference path/to/file/you/want/to/change/permission/to

And you can do the same thing for file ownership as well.

| improve this answer | |

This does not work to me.

sudo chmod -f 777 /path/to/your/file/or/directory

I have to use -f also.

sudo chmod -R -f 777 /path/to/your/file/or/directory
| improve this answer | |

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