Skip to content

Linux Foundations / Manage a Linux host ยท Step 12 of 50

Lesson: SetgidSwitch
โ† Linux Foundations

System administration

Setgid

Learn what the setgid bit does on programs and how it keeps shared directories in one group.

The setgid (set group ID) bit is the group version of setuid. It behaves differently depending on whether you put it on a program or on a directory.

Setgid on a program

A setgid binary runs with the group identity of the file's group, in place of the caller's primary group:

$ ls -l /usr/bin/chage
-rwxr-sr-x 1 root shadow 113848 Apr 19  2025 /usr/bin/chage

The s sits in the group execute position. On Debian, chage uses this to read /etc/shadow through the shadow group without becoming root. Other distributions may set up this program differently.

Setgid on a directory

This is the use you will meet most often. Files created inside a setgid directory take the directory's group, not the creator's primary group. New subdirectories also receive the setgid bit, so the rule carries on down the tree.

Here is a shared project folder:

$ sudo mkdir /srv/project
$ sudo chgrp devs /srv/project
$ sudo chmod 2775 /srv/project
$ ls -ld /srv/project
drwxrwsr-x 2 root devs 4096 Mar  7 10:00 /srv/project
$ touch /srv/project/plan.txt
$ ls -l /srv/project/plan.txt
-rw-r--r-- 1 maya devs 0 Mar  7 10:01 /srv/project/plan.txt

The user running touch must be a member of devs to create the file here. The group of plan.txt is devs even though maya's primary group is maya.

Notice that the file is not group writable. Setgid controls the group name, not the mode. For true shared editing, members also need a umask such as 002.

Files that already existed keep their old group, and so does a file moved in with mv from elsewhere on the same filesystem.

Setting the bit

Use chmod g+s or add a leading 2 to the numeric mode. A capital S in the group position means setgid is set without group execute.

To remove it, use chmod g-s. With GNU chmod, a short numeric mode such as 755 leaves a directory's setgid bit in place, so the symbolic form is the clear way.

Try it

Pick a group you belong to from id, then:

$ mkdir shared
$ chgrp GROUPNAME shared
$ chmod g+s shared
$ touch shared/one; mkdir shared/sub
$ ls -ld shared shared/one shared/sub

Replace GROUPNAME with your group. Check which entries show that group and which directory shows an s. More exercises are in the permissions in depth lab.

Check your understanding

Setgid

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.