Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Moved here from AskUbuntu

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?

I am a bit of a Linux server noob, but have learned a lot in the last few months - this is probably the first shell script I've attempted to write and I still haven't fully mastered Linux permissions.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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
share|improve this answer
    
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 Apr 26 '13 at 13:39
    
slm, thank you! You've given me solid feedback :) –  maGz Apr 27 '13 at 16:35
    
No problem, glad I was able to help, good luck with your project! –  slm Apr 27 '13 at 16:39
add comment
  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.
share|improve this answer
    
Thanks Nitesh! Let me try this out and I'll post back here –  maGz Apr 26 '13 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 Apr 26 '13 at 11:24
    
#4 is an extremely large security risk, allowing arbitrary code execution by that user. –  Chris Down Apr 26 '13 at 12:43
    
@Chris: yup, you are right, its a large security risk, but google will help you for sure. –  Nitesh B. Apr 27 '13 at 8:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.