2

I currently have a small Ubuntu Server 12.04 machine (test environment) with about 3 non-root users created. Each user has their own public_html directory under their home...thereby allowing them to deploy multiple apps as named virtual hosts. Each user belongs to the Apache www-data group, set up as follows:

sudo usermod -a -G www-data [username]
sudo chown -R [username]:www-data /home/[username]/public_html
sudo chmod 2750 /home/[username]/public_html

Now as the root user, I am in the process of creating a bash script that will automate the creation of the folders for the VirtualHost under a prompted user's public_html as well as creating an associated entry in /etc/apache2/sites-available/. The script (run with sudo) will prompt for the user ($uzer) and the desired virtual host name ($vhost). So far after running a few checks I eventually get to the following...

mkdir -vp /home/$uzer/public_html/$vhost
mkdir -vp /home/$uzer/public_html/$vhost/www
mkdir -vp /home/$uzer/public_html/$vhost/logs
mkdir -vp /home/$uzer/public_html/$vhost/backups

I need to change the ownership of these newly created folders, so I'm unsure whether I should be doing the following:

chown -vR $uzer:www-data /home/$uzer/public_html/$vhost
chmod 2750 /home/$uzer/public_html/$vhost

My questions:

  • Is my folder structure correct/ideal?
  • I know I've used recursive (-R) option, but should I be repeating the same for $vhost/www, $vhost/logs and $vhost/backups?
  • Am I correct in thinking that the chmod above is probably redundant?
  • Is there a way I can run the mkdir commands as the user $uzer?

2 Answers 2

3

Q: Is my folder structure correct/ideal?

A: Folder structure seems fine.

Q: I know I've used recursive (-R) option, but should I be repeating the same for $vhost/www, $vhost/logs and $vhost/backups?

A: It would be redundant to run it on those directories

Q: Am I correct in thinking that the chmod above is probably redundant?

Yes technically it's redundant because your initial sudo that cretes the directories is setting the 'set group id bit', but setting that bit, (the 2 in 2750), is not a guarantee. I've seen directories with this on where users have either moved files into the directory or accidentally changed the group on files, so I'd leave it.

Is there a way I can run the mkdir commands as the user $uzer?

root$ su -u $user -c "mkdir ..."

Also you could save a step on the chmod of the /www, /log, & /backups by using the mkdir --mode=... switch.

For example

mkdir -vp --mode=2750 /home/$uzer/public_html/$vhost
mkdir -vp --mode=2750 /home/$uzer/public_html/$vhost/www
mkdir -vp --mode=2750 /home/$uzer/public_html/$vhost/logs
mkdir -vp --mode=2750 /home/$uzer/public_html/$vhost/backups
3
  • slm, thank you for your response. Please could you elaborate on your last statement by saving a step on chmod. how would I use the --mode switch?
    – maGz
    Commented Apr 26, 2013 at 13:39
  • slm, thank you! You've given me solid feedback :)
    – maGz
    Commented Apr 27, 2013 at 16:35
  • No problem, glad I was able to help, good luck with your project!
    – slm
    Commented Apr 27, 2013 at 16:39
0
  1. Actually if doesn't matter what folder structure you are using.
  2. If you are using -R option in chown, it will only effect to the files and directories which are present at the time you are running the command.
  3. No, because always you have to use chmod command to change the permission if you are making any changes manually.
  4. You can ask /etc/sudoers file to allows $user to create any directory with mkdir command.
4
  • Thanks Nitesh! Let me try this out and I'll post back here
    – maGz
    Commented Apr 26, 2013 at 11:20
  • Regarding (3), My question of whether the chmod was redundant or not, is due to the fact that when the user is initially created the sudo chmod 2750 /home/[username]/public_html command should set the permissions for any new files/folders created under public_html to 750...that's what the extra bit 2 is for I think
    – maGz
    Commented Apr 26, 2013 at 11:24
  • #4 is an extremely large security risk, allowing arbitrary code execution by that user.
    – Chris Down
    Commented Apr 26, 2013 at 12:43
  • @Chris: yup, you are right, its a large security risk, but google will help you for sure.
    – Nitesh B.
    Commented Apr 27, 2013 at 8:02

You must log in to answer this question.

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