How to Set-up shared folder/repository between two or more users on Linux

14 / Jun / 2010 by Himanshu Seth 6 comments

We had a case where we wanted two different applications (run by different users) to be able to read and write from the same file system.
This is how we solved this problem:

  1. Create a group which these users will belong to :
    groupadd GROUP_NAME
  2. Edit user1 and user2 to be a member of this group:
    usermod -a -G GROUP_NAME user1 ;
    usermod -a -G GROUP_NAME user2;
    
  3. Create a shared directory. In our case, it had to be the document root for an Apache site. Thus we chose the location SHARED_FOLDER
  4. Now we need to change the group of this folder :
    chgrp -R GROUP_NAME SHARED_FOLDER 
  5. We’ll also need to grant the group write access on this folder :
    chmod g+w SHARED_FOLDER 
  6. Now we’ll need to set the GroupID flag on this folder. For a directory, the set-groupID flag means that all files created inside that directory will inherit the group of the directory. Without this flag, a file takes on the primary group of the user creating the file. This property is important to people trying to maintain a directory as group accessible. The subdirectories also inherit the set-groupID property. (http://www.dartmouth.edu/~rc/help/faq/permissions.html).
    chmod +s SHARED_FOLDER
  7. Now in your .bashrc / .bash_profile, set the umask as 002. Setting this umask ensures that all the newly created files by this user will have the permission “rw-rw-r”. Thus giving the group write permission.: 
     umask 002 

Now when either of the users create any file in the SHARED_FOLDER, all the users of this group will have the read/write permissions on that file. Not only this, these permissions will be on the subfolders and the files with-in that folder as well.

But, if any of these users create a file outside the SHARED_FOLDER, the primary group of that file/folder will be the same as the primary group of that user. Thus files/folder only in the SHARED_FOLDER are shared between these users.

This is just one of the many great abilities that Linux provides.

Hope this saves you some time.

Your feedback and suggestions are welcome.

Regards
~~Himanshu Seth~~

http://www.tothenew.com

FOUND THIS USEFUL? SHARE IT

comments (6)

  1. Oliver Doepner

    I found that the suggested solution doesn’t work if you use “mv” or similar file operations. It has frustrated me for a long time that a simple use case like this is apparently not supported by Unix/Linux file systems.

    Reply
  2. Dennis

    Thank You for the info. I’m hung up on the .bashrc file. I searched for umask and don’t see it in the users directory /home/USERNAME/.bashrc Should I just add umask 002

    I have been trying to mount my shared drive to each users drive but unsuccessfully.
    SHARED DIRECTORY: /home/shared
    USERS DIRECTORY: /home/$user/shared

    sudo mount -w –bind /home/shared /home/$user/shared

    This works when server is up but does not work in /etc/rc.local and I’m guessing its because the variable $user has not been defined yet.

    Any suggestions?

    Reply
  3. Bhagwat Kumar

    Thanks for the Nice Blog.

    While going through the steps you mentioned in the blog, I found you missed command for “set-group-ID”.

    I used this “chmod g+s sharedFolder”.

    To verify if this worked, check if the execute permission on group for “sharedFolder” is shown as “s” instead of “x”.

    Reply

Leave a comment -