Sticky Bit in Linux: What It Is and How It Works
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.
mkdir shared_dir
chmod 1777 shared_dir
ls -ld shared_dirWhen 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, often1777for shared directories. - It only affects deletion and renaming of files inside the directory, not reading or writing files.
- Commonly used on
/tmpand other shared temporary directories. - Helps prevent accidental or unauthorized file removal in multi-user environments.