chmod 2775 (rwxrwsr-x)

A 775 directory with the setgid bit. Files created inside take the directory's group instead of their creator's own, which is what keeps a shared directory shared.

Owner
Group
Others
Symbolic
Command

This tool runs in your browser. We never send what you type or paste to a server.

What chmod 2775 sets

WhoDigitReadWriteExecuteSymbolic
Owner (u)7yesyesyesrwx
Group (g)7yesyesyesrws
Others (o)5yesnoyesr-x

The leading 2 sets setgid, shown as s in place of an x.

chmod 2775 file               # numeric
chmod u=rwx,g=rwxs,o=rx file  # the same, in symbolic form

ls -l shows this mode as -rwxrwsr-x on a file and drwxrwsr-x on a directory. On a file, execute means it can be run as a program. On a directory, read lists the names inside, write creates, renames and deletes entries, and execute (search) is what lets you enter it and reach the files inside.

The problem setgid solves

On Linux a new file normally belongs to the primary group of whoever creates it. In a plain 775 directory owned by group project, a file made by ana belongs to group ana, and her colleagues cannot edit it even though they could create it.

With setgid on the directory:

  • a new file gets the directory's group, project;
  • a new subdirectory gets the group and the setgid bit, so the behaviour carries on down the tree.

What setgid does not do

  • It does not make new files group-writable. The mode of a new file still comes from its creator's umask. With umask 022 the file is 644: right group, no group write. Members need umask 002, which gives 664.
  • It does not touch what is already there. Existing files keep their group, and existing subdirectories do not gain the bit.
  • It does not apply to files moved in. mv within a file system keeps a file's group and mode. A copy is a new file, so it does get the directory's group.

Setting up a shared directory

groupadd project                                # Linux, as root
usermod -aG project ana                         # for each member; takes effect at their next login
chgrp -R project /srv/project
find /srv/project -type d -exec chmod 2775 {} +
find /srv/project -type f -exec chmod 664 {} +

Then deal with the umask. Either every member sets umask 002, or, on Linux file systems with ACL support, you give the directory a default ACL. When a directory has one, new files take their permissions from it and the creator's umask is not applied:

setfacl -d -m g::rwx /srv/project               # Linux: new files come out group-writable

If chmod g+s appears to do nothing, check that you are in the directory's group. When an unprivileged user sets the bit on something whose group they do not belong to, the kernel drops it without reporting an error.

Why chmod 775 does not remove it

GNU chmod deliberately leaves the setuid and setgid bits of a directory alone when you give it an ordinary numeric mode, so chmod 775 dir and even chmod 0775 dir leave a 2775 directory as it is. POSIX leaves this behaviour to the implementation. Say what you mean explicitly:

chmod g+s dir          # add setgid, keep everything else
chmod g-s dir          # remove it (portable)
chmod 00775 dir        # GNU: five digits clear the special bits
chmod =775 dir         # GNU: same effect
stat -c '%a %U:%G %n' dir        # GNU/Linux: shows 2775
stat -f '%Mp%Lp %N' dir          # macOS and BSD

Things to be careful about

  • It is still a 775 directory: every group member can delete and rename every file in it. Add the sticky bit (chmod +t, giving 3775) if people should only remove their own.
  • On a regular file, 2775 means something else: a setgid program, which runs with the file's group as its effective group. Treat that with the same suspicion as setuid (4755). chmod -R 2775 does this to every file in the tree, so use the find commands above.
  • drwxrwSr-x, with a capital S, is setgid without group execute: inheritance is on, but group members cannot enter the directory.

2775 compared with its neighbours

ModeSymbolicDifference from 2775
775rwxrwxr-xNo setgid: new files get their creator's own group
2770rwxrws---The same, closed to everyone outside the group
1777rwxrwxrwtOpen to all users, with deletion limited to each file's owner
4755rwsr-xr-xsetuid on a program: a different bit with a different job

Questions

Why are new files in my setgid directory not writable by the group?

Because setgid only controls which group a new file belongs to. Its permissions come from the umask of the process that creates it, and the common umask of 022 produces 644. Have members use umask 002, or set a default ACL on the directory with setfacl -d so that new files are group-writable whatever the umask.

Why does chmod 775 not remove the setgid bit from a directory?

GNU chmod preserves a directory's setuid and setgid bits unless you mention them explicitly. Use chmod g-s on the directory, or a five-digit mode such as chmod 00775. On regular files a plain numeric mode does clear the bits.

What does the s in drwxrwsr-x mean?

The setgid bit is set and the group also has execute permission. On a directory it means files and subdirectories created inside inherit the directory's group. A capital S in the same position means setgid is set without group execute.