39

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

43

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

install -d -o <user> -g <group> -m <mode> <path>
2
  • % sudo install -d -o <user> -g <group> -m <mode> <path> for creating the directory in a <path>/../ owned by root, for instance
    – Brandt
    Sep 11, 2020 at 18:17
  • 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
    May 8 at 16:48
40

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

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.

6
  • yea, tough one this. This is a silly question, but what are my options?
    – maGz
    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
    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
    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
    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
    Apr 29, 2013 at 6:34
0

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
1
  • 1
    sudo install -d -o <user> -g <group> -m <mode> <path> sets the owner/group/mode of the last component as <user>/<group>/<mode>.
    – montiainen
    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 .