devkult_
tools30converters34

chmod g+s

Sets setgid: files created in the directory inherit its group.

On a directory, the setgid bit makes every new file and subdirectory belong to the directory's group instead of the creator's primary group. That's what keeps a shared project folder shared.

On Linux, new subdirectories inherit the setgid bit too, so you only set it once at the top.

What it changes

Before
drwxrwxr-x (775)
After chmod g+s
drwxrwsr-x (2775)

the s replaces the group's x

Examples

chgrp -R devs /srv/shared && chmod g+s /srv/shared

give the folder to the team's group, then make new files inherit it

chmod 2775 /srv/shared

the same as a full numeric mode: setgid plus rwxrwxr-x

find /srv/shared -type d -exec chmod g+s {} +

add it to existing subdirectories as well

chmod g-s /srv/shared

remove it

Good to know

  • Group members also need write permission on what others create. Set umask 002 for them, or new files come out 644 and the group can't edit them.
  • In ls -l, a lowercase s means setgid plus group execute; a capital S means setgid without execute, which is rarely what you want on a directory.
  • GNU chmod keeps a directory's setgid bit when you apply a numeric mode like 775. To clear it numerically, write 00775 — or just use chmod g-s.
  • On an executable file, setgid makes the program run with the file's group. A few system tools rely on that; don't set it on your own files without a reason.

Frequently asked questions

What does chmod g+s do?

It sets the setgid bit. On a directory, files created inside inherit the directory's group. On an executable, the program runs with the file's group instead of the user's.

What is the difference between chmod g+s and chmod 2775?

g+s adds only the setgid bit and leaves the rest of the mode alone. 2775 sets the whole mode: setgid plus rwxrwxr-x.

Why doesn't chmod 775 remove the setgid bit?

GNU chmod deliberately preserves setuid and setgid on directories when you give a numeric mode without the leading digit. Use chmod g-s dir or chmod 00775 dir.

Related