0
0
Linux-cliConceptBeginner · 3 min read

Sticky Bit in Linux: What It Is and How It Works

The sticky bit in Linux is a permission setting that restricts file deletion within a directory so only the file owner, directory owner, or root can delete or rename files. It is commonly used on shared directories like /tmp to prevent users from deleting each other's files.
⚙️

How It Works

The sticky bit is a special permission that you can set on directories in Linux. When set, it changes how files inside that directory can be deleted or renamed. Normally, if you have write permission on a directory, you can delete or rename any file inside it, even if you don't own the file.

With the sticky bit set, only the file's owner, the directory's owner, or the root user can delete or rename files inside that directory. Think of it like a shared locker room where everyone can put their stuff, but only the owner of each item or the locker room manager can remove it. This prevents accidental or malicious deletion of other users' files.

💻

Example

This example shows how to set the sticky bit on a directory and how it affects file deletion permissions.

bash
mkdir shared_dir
chmod 1777 shared_dir
ls -ld shared_dir
Output
drwxrwxrwt 2 user user 4096 Apr 27 12:00 shared_dir
🎯

When to Use

The sticky bit is useful on directories where many users can create files but should not delete others' files. A common example is the /tmp directory, where temporary files from all users are stored. Setting the sticky bit there prevents users from deleting or renaming files they do not own.

Use the sticky bit on any shared directory to protect users' files from being removed by others while still allowing everyone to add files.

Key Points

  • The sticky bit is set using the permission mode 1xxx, often 1777 for shared directories.
  • It only affects deletion and renaming of files inside the directory, not reading or writing files.
  • Commonly used on /tmp and other shared temporary directories.
  • Helps prevent accidental or unauthorized file removal in multi-user environments.

Key Takeaways

The sticky bit restricts file deletion inside a directory to file owners, directory owner, or root.
Set the sticky bit with chmod 1777 on shared directories like /tmp.
It protects users' files from being deleted by others while allowing shared write access.
The sticky bit only affects deletion and renaming, not file reading or writing.