2

Recently I noticed that new directories I create are always owned by root. Is this how supposed to be? I created this directory to put html files for a web server /var/www/site1/html/index.html and it is owned by root. I did sudo chown -R a:a /path-to-directory to change the owner to me; and I did sudo chmod 777 -R /path-to-directory to change permissions for all. Now I have:

-rwxrwxrwx  1 a a 2 Dec 17 19:18 index.html

How can I create directories so that I own the directories I created with read and write permissions? And is it good to change a file read/write/execute for all?

  • If you are setting up a public webserver, you should probably make a user account dedicated to that purpose and have the files for it owned by that user. Typically, anyway - for home server use, probably not an issue. – nerdwaller Dec 18 '13 at 3:45
  • 3
    chmod 777 is the second worst pitfall after the abuse of using a root shell, please don't use it unless you are very sure why do you want that – AnonymousLurker Dec 18 '13 at 5:19
4

Ownership should be dictated by who is creating the directory. If your user does the mkdir command, it should have ownership of the directory.

However, if you use sudo mkdir then the root is the user actually creating the directory, not your user. If your user does not have permissions in the parent directory to create the subdirectory, which is the only reason to use sudo mkdir then the best way to resolve this is to change the directory permissions for the parent directory.

  • Ok, thanks. In /etc/nginx/site1.conf I assume that it is not good to change the ownership of /etc/ (because it is system file?) but I went ahead and changed /nginx/ and sub directories to user and now I can edit it in emacs. What is the best practice to changing/not changing permissions of directories? – Zeynel Dec 18 '13 at 1:14
  • Depends on what you are worried about, exactly. If you're worried about rogue users being able to see the directory using some kind of directory mapping through html you could always change the permissions using sudo on the parent directory, then change them back to the safe octal after you're done making your adjustments. Once you've created the directory if you don't -R the chmod it won't affect them, just the parent. Real discussion of permissions best practices is probably best for a separate question though, after some google fu. – George Spiceland Dec 18 '13 at 1:38
  • @Zeynel You probably shouldn't make the permissions on anything in /etc more permissive. They're protected for a reason, and generally to edit them you should be using sudo or similar. – ernie Dec 18 '13 at 1:53
  • Using sudo creates exactly the issue he's referring to. – George Spiceland Dec 18 '13 at 1:56

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.