Now, let's learn about the setgid permission. This flag has two different meanings, which is important to know about and should be memorized. When set on a file, it has the same effect as the setuid permission, but here it will execute a command with the rights of the group owner instead of the file owner.
To set the setgid flag on the file, use the number 2 in the chmod command:
![](assets/6045bc66-e569-4642-b5ab-758dbd38206a.png)
The second meaning of the setgid flag is very important and should be memorized because it can be a typical use case. If you set the setgid on a folder instead of a file, every new file, or folder, or subfolder created within this folder will automatically get the group permissions of the folder where you set the setgid flag. This works for all the files included recursively. This can become very important because normally the group permissions of new files created automatically get assigned by the creator of the file.
So if you want to separate locations in your filesystems for collaborations or group work where you can put in shared files for anyone belonging to a special group, setgid is a very powerful feature. This is like a shared folder you may know about from other operating systems. So if you want to separate your filesystems into locations for collaborations or group work, where anybody who is belonging to a special group can create documents that automatically can be fully accessed by other persons of that same group, just set a setgid flag on a folder.
To test this:
- Create a new folder under the username olip.
- Now, change the group ownership to awesome_group. Now, if a user creates a new file in this folder, it will have the group ownership of that user.
- Now, let's set the setgid flag on that folder and see what happens.
- Let's create a new file under the username olip in that folder where we set the setgid flag:
![](assets/31c7762e-1527-44a9-b50d-73ecf61a3018.png)
As you can see, any new file created in this folder now gets the group ownership of the folder, which is awesome_group. So our setgid flag is working properly.