Sweshi's Tutorials

Linux Permissions

Linux Special Permissions: setgid

NB: This tutorial has worked on Centos 6,7,8 and 9

SetGID allows a program to inherit the group ownership of the directory or file, rather than the group ownership of the user who runs the program.

  • SetGID on Directories:When SetGID is set on a directory, any new files or directories created within that directory will inherit the group ownership of the parent directory rather than the primary group of the user who created the file or directory. So if you are logged on to an account named "sweshi" who belongs to a group named "sweshi" but are working in a folder/directory that belong to a group called "lecturer" and this bit is on, when a "sweshi" creates a file in this folder, the account owner will be sweshi but the group owner will be "lecturer" instead of group "sweshi".
  • SetGID on Executables:When SetGID is set on an executable file, the program runs with the privileges of the group that owns the file, rather than the group of the user who executed the program.
Special Permission Letter Representation Numerical (Octal) Representation Types of files it works on
setgid s 2 executable files and folders
Examples of setgid using letter representation
how to add the setgid Linux Special Permission using chmod with letters

We can enable the setgid as shown in the screenshot. The command “chmod g+s example.txt” enables the setgid on the example.txt file. When we do a long listing, we see that the group section of the file permissions have a letter “S” where the execute permission is normally placed. This means that the setgid is enabled.

how to remove the setgid Linux Special Permission using chmod with letters

We can then remove the setgid by using “g-s” on the file or folder. In this case I ran “chmod g-s example.txt” to remove it from the “example.txt file. When we do the listing using “ls -l”, we see that the letter “s” is removed meaning that the setgid is disabled on the file.

Examples of setgid using numbers (octal) representation
how to add the setgid Linux Special Permission using chmod with numbers (octal)

We can also enable the setgid using octal(numbers). We use number “2” in front of the standard permissions as seen in the figure. Here I ran “chmod 2644 example.txt” to enable the setgid on the example.txt file. When we do a long listing with “ls –l”, we see that the letter “S” is on the group section of the permissions which shows that the setgid is enabled on the file or directory.

How to remove the setgid Linux Special Permission using chmod with numbers (octal)

We can then remove the setgid by using zero “0” in front of the standard permisions. In this case I ran “chmod 0644 example.txt” to remove the setgid from the “example.txt file. When we do the listing using “ls -l”, we see that the letter “s” is removed, meaning that the setgid is disabled on the file.


Video