How do I set up a folder so that anything created in it inherits permissions?

I have a /data folder (actually a partition) for all data that should be accessible by all users (in this case family members). We all have individual user accounts and are often all logged in at any time on this one PC.

How can I set up permissions so that we all retain access to files there no matter who creates them, including new folders? If I create a folder it gets my user and group, so nobody else can write to it.


Solution 1:

Another approach is to use Access Control Lists, a superset of file permissions.

First of all, we have to install the acl Install acl package:

sudo apt-get install acl

Before Ubuntu 14.04, the partition has to be mounted with the option acl for the following to work. It could be added in /etc/fstab, as in

UUID=<XXXX>  /media/shared  ext4  noatime,acl  0  2

or for an already mounted filesystem

sudo mount -o remount,acl /media/shared

Next, you should create a new group, to which all users allowed to access the share in read/write mode will be added. I call it usershare. An already existing group could be used.

sudo addgroup usershare

Now we add the users enzotib and steevc to that group:

sudo gpasswd -a steevc  usershare
sudo gpasswd -a enzotib usershare

(effective at the next login).

Then we add an ACL with rwx permissions for the group usershare to all files already in /media/shared

sudo setfacl -Rm g:usershare:rwX /media/shared

Finally we add a default ACL with rwx permissions for the group usershare for all files created from now on inside /media/shared

sudo setfacl -d -Rm g:usershare:rwX /media/shared

Now all users of the usershare group have full permissions on all files under /media/shared. Permissions of each user on his and other's home directories are not affected.

I tested this solution and seems to work, but suggestions and corrections are welcome.

Remark: new files and directories created in the considered directory will have write permission for the usershare group, but files copied or moved in the folder will retain their original permissions. If the user, as I understand, only require write access to newly created directories, this is not a problem. Otherwise it should modify permissions by hand. See this answer on how to overcome this by defining the umask of users to 002.

Solution 2:

  1. Make folder.

    For example:

    mkdir /mnt/family
    

    If you need mount partition to them... if ext4 all you need in /etc/fstab is

    UUID=xxx    /mnt/family ext4    **rw,exec,defaults,auto,async   0   2**
    
  2. Create group myfam.

    addgroup myfam
    
  3. Add some users to that group

    adduser papa myfam  
    adduser mom myfam
    
  4. Now take and give permission.

    I think you should start, from changing umask.

    Default filesystem permissions and access rights in 12.04

    chown -R you.myfam /mnt/family  
    chmod -R g+rwx /mnt/family  
    

    Now most important. That line create setgid anything you create under that folder have your users as owner and group myfam. That let system override user primary group.

    chmod -R g+s /mnt/family   
    

Solution 3:

The simplest thing that come to mind is to add each user to the group of all other users.

Then change umask of each user from 022 to 002, this could be done in /etc/profile.

Edit

The first step could be replaced with the following: make all users belong to users group as the primary group.

Edi2

As @James_Henstridge suggested, it can be convenient to set the setgid bit on the main directory, so that new files and directories created will have its same group, indipendently of the user. In this way you can avoid the set users as the primary group for users.