Automatically set group and permissions on files in a folder?
Solution 1
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::...
Solution 2
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.
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on September 18, 2022Comments
-
Admin almost 2 years
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 tofoo
and my default groupbar
. I'd like new folder to belong to theapache
group instead.I'd like the same behavior for files.
Any ideas?
-
Rajan Kadeval over 9 yearsHow 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?
-
muru over 9 years@naomik per shell session (in that it's usually set by the
umask
command): serverfault.com/questions/383734/… -
Hauke Laging over 9 yearsDefault ACL values for user, group and others override the umask.
-
muru over 9 years@HaukeLaging Well, assuming you can set an ACL. On Ubuntu that's been a problem for sometime, I think: help.ubuntu.com/community/FilePermissionsACLs
-
Hauke Laging over 9 yearsIt 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.
-
muru over 9 years@HaukeLaging the bug's filed with GNU coreutils, so I assumed that the bug affected other distributions as well, and not only Ubuntu.
-
Hauke Laging over 9 yearsI 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? -
muru over 9 years@HaukeLaging Answers aren't set in stone. They are updated over time. If and when the situation changed, the answer can be updated.