having a bit of a difficult time trying to create a folder under another user's /home/devuser1/pubic_html folder. I'm trying to avoid using sudo and looking for an alternative. The permissions on the said folder reads as:

drwxr-s--- 2 devuser1  www-data 4096 Apr 28 19:40 public_html

Alternatively, assuming I use the sudo prefix, what would be the implications be? I've read that it's bad practice to use sudo to make a folder. After the new folder is created, I'm still changing the ownership of it to the user in question. Example:

chown -vR devuser1:www-data /home/devuser1/public_html/$vhost

With install, you can create a directory with a specific owner, group, and mode:

install -d -o <user> -g <group> -m <mode> <path>

sudo -u [username] mkdir /home/[username]/public_html/[folder_name] works fine.

From what I can see the permissions and ownership is the same if I were to log in as the same user and create the folder under public_html.

You can also call su -c "mkdir /home/[username]/public_html/[folder_name]" [username]

  • 2
    When you run sudo -u USERNAME mkdir DIRNAME, you are executing the mkdir command as the user USERNAME. This isn't exactly equivalent to logging in as USERNAME, because logging in also implies setting environment variables and so on, but it's the part that matters, namely executing a process as a particular user. – Gilles Apr 30 '13 at 0:49
  • 1
    Also that user might not have permission. – mahemoff Apr 23 '18 at 10:35

Given those permissions, only the owner of the directory or the super user can create subdirectories.

The only way that you could avoid use extra privileges to create the folder is change the ownership to yourself (with sudo), create the subdirectory and finally return the ownership to the owner, but doesn't look like a good solution to me.

  • yea, tough one this. This is a silly question, but what are my options? – maGz Apr 28 '13 at 20:51
  • what about (as root) running the command as that user?: sudo -u devuser1 mkdir /home/devuser1/public_html/test. That's not the same as creating the folder with sudo is it? ls -al reveals: drwxr-sr-x 2 devuser1 www-data 4096 Apr 28 23:49 test – maGz Apr 28 '13 at 22:02
  • Maybe I'm being naïve, but I don't really see any evil in create a folder with sudo as long as you change the ownership after that. – RSFalcon7 Apr 28 '13 at 22:14
  • From what ls reveals on the contents of the user's public_html, it appears as though changing ownership is not required, maybe permissions though. – maGz Apr 29 '13 at 6:31
  • I think I'm also being naïve in thinking this: I'm not creating the folder as su, instead I'm using it to authenticate myself as the user who is going to create the folder...does that make sense? – maGz Apr 29 '13 at 6:34

I tried to do a mkdir -p /srv/postgres/data/mydir but because

drwxr-xr-x 3 root root 4.0K Apr 7 08:26 srv has owner root

neither install -d -o <user> -g <group> -m <mode> <path> @user2313838

nor sudo -u postgres mkdir -p /srv/postgres/data/mydir works @maGz

So I've to split it in two commands:

sudo mkdir -p /srv/postgres/data/mydir
sudo chown postgres:postgres /srv/postgres -R

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.