How to Make Directory Sharable in Linux

When you create a new file, Linux assigns the file permissions of the newly created file using your default UID and GID. To allow others access to the file, you need to either change the security permissions for the everyone security group or assign the file a different default group that contains other users.

Sharing-Folder-Linux

ON THIS PAGE

  1. What’s file sharing
  2. How Linux stores information about a file
  3. Easy ways to share a file or a directory
  4. Steps to make a file or a directory sharable

What’s file sharing

It means giving access to others so that they have permission on it.

How Linux stores information about a file

Linux stores three additional bits of information for each file and directory:

  • Set user ID (SUID): When a file is executed by a user, the program runs under the permissions file owner’s permissions.
  •  Set group ID (SGID): For a file, the program runs under the permissions of the file’s group. For a directory, new files created in the directory use the directory group as the default group.
  •  The sticky bit: When applied to a directory, only file owners can delete or rename the files in the directory.

Easy ways to share a file or a directory

The SGID bit is important for sharing files. By enabling the SGID bit, you can force all new files created in a shared directory to be owned by the directory’s group and now the individual user’s group. Here’s a helpful book on Linux

The SGID is set using the chmod command. It’s added to the beginning of the standard three-digit octal value (making a four-digit octal value), or you can use the symbol s in symbolic mode.

BinaryOctalDescription
0000All bits are cleared.
0011The sticky bit is set.
0102The SGID bit is set.
0113The SGID and sticky bits are set.
1004The SUID bit is set.
1015The SUID and sticky bits are set.
1106The SUID and SGID bits are set.
1117All bits are set.
Linux OCTAL values

Steps to make a file or a directory sharable

The first step is to create a directory that you want to share using the mkdir command. Next, use the chgrp command to change the default group for the directory to a group that contains the members who need to share files. Finally, the SGID bit is set for the directory to ensure that any files created in the directory use the shared group name as the default group.

To work properly, all the group members need to have the umask values set, so that group can have writable access. Below, you’ll find the umask is changed to 002 so that the files are writable by the group.

After all, any member of the group can go to the shared directory and create a new file. As expected, the new file uses the default group of the directory, not the user account’s default group. Now any user in the shared group can access this file.

 $ mkdir testdir
 $ ls -l
 drwxrwxr-x    2 rich     rich         4096 Sep 20 23:12 testdir/
 $ chgrp shared testdir
 $ chmod g+s testdir
 $ ls -l
 drwxrwsr-x    2 rich     shared       4096 Sep 20 23:12 testdir/
 $ umask 002
 $ cd testdir
 $ touch testfile
 $ ls -l
 total 0
 -rw-rw-r--    1 rich     shared          0 Sep 20 23:13 testfile
 $

Related

References

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.