Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I ssh as root user to our unix server from my local ... and create file in say a virtual server web root directory, how can I make sure that files and folder created inside that folder always have some different user/group as owner instead of root ?

Or is there any way I can run a command after uploading the files via root login ... so that all files in that folder will have same user/group as the parent directory ?

share|improve this question
Allowing root logins via ssh is dangerous – symcbean Oct 20 '10 at 14:08

You can't force the ownership, but you can force the group. To do that set the group of the directory to your chosen group and then set the group sticky bit. For example, if the group is web and the directory is /www/primary then you'd do:

chgrp web /www/primary
chmod g+s /www/primary

People's umask setting will still control the permissions set.

share|improve this answer

A newly created file will always be owned by the user that created it, and (by default) the group it's a member of.

If, however, you set the setgid access right flag on the parent directory, new files and subdirectories created under it will inherit the parent directory's groupID instead.

For example:

~$ sudo addgroup testing
Adding group `testing' (GID 1001) ...
~$ mkdir dir
~$ chown :testing dir/ # Change owner to group "testing"
~$ chmod g+s dir/      # Set the setgid flag
~$ ls -ld dir/
drwxr-sr-x 2 root testing 4096 2010-10-19 04:20 dir/

If you now create (as root) a new file under the dir/ directory, it will be owned by user root and group testing instead of user root and group root:

~$ touch dir/file
~$ ls -l dir/file
-rw-r--r-- 1 root testing 0 2010-10-19 04:46 dir/file

You will probably want to look at the umask command as well.

share|improve this answer

Your Answer


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.