Create shared directory in Linux

Sometime we work in a group and we need to access a specific directory or file by multiple user. And in linux we can get this functionality very easily.

Create a common group and directory

We use groupadd command in linux for creating a group. Let’s execute below command and create a group name shared and a directory.

$ mkdir /home/Desktop/shared_directory

$ sudo groupadd shared

Add existing user to the group

We use usermod command to modify an user account in linux. Let’s execute below command to add our existing user aniruddha to the group shared.

$ sudo usermod -a -G shared aniruddha 

Set appropriate permission on the directory

By default linux assign the group of a newly created directory or file to it’s current user. But we have to set the group of the directory to shared so that all user in the shared group can access the directory. And we also have to set the setGID so that newly created files or directory inside the shared_directory have the same group as the parent directory.

$ sudo chgrp -R shared /home/Desktop/shared_directory

$ sudo chmod -R 2775 /home/Desktop/shared_directory

Here chgrp is used to change the group of any directory or file and chmod is used to change the permission.
Here -R is recursive operation.
2 is for the setGID.
7 is the rwx permission.
5 is the rx permission.

Create more system users

Now it’s time to create more system user and assign them to the group shared.

$ sudo useradd -m -c "John Doo" -s/bin/bash -G shared john

Now if log in as another user and go to shared_directory you can access it.

Advertisement