Automatically set group and permissions on files in a folder?

12,011

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.

Share:
12,011

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    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 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?

  • Rajan Kadeval
    Rajan Kadeval over 9 years
    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?
  • muru
    muru over 9 years
    @naomik per shell session (in that it's usually set by the umask command): serverfault.com/questions/383734/…
  • Hauke Laging
    Hauke Laging over 9 years
    Default ACL values for user, group and others override the umask.
  • muru
    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
    Hauke Laging over 9 years
    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.
  • muru
    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
    Hauke Laging over 9 years
    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?
  • muru
    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.