Linux Labs

Users, permissions, and processes / Stage 4 of 13

A shared directory: setgid, sticky, and umask

Not running Starting usually ~20 s
Leave the lesson

Step 4 of 7 · on a machine · 6 min

Others' files stay put

bob deleted alice's plan without the right to write to the file itself. The sticky bit closes that gap in a shared directory.

Deleting erases a name from the directory's list, so the directory's rights decide it. The devs group has w on /srv/team, and that was enough for bob.

The sticky bit adds a condition: only the file's owner, the directory's owner, or root may delete or rename a file. The bit sits in the others' x position as the letter t. That is how /tmp, where everyone writes, is set up:

bash
ls -ld /tmp

drwxrwxrwt: anyone can create a file in /tmp, but no one can delete someone else's. Such a directory protects others' files with a refusal: rm: cannot remove '…': Operation not permitted.

chmod +t directory sets the bit. As a number it is a 1 in the same place as setgid's 2, and both at once are chmod 3775.

Warning

On Ubuntu, sticky also gets in the way of editing together: in a sticky directory the group writes to, another person's file will not open for appending — tee -a and >> get Permission denied even though the group has w. This is the kernel's protection against file spoofing: cat /proc/sys/fs/protected_regular prints 2. So sticky goes on a directory where everyone drops their own files.

Check yourself 1 question

The devs team needs two directories: docs for documents everyone edits together, and inbox for reports where each person drops their own. Which modes do you give them?

  • With sticky on docs, Ubuntu will not let anyone append to another person's document: echo x >> file as a colleague gets Permission denied.

  • 1777 opens docs for writing to everyone, not just devs, and does not pass the group to new files. inbox without sticky does not protect reports from deletion.

  • Without sticky on inbox, bob can delete alice's report: the group can write to the directory, so rm goes through.

Editing together needs a shared group for new files — setgid, 2775. For a directory where each person drops their own files, sticky adds protection from deletion — 3775; nobody needs to append to others' files there.

Key points

  • The directory's rights decide deleting and renaming; sticky (t) leaves them to the file's owner, the directory's owner, and root.
  • Setgid and sticky together are chmod 3775, shown as drwxrwsr-t.
  • On Ubuntu, sticky in a group directory blocks appending to others' files: put it where everyone writes their own.
Ubuntu 24.04
Loading…