On a Linux host, `ls -ld /tmp` prints `drwxrwxrwt`. What does that trailing `t` do, and why is it needed on a world-writable directory?
answer
- last character of the mode string
- deletion is a directory-level right
- only the owner may remove it
- octal 1000, chmod +t
- t versus T in ls output
basics
~20 sThe trailing t is the sticky bit, also called the restricted-deletion flag. On a world-writable directory it still lets anyone create files, but only a file's owner, the directory's owner, or root may rename or delete an entry.
solid answer
~40 sDeletion on Unix is not controlled by the file's own permissions — `unlink()` modifies the *directory*, so write permission on the directory is what decides it. That means in a plain `0777` directory any user could delete anyone else's files, which is exactly what a shared scratch area like `/tmp` needs to avoid. The sticky bit (octal `1000`, symbolically `chmod +t`) adds a restriction on top: within that directory, a file may only be renamed or unlinked by the file's owner, the directory's owner, or a privileged process. Everyone can still create their own files freely. `ls` shows it in the other-execute slot, as `t` when the other-execute bit is also set and `T` when it is not. `/tmp`, `/var/tmp` and `/dev/shm` are mode `1777` for this reason.
code
bash · 3 linesmkdir -p /tmp/shared-demo
chmod 1777 /tmp/shared-demo
ls -ld /tmp/shared-demogo deeper
Recognise the final t in drwxrwxrwt as the sticky bit and say plainly that in /tmp anyone can create files but only the file's owner can delete their own.
Explain why the bit is needed at all: unlink checks permission on the directory, not the file, so a world-writable directory would otherwise let anyone remove anyone's files. Know chmod 1777 and the t versus T distinction.
Be able to state the bit's limits under pressure — contents of a world-writable file can still be overwritten, and symlink races in /tmp are handled by fs.protected_symlinks rather than by this bit.
Frame shared writable directories as a design smell: argue for per-service private directories or group-owned 1770 trees over a global scratch space, and know what your fleet baseline sets for the protected_* sysctls.
## Why the bit exists at all The surprising premise behind this question is that **deleting a file has nothing to do with the file's permissions**. Removing a name from the filesystem is `unlink()`, and `unlink()` modifies the *directory* that holds the name, not the file's data. So the kernel checks write and search permission on the **containing directory**. A read-only file owned by root can be deleted by anybody who can write the directory it lives in; a file you own and have full `rwx` on cannot be deleted if the directory is not writable by you. That rule is fine for `/home/alice`, where only Alice can write. It falls apart for a shared scratch directory. `/tmp` must be writable by every user on the machine so that any process can drop temporary files there — but a plain mode `0777` directory would let any user delete or rename any other user's temporary files, which is trivially a denial-of-service and, worse, the setup for a swap-the-file race. ## What the sticky bit actually restricts The sticky bit (`S_ISVTX`, octal `1000`) is the fix. When it is set on a directory, an entry inside it may only be renamed or unlinked by: - the owner of the file, - the owner of the directory, or - a privileged process (one holding `CAP_FOWNER`). Everything else is unchanged: any user with write permission can still **create** new entries, and read permissions on existing files are untouched. The bit narrows exactly one operation — removing or renaming somebody else's name. ``` $ ls -ld /tmp drwxrwxrwt 18 root root 4096 ... ``` ## Reading and setting it `ls -l` has no spare column, so the three special bits are overloaded onto the execute positions. The sticky bit appears in the **other-execute** slot: lowercase `t` means the bit is set *and* other-execute is also set; uppercase `T` means the sticky bit is set but the directory is not searchable by others — which is usually a mistake on a shared directory, since nobody could enter it. Set it numerically as a fourth leading octal digit, or symbolically: ``` chmod 1777 /srv/scratch chmod +t /srv/scratch chmod -t /srv/scratch ``` ## What it does not do This is where candidates lose points. - **It does not protect file contents.** If a file inside the sticky directory is itself world-writable, another user can still open it and overwrite or truncate it in place. The name survives; the data does not. The sticky bit protects the *directory entry*, not the inode. - **It does not stop reading.** Anything world-readable in `/tmp` is readable by everyone on the box. - **It does not close symlink and hardlink races.** A classic attack is planting a symlink in `/tmp` that a privileged program later follows or truncates. Linux hardens this separately with the sysctls `fs.protected_symlinks` and `fs.protected_hardlinks` (plus `fs.protected_fifos` and `fs.protected_regular`), which restrict following symlinks and creating hardlinks in world-writable sticky directories when the owners do not match. They are enabled by default on current distributions and are a separate mechanism from the mode bit. - **On Linux it is meaningless on a regular file.** The name is a fossil: on early Unix the bit asked the kernel to keep a program's text segment in swap so it reloaded faster. Linux ignores it on regular files entirely; the modern meaning is the directory rule only. ## Where you meet it in practice Beyond `/tmp` and `/var/tmp`, the same pattern appears on `/dev/shm`, on shared upload or spool directories, and anywhere multiple service accounts write into one place. If you build such a directory yourself, mode `1777` — or better, `1770` with a shared group so the world cannot even list it — is the standard shape. The interviewer's real target is whether you know that deletion is a directory-level right. Say that first, then the bit follows naturally: the sticky bit is the one exception the kernel makes to "write on the directory means you can remove anything in it".
- If the sticky bit stops me deleting your file in /tmp, can I still damage it?Yes, if the file itself is writable by you. The sticky bit only guards the directory entry — rename and unlink. Opening a world-writable file and truncating or overwriting it is a permission check against the file's own mode, which the sticky bit never touches. That is why temporary files should be created with a restrictive mode, not left at 666.
- What does an uppercase T mean instead of a lowercase t?Both mean the sticky bit is set; the case reports the other-execute bit that shares the slot. Lowercase t means other-execute is also on, uppercase T means it is off. On a directory, no execute for others means they cannot traverse into it at all, so a T on a supposedly shared directory is usually an accident.
- Does the sticky bit do anything on a regular file on Linux?No. Historically it asked the kernel to keep the executable's text segment in swap for faster reloads, which is where the name comes from. Linux ignores it on regular files; only the directory meaning survives. Seeing it on a file is harmless but is almost always a leftover or a misunderstanding.
A pinboard in a corridor: anyone may pin up a note, but only the person who pinned a note (or the office manager) may take it down.
saying these in an interview costs you the question
- Says the sticky bit keeps a file's contents from being modified.
- Thinks the sticky bit pins the file in memory or cache.
- Believes file permissions decide who may delete a file.
- Claims root is also blocked from deleting other users' files.
- Says the sticky bit prevents users from creating files in /tmp.