Skip to content

Linux Foundations / Manage a Linux host ยท Step 14 of 50

Lesson: The Sticky BitSwitch
โ† Linux Foundations

System administration

The Sticky Bit

Find out how the sticky bit stops users deleting each other's files in shared directories.

Anyone with write and execute permission on a directory can delete or rename the files inside it, even files they do not own. That is a problem for a directory everyone can write to. The sticky bit solves it.

What it does

In a directory with the sticky bit, a file can be removed or renamed only by:

  • the owner of the file,
  • the owner of the directory, or
  • root (more precisely, a suitably privileged process).

Everyone with write access can still create new files there.

The classic example is /tmp:

$ ls -ld /tmp
drwxrwxrwt 18 root root 4096 Mar  8 09:00 /tmp

The t in the last position is the sticky bit.

If one user tries to delete another's file, they get an error:

$ rm /tmp/liam.txt
rm: cannot remove '/tmp/liam.txt': Operation not permitted

rm may first ask for confirmation if you cannot write to the file.

The sticky bit does not protect a file's contents. Reading and writing are still controlled by the file's own permissions. If Liam makes his file writable by everyone, others may be able to overwrite what is in it, although they still cannot remove it. Many Linux systems add further protections in world-writable sticky directories, so behaviour there can be stricter.

Setting it

$ mkdir dropbox
$ chmod 1777 dropbox
$ ls -ld dropbox
drwxrwxrwt 2 maya maya 4096 Mar  8 09:10 dropbox

The leading 1 is the sticky bit. The symbolic form is chmod +t dropbox, and chmod -t dropbox removes it.

A capital T means the sticky bit is set but others do not have execute permission:

$ chmod 1770 dropbox
$ ls -ld dropbox
drwxrwx--T 2 maya maya 4096 Mar  8 09:10 dropbox

That can be intentional, for example when only a group should use the directory.

The name is historical. Older Unix systems used the bit on program files to keep them in swap space. Linux ignores it on regular files, so today it matters on directories.

Try it

$ ls -ld /tmp /var/tmp
$ mkdir box && chmod 1777 box
$ stat -c '%A %a %n' box
$ chmod o-x box
$ stat -c '%A %a %n' box

Watch the t turn into T. If you have a test machine with two accounts, create a file as one user and try to delete it as the other.

Check your understanding

The Sticky Bit

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.