chmod 2775: The setgid Bit on Directories and on Files
2775 is rwxrwsr-x: the 775 mode plus the setgid bit. On a directory that makes every new entry inherit the directory's group instead of the creator's, which is what makes a shared directory actually shared.
At a glance
- Octal
- 2775. The leading 2 is the setgid bit
- Symbolic
- u=rwx,g=rwxs,o=rx
- On a directory
- drwxrwsr-x
- Effect on a directory
- New files and subdirectories inherit the directory's group, and new subdirectories inherit the setgid bit too
- Effect on an executable file
- The process runs with the effective group id of the file's group
- Lowercase s or capital S
- s means setgid with group execute set. S means setgid without it
- In the calculator
- Available as the 2755 preset and as a setgid checkbox
In detail
The problem setgid solves on a directory is the one every shared folder hits. A file created inside a 775 directory takes the creator's primary group, not the directory's, so Alice creates a file owned by alice:alice and the rest of the team can read it but not edit it. Nobody notices until someone tries to change it days later.
With the setgid bit on the directory, new entries inherit the directory's group automatically, and new subdirectories inherit the setgid bit as well, so the property propagates down the tree without anyone remembering to reapply it. Combined with a umask of 002, which leaves the group write bit set, a team gets a directory where collaboration simply works.
It does not fix files that already exist. Setting 2775 changes the behaviour from that moment on, so an existing tree needs a one time pass with chgrp to correct the group, and usually a chmod to restore group write. Doing the setgid change and forgetting the historical fix is the most common half completed version of this.
On an executable file, setgid means something different: the process runs with the file's group rather than the caller's. That is how a small number of utilities get access to a device or a spool directory without being setuid root. The write utility being setgid to the tty group is the example most people have on their machine.
The security profile is much milder than setuid. A setgid binary gains one group's access rather than a whole identity, and setgid on a directory grants nothing at all, it only changes which group new files belong to. It is still worth knowing where it is set, because a setgid directory quietly changes ownership rules for everything created inside it.
Examples
chmod 2775 /srv/project
chmod u=rwx,g=rwxs,o=rx /srv/projectdrwxrwxr-x developers /srv/project # 775
$ touch /srv/project/a.md
-rw-rw-r-- alice alice a.md # wrong group
$ chmod 2775 /srv/project
drwxrwsr-x developers /srv/project # 2775
$ touch /srv/project/b.md
-rw-rw-r-- alice developers b.md # inheritedchgrp -R developers /srv/project
chmod -R g+w /srv/project
find /srv/project -type d -exec chmod g+s {} +Things that catch people out
- Setting setgid does not retroactively change files already in the directory. Run chgrp -R once to fix the history, or half the tree keeps the old group.
- Group inheritance without group write achieves nothing useful. The umask has to leave the group write bit set, which usually means 002 rather than the default 022.
- New subdirectories inherit setgid, but a directory moved in from elsewhere keeps whatever it had. Use find with -type d to reapply after a move.
- Capital S in the group position means setgid is set without the group execute bit, which on a directory removes traversal for the group and breaks the thing you were trying to enable.
Try it: Chmod Calculator
Tick the nine permission boxes, or type an octal or symbolic value, and get the three digit and four digit octal, the ls -l rendering and the exact chmod command back.
Chmod Calculator
LOCAL · Browser-OnlyCalculate Unix file permissions with an interactive chmod builder
Permission Matrix
| Entity | Read (4) | Write (2) | Execute (1) | Octal | Symbolic |
|---|---|---|---|---|---|
| Owner (u) | 7 | rwx | |||
| Group (g) | 5 | r-x | |||
| Others (o) | 5 | r-x |
Special Bits
Advanced permission flags for executables and directories
Generated Permissions
755rwxr-xr-x-rwxr-xr-xchmod 755 filenamechmod u=rwx,g=rx,o=rx filenamePermission Breakdown
Owner (u)
7rwx- - Read files / list directory contents
- - Write / modify / delete
- - Execute / traverse directory
Group (g)
5r-x- - Read files / list directory contents
- - Execute / traverse directory
Others (o)
5r-x- - Read files / list directory contents
- - Execute / traverse directory
Common Permissions Reference
| Octal | Symbolic | Meaning |
|---|---|---|
777 | rwxrwxrwx | Full access for everyone |
755 | rwxr-xr-x | Owner full, others read and execute |
750 | rwxr-x--- | Owner full, group read and execute |
700 | rwx------ | Owner full access only |
666 | rw-rw-rw- | Read and write for everyone |
644 | rw-r--r-- | Owner read/write, others read only |
600 | rw------- | Owner read/write only |
555 | r-xr-xr-x | Read and execute for everyone |
444 | r--r--r-- | Read only for everyone |
400 | r-------- | Owner read only |
Related File permissions
All file permissions →chmod 775: Owner and Group Write, Everyone Else Reads
775 gives the owner and group full access and leaves others with read and execute. The correct mode for shared directories, and why it needs setgid.
chmod 4755: The setuid Bit and What It Actually Does
4755 sets the setuid bit on top of 755, so the program runs as its owner instead of as the caller. Where it is required and why it is dangerous.
chmod 1777: The Sticky Bit and Why /tmp Needs It
1777 adds the sticky bit to a world writable directory so only a file's owner can delete it. The mode of /tmp and the fix for any shared drop directory.
umask: How the Default Mode of a New File Is Decided
umask removes permission bits from newly created files and directories. Why 022 gives 644 and 755, and why a new file is never executable.
The Execute Bit on a Directory Means Traverse, Not Run
On a directory, execute grants the right to resolve names through it and read grants the right to list them. Why a directory at 644 cannot be entered.
Symbolic chmod Notation: u, g, o, a With Plus, Minus and Equals
Symbolic mode changes one class at a time instead of setting all nine bits. How plus, minus and equals differ, and what the capital X form is for.
Tools for this
- Chmod Calculator Free online chmod calculator - calculate Unix file permissions with an interactive chmod builder