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.
ON THIS PAGE
- What’s file sharing
- How Linux stores information about a file
- Easy ways to share a file or a directory
- 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.
Binary | Octal | Description |
---|---|---|
000 | 0 | All bits are cleared. |
001 | 1 | The sticky bit is set. |
010 | 2 | The SGID bit is set. |
011 | 3 | The SGID and sticky bits are set. |
100 | 4 | The SUID bit is set. |
101 | 5 | The SUID and sticky bits are set. |
110 | 6 | The SUID and SGID bits are set. |
111 | 7 | All bits are set. |
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