What it means
chmod 1777 is chmod 777 plus the sticky bit. Everyone can create files in the directory, but a file can only be deleted or renamed by its owner, the directory's owner, or root. It's the mode of /tmp and /var/tmp. In ls -l it shows as drwxrwxrwt: the t in the last position is the sticky bit.
| Who | Digit | Read | Write | Execute | Symbolic |
|---|---|---|---|---|---|
| Special | 1 | sticky (t) |
|||
| Owner (u) | 7 | yes | yes | yes | rwx |
| Group (g) | 7 | yes | yes | yes | rwx |
| Others (o) | 7 | yes | yes | yes | rwt |
The first of four digits holds the special bits: 4 is setuid, 2 is setgid, 1 is sticky. The symbolic equivalent:
chmod a=rwx,+t /srv/dropbox
On a directory
Normally, write permission on a directory lets you delete any file in it, whoever owns the file. In a world-writable directory that would let any user delete or replace everyone else's files. The sticky bit closes that gap. Each user can still create files freely, and can delete their own, but not those of others.
ls -ld /tmp
# drwxrwxrwt 12 root root 4096 Oct 5 10:00 /tmp
If you see T (capital) instead of t, the sticky bit is set but others have no execute permission, so the directory isn't usable by others anyway.
On a file
On Linux, the sticky bit on a regular file is ignored. Historically it told some Unix systems to keep a program's text in swap after it exited, which is where the name comes from. Don't set it on files.
Typical uses
/tmpand/var/tmp. Every user and service writes temporary files here. If/tmploses its sticky bit (a bad restore, a carelesschmod), restore it:
sudo chmod 1777 /tmp
- Shared drop directories on multi-user machines where people upload files for each other but shouldn't remove other people's uploads.
What the sticky bit doesn't do
- It doesn't stop others from reading files in the directory. A file created as 644 in
/tmpis readable by every user. Create private temp files withmktemp, which uses mode 600, or setumask 077first. - It doesn't stop others from writing to a file whose own mode allows it. It only protects against delete and rename.
- It doesn't stop name collisions. An attacker can create
/tmp/myapp.lockbefore your program does. Usemktempor a per-user directory such as$XDG_RUNTIME_DIRinstead of fixed names in/tmp.
Linux also has the fs.protected_symlinks and fs.protected_regular settings, which harden sticky world-writable directories against symlink and file-planting attacks. They are enabled by default on many distributions; check with sysctl fs.protected_symlinks.
Prefer narrower options
1777 is right for system temp directories. For a team share, a group-owned directory is usually better: chmod 2775 or 2770, optionally with the sticky bit as well (3775) so members can't delete each other's files.
Recursive pitfalls
Don't run chmod -R 1777. It sets every file inside to world-writable (and sticky, which is ignored on files). Apply the mode to the directory itself only.
Related modes
- chmod 777: world-writable without the protection.
- chmod 2775: the setgid bit for shared group directories.
- chmod 4755: the setuid bit on programs.
Toggle the special bits in the chmod calculator to see how the t, s and S flags appear.