4

I want all the files in /var/www directory be owned by nginx.devel. I have performed chmod once and update these files using rsync.

But if I create a new file and then rsync it, it will be owned by user.user and I need to run chmod with sudo privileges on it.

  • user is not nginx? – drAlberT Aug 13 '09 at 9:29
  • yes. It is different user. – lexsys Aug 13 '09 at 9:38
5

You can't. Well, not automatically anyway. You can, however, enforce group permissions, which with careful planning and an appropriate umask, can solve most permissions problems. If you set the setgid bit on the /var/www directory it will ensure that any new files created under it are owned by the devel group.

Note that depending on which flags you use, rsync may override this behavior and replicate the ownership of the source files.

To set the setgid bit:

$ ls -ld test
drwxr-xr-x 2 insyte insyte 4096 2009-08-13 04:39 test
$ chmod g+s test
$ ls -ld test
drwxr-sr-x 2 insyte insyte 4096 2009-08-13 04:39 test

Demo of the behavior:

$ id
uid=1000(insyte) gid=1000(insyte) groups=4(adm),20(dialout),24(cdrom)
$ ls -ld test
drwxr-xr-x 2 insyte backup 4096 2009-08-13 04:39 test
$ touch test/file1
$ ls -l test
total 0
-rw-r--r-- 1 insyte insyte 0 2009-08-13 04:43 file1
$ sudo chmod g+s test
$ ls -ld test
drwxr-sr-x 2 insyte backup 4096 2009-08-13 04:43 test
$ touch test/file2
$ ls -l test
total 0
-rw-r--r-- 1 insyte insyte 0 2009-08-13 04:43 file1
-rw-r--r-- 1 insyte backup 0 2009-08-13 04:44 file2

The effect is recursive as well; any new directories created after you set the setgid bit will also have the setgid bit set.

An alternative to this approach, of course, would be a cron job that runs every minute and fixes the permissions on the files under /var/www/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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