Users typically don’t have access to each other’s directories, so sharing files between users who, for example, might be working on the same project ordinarily can’t be achieved. Here, I’ll show you how to set up a shared directory between two users.
For this, I’m going to create a directory that I’ll use for sharing called “/home/public”. So I’ll create this directory first as root.
Next, I’ll add a user group that all users who want to access the “/home/public” directory have to belong to called “share”.
Next, I’ll change the user owner to root, but set the group owner of the directory to the above group “share”.
chown -R root.share /home/public
Next, I’ll add the first user I want to the “share” group (you can do this as many times as you like for different users).
/usr/bin/gpasswd -a user1 share
I’ll then switch the “/home/public” to full permissions for the group and owner with: -
chmod ug+rwx -R /home/public
Lastly, I’ll set the SGID bit on the shared directory. Normally whenever you create files in a directory, by default it belongs to the default group or user. When a file is created in a directory with the SGID bit set it belongs to the same group as the directory. The result being that all users of the “share” group can create/alter files in “/home/public” directory.
chmod g+s /home/public
If you now do a detailed list command on the “/home” directory using:-
…you’ll get output similar to:-
drwxrwsr-x 2 user1 shared 4096 Jul 11 12:19 public
That ‘s’ listed under the file permissions means the SGID bit is set successfully.
You can verify that the users you want to be able to write to the public directory are added by listing the “/etc/group” file with: -
…which will show the users assigned to the group. For example: -
That’s it :-)