linux setgid examples

Posted on September 15, 2012

setgid(short for “set group ID upon execution”) is an unix access rights flags that allow users to run an executable with the permissions of the executable’s group.

The setgid attribute will allow for changing the group-based privileges within a process, like the setuid flag does for user-based privileges.

Usually we can set setgid on a directory so that all new created files or dirs will inherit it’s group id but not the user’s default group.


First we use chmod command to set setgid attribute on directory dir1

bash-3.2$ chmod g+s dir1
bash-3.2$ ls -l
total 4
drwxr-s--- 2 user1 group1 4096 Feb  1 15:42 dir1

Then we su to another user and create a new file and directory,we can see all the new created files inherit dir1′s group id.

bash-3.2$ groups
group2 group1
bash-3.2$ cd dir1
bash-3.2$ touch file2
bash-3.2$ mkdir dir2
bash-3.2$ ls -l
total 4
drwxr-s--- 2 user2 group1 4096 Feb  1 15:46 dir2
-rw-r----- 1 user2 group1    0 Feb  1 15:46 file2

» Filed Under Linux


Leave a Reply