Linux setup shared directory

last updated in Categories

Sharing a directory among users in same group is one of the essential tasks. You need to use chmod command and add user to appropriate group. To make idea clear here is an scenario:

  • /home/myproj : is shared directory
  • usr1, usr2, … usrN : would like to work and share files in /home/myproj directory
  • padmin : Main project administrator user

Step # 1: Create a shared directory /home/myproj
If this directory does not exist then create it:

# mkdir /home/myproj

Step # 2: Create the group shared group
You need to create a new group. Let us assume group name is myproj

# groupadd myproj

Step # 3: Add user project administrator (padmin) and setup password:

# useradd -d /home/myproj/ -g myproj -m padmin
# passwd padmin

Step #4: Add rest of users to group myproj

# useradd -d /home/myproj/ -g myproj usr1
# passwd usr1

Add second user:

# useradd -d /home/myproj/ -g myproj usr2
# passwd usr2

… and so on…

Step #5: Setup permission on /home/myproj directory as follows:
(a) Setup group ownership to myproj group:

# chown -R padmin.myproj /home/myproj/

(b) Setup full permission for group and owner on a directory:

# chmod -R 775 /home/myproj/

(c) Setup sgid bit. So what is sgid bit? Normally whenever you creates file in a directory it belong to default group of user. When a file is created in a directory with the sgid bit set it belogns to the same group as the directory. The result is all users of myproj group can create/alter files in /home/myproj directory:

# chmod -R 2775 /home/myproj/


# chmod -R g+s /home/myproj/

See also:


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

9 comment

  1. My problem is that when usr1 creates a file foo in /home/myproj he then becomes the owner of foo and no one else can edit it. How can I cause files and dirs under /home/myproj to be accessed (read write del) by all the users in the group?

    ls -l /home/myproj
    -rw-r–r– 1 usr1 users 5 May 31 03:37 foo

  2. I made a web group, added the users.

    Made a directory called test
    chown root.web test -R
    chmod 2775 test -R

    I can see the guid bit set, but when I make a file in the test directory, it does not have group write permissions? the file is root.web but the permissions are 644.

    I thought since the directory was 2775, the files that are created inside are 664… Did I do something wrong?

    Group members need to add files and then other group members need to write to them.

  3. The reason this doesn’t work as Jon and Dunlap want:

    Setgid when set on a directory (e.g., the 2 in 2775) basically only specifies the default group for new files not their permissions. The permissions will still be dictated by umask; so you should set umask 002 (so new files are created with read/write permission for everyone in the group by default — the normal umask is 022).

    This is normally done in /etc/profile or in each users ~/.profile or ~/.bashrc

    1. Just wondering: is there any security implications by setting umask to 002?

      Can I set it just for a certain directory?

      1. But this don’t prevent the users from using a more restrictive umask, or from using faulty graphical clients that use another permissions.

        The only solution I found is to check the permissions everytime someone changes the permissions of a file/dir, creates a file/dir, or modifies a file/dir with inotify and then set the permissions accordingly. Unfortunately, this task is very difficult, because some clients change the permissions *after* writing all the file, some when creating it… and you can run into some infinite loops, too. So some in-field testing will be required.

  4. I actually found that my issue had to do with php ssh2. I was using WordPress auto upgrade feature and the files would get created but without the correct file permission. I was able to use filezilla sftp for the developers to upload and create files which worked.

    I made a change to the mask in the wordpress config that created group writable files.

  5. i have a drive in windows server 2003 and I want share this drive for one user
    this user use a linux Red Hat OS and i want just this user modify this drive and other network user can’t any modify this folder

    How can i ddo this
    by Active directory User How?

    Still, have a question? Get help on our forum!