3

When I create new files and folders in a specific folder, I would like them to inherit the group and permissions of the parent folder. Is this possible ?

For example

mkdir -p example.com/public
cd example.com
sudo chgrp apache public
chmod 775 public

Now when I create a new folder

cd public
mkdir test
ls -al
drwxrwxr-x 3 foo bar 4096 Nov 3 16:19 public

The test directory belongs to foo and my default group bar. I'd like new folder to belong to the apache group instead.

I'd like the same behavior for files.

Any ideas?

0

2 Answers 2

7

The group can be inherited by setting the SGID bit:

chmod g+s /path/to/directory

For inheriting permissions you need ACL: man 5 acl

You have to set a default ACL for the directory: setfacl -d -m g::...

1

Inheriting the group ownership is easy. Simply set the SETGID bit:

chmod g+s example.com/public

Anything created in the directory should now have apache as the group owner, and directories will inherit the SETGID bit.

Inheriting permissions with ACLs is not possible on all systems, and may have performance impacts in some cases. Instead of using ACLs you can set the umask for your web server to set the permissions of new files/directories.

8
  • How does umask get saved? is it per directory? per shell session? Once umask is set on a directory, will it be used for anything that interacts with that directory?
    – user16614
    Nov 3, 2014 at 21:25
  • @naomik per shell session (in that it's usually set by the umask command): serverfault.com/questions/383734/…
    – muru
    Nov 3, 2014 at 21:26
  • 2
    Default ACL values for user, group and others override the umask. Nov 3, 2014 at 21:35
  • 1
    It would be OK to mention the Ubuntu problem. The OP doesn't mention Ubuntu, though. Ignoring the obvious solution because of a problem with a single distro seems strange to me, considering that ACL have been a default mount option for years now. Nov 3, 2014 at 21:45
  • 1
    I may have misunderstood the hint on the page you liked. I thought they meant that to be Ubuntu-specific. But I have openSUSE with coreutils 8.21-7 and I don't have this problem. But even if that affected all distros then you still shouldn't pretend that umask is the only way but say that ACL is the tool to use but there may be problems currently. For how long is a bug relevant, and for how long are the answers here read? Nov 3, 2014 at 22:16