I often find myself chown-ing files as I created them through SSH and they are owned by jm:jm for example.

Then to allow Apache to access it, I need to for example do chown jm:www-data.

I am sure I can specify that when I create files, instead of using the group under my name, specify another?

Or is there a better way of managing users/groups?


If you want all new files in a particular directory to be owned by a particular group, just apply the setgid bit on it:

chgrp www-data /some/dir
chmod g+s /some/dir

If you have an existing tree of directories that you want to apply this behaviour to, you can do so with find:

find /some/dir -type d -exec chgrp www-data {} +
find /some/dir -type d -exec chmod g+s {} +

(if the directories are already owned by the relevant group, then you can omit the first command in both of these examples).

  • 1
    Nice answer! Could you, please clarify what do the symbols ... {} + mean?
    – Alex Lomia
    Jul 21 '16 at 21:16
  • 3
    They are arguments to the find command, covered in the man page. For the first invocation, it will run the command chgrp www-data, appending a number of results as arguments (rather than invoking the command once for each result as you'd get with {} \;). Jul 23 '16 at 3:01
  • 9
    Why not use chmod g+s /some/dir -R? Just tested and it seems to work.
    – CragMonkey
    Sep 22 '17 at 2:01
  • 20
    @Cragmonkey: that will also set the setgid bit on regular files. If any of them happen to be executable, this would allow another user on the system to gain the privileges of the group that owns the file. Sep 26 '17 at 14:20

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.