I know sometimes we can do chmod 777 so we have a directory at our complete reach for accessing, reading and writing. But usually I prefer to do a sudo chown {myusername} {directory}.

Obviously, the chmod 777 leaves a directory completely open to anyone, but, does my practice of using sudo chown ... actually offer protection to my directories vs. the chmod 777?

  • In general chmod 777 is a bad way to give access. Better to setup a Unix group or change ownership. – slm Jul 10 '13 at 7:30

chmod 777 means “anyone can do anything”. Well, ok, almost anything — a few operations, like changing the time and permissions, are reserved to the owner. But anyone can create, remove or overwrite any file, so there's no protection.

I can pretty much guarantee that if you run chmod 777, you're doing it wrong. If you want to share files and edit each other's files, set up a revision control system and push changes to share them. If you only want to make some files available to others, create a ~/pub directory and put your own files there.

  • 1
    ACLs are also an option if your filesystem supports them. – jordanm Jul 10 '13 at 4:07

chmod 777 foo gives anyone with access to your box, access to the file foo.

chown user foo gives only that user access to the file foo.

If you combine the two approaches, then anyone with access to your box has access to that file.

Your method of taking ownership of the directory instead of changing its permissions to 777 is more secure in many contexts.

Default permissions

Many systems have default umasks of 022. When a directory is created, the umask is usually subtracted from 777 to calculate the permissions of the new directory, meaning the default directory permissions on most systems are 755. This means that by default, only a directory's owner may create and delete files within it. Other users are not prevented from writing to files within that directory though. That protection is provided by the files' permissions. You should be aware that if a user has write permissions to a directory but not to a file within that directory, they can simply delete the file and replace it.

The answer

In summary, taking ownership of the directory instead of changing its permissions to 777 will prevent other users from adding, removing, or replacing files within that directory but will not prevent users from reading files within the directory.

A note on directory permissions

Directory permissions are slightly odd and unintuitive compared to file permissions. The executable permission (1) allows you to retrieve metadata for files within a directory, including the files' inodes. This means that without execution permissions for a directory, you cannot read files within that directory knowing only the files' paths. The read permission (4) allows you to see a list of files in a directory but not their metadata. The write permission (2) allows you to create and delete files within a directory.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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