What does SGID do on a directory, and when is it useful?
On a directory, SGID makes new files and sub-folders inherit the directory's group instead of the creator's primary group. It is used for shared team folders so everyone's files stay accessible to the same group. It shows as s in the group execute slot.
Explain it simply
Imagine a project room for the final-year team. Every document placed in that room should carry the team's stamp, no matter who brought it. SGID on a directory does that stamping automatically.
Without SGID, a file you create belongs to your primary group (often a group with your own name). Teammates in the devteam group then cannot edit it. With SGID on the folder, new files get the folder's group, devteam, and new sub-folders inherit the SGID bit too.
Set it with chmod g+s dir or chmod 2775 dir. In ls -l the group execute slot shows s (drwxrwsr-x).
On an executable file, SGID works like SUID but with the file's group: the program runs with that group's privileges.
Example
sudo mkdir /srv/team
sudo chgrp devteam /srv/team
sudo chmod 2775 /srv/team
touch /srv/team/notes.txt
ls -l /srv/team/notes.txt
# -rw-r--r-- 1 amit devteam 0 ... notes.txtThe file's group is devteam, not amit's own group. Add umask 002 so the group can also write.
What to say in the interview
"SGID on a directory makes new items inherit the directory's group, which keeps shared folders working for a whole team. I combine chmod 2775, the right group ownership and a umask of 002. On executables it runs the program with the file's group."
Common mistakes
- Setting SGID but leaving the umask at 022, so group members still cannot write.