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

4 Answers 4


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

install -d -o <user> -g <group> -m <mode> <path>
  • % sudo install -d -o <user> -g <group> -m <mode> <path> for creating the directory in a <path>/../ owned by root, for instance
    – Brandt
    Commented Sep 11, 2020 at 18:17
  • 1
    One potential issue with this is it won't set the specified ownership on parent dirs it creates, only the last path segment. That's the behavior I'm seeing in version 8.32 on Rocky 9.
    – Sherwin F
    Commented May 8, 2023 at 16:48

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. Commented Apr 30, 2013 at 0:49
  • 1
    Also that user might not have permission.
    – mahemoff
    Commented Apr 23, 2018 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
    Commented Apr 28, 2013 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
    Commented Apr 28, 2013 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
    Commented Apr 28, 2013 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
    Commented Apr 29, 2013 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
    Commented Apr 29, 2013 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

The solution is to split it in two commands:

sudo mkdir -p /srv/postgres/data/mydir
sudo chown postgres:postgres /srv/postgres -R
  • 2
    sudo install -d -o <user> -g <group> -m <mode> <path> sets the owner/group/mode of the last component as <user>/<group>/<mode>.
    – montiainen
    Commented Feb 13, 2020 at 14:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .