SGID Permission


The SGID, or Set Group ID, permission is another special permission type. When set on a file, it allows the file to be executed with the permissions of the file's group, similar to how SUID works for the user. When set on a directory, any new files or directories created within it will inherit the group ownership of the parent directory, rather than the primary group of the user who created them.


Example 1: Setting SGID on a file

# Create a file for the SGID example
touch sgid_file_example

# Set the SGID bit using the symbolic method
chmod g+s sgid_file_example

# Verify the permissions
ls -l sgid_file_example

Explanation: This code snippet begins by creating a new file named sgid_file_example. The command chmod g+s is then used to apply the SGID permission to the group. A subsequent ls -l will show an 's' in the group's execute permission slot, confirming that the SGID bit is active.


Example 2: Setting SGID on a directory

# Create a directory
mkdir sgid_dir_example

# Set the SGID bit on the directory
chmod g+s sgid_dir_example

# Verify the permissions on the directory
ls -ld sgid_dir_example

Explanation: Here, we create a directory named sgid_dir_example. The chmod g+s command sets the SGID bit on this directory. The ls -ld command is used to view the permissions of the directory itself, which will now have an 's' for the group execute permission.


Example 3: Inheritance of group ownership with SGID

# Create a directory and set SGID
mkdir project_files
chgrp developers project_files
chmod g+s project_files

# Create a new file within the directory
touch project_files/new_document.txt

# Check the group ownership of the new file
ls -l project_files/new_document.txt

Explanation: This example demonstrates the primary use of SGID on directories for collaborative work. After creating a directory and setting its group to 'developers' and applying SGID, any new file, such as new_document.txt, created inside project_files will automatically have its group set to 'developers', regardless of the primary group of the user who created it.


Example 4: Setting SGID using octal notation

# Create a directory for this octal example
mkdir sgid_octal_dir

# Set read, write, and execute for user and group, and read-only for others, plus SGID
chmod 2774 sgid_octal_dir

# Check the permissions
ls -ld sgid_octal_dir

Explanation: The octal notation 2774 is used here to set the permissions. The leading '2' represents the SGID bit. The remaining digits '774' define the standard read, write, and execute permissions for the user, group, and others.


Example 5: Finding all SGID files on the system

# Search the entire filesystem for files with the SGID bit set
find / -perm /2000

Explanation: The find command is used to locate all files on the system that have the SGID permission. The -perm /2000 argument specifically searches for files where the SGID bit (represented by the octal value 2000) is set.


Example 6: Removing SGID from a directory

# Assume sgid_dir_example has the SGID bit set
# Remove the SGID bit using the symbolic method
chmod g-s sgid_dir_example

# Verify the permissions have been updated
ls -ld sgid_dir_example

Explanation: To remove the SGID permission from a directory, the chmod g-s command is used. This command targets the group permissions and removes the 's' bit. The ls -ld command will confirm that the SGID permission has been successfully removed.


Example 7: Command variations in other UNIX flavors

# On RHEL/Linux
chmod g+s directory_name

# On FreeBSD, the command is the same, but verification might involve stat
# RHEL/Linux
ls -ld directory_name

# FreeBSD
stat -f "%Sp" directory_name

Explanation: While the chmod command for setting SGID is standard across most UNIX-like systems including RHEL and FreeBSD, the tools for viewing file status and permissions can have different options. In RHEL, ls -ld is common, whereas in FreeBSD, the stat command with specific format options like %Sp might be used to display the file permissions in a symbolic string format.