How to Set-up shared folder/repository between two or more users on Linux
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:
- Create a group which these users will belong to :[shell]groupadd GROUP_NAME[/shell]
- Edit user1 and user2 to be a member of this group:[shell]
usermod -a -G GROUP_NAME user1 ;
usermod -a -G GROUP_NAME user2;
- 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
- Now we need to change the group of this folder :[bash]chgrp -R GROUP_NAME SHARED_FOLDER [/bash]
- We’ll also need to grant the group write access on this folder :[bash]chmod g+w SHARED_FOLDER [/bash]
- 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).[bash]chmod +s SHARED_FOLDER[/bash]
- 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.: [bash] umask 002 [/bash]
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.
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.
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.
Thanks for pointing that out Bhagwat. Have fixed it now
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”.
Step 4, you need a group name as well:
Now we need to change the group of this folder :
Thnx for pointing that out. Have updated the blog