How to Add Sicky Bit for a Directory in Linux

By | April 24, 2010

First of all, let me explain, what the sticky bit is. When set, it tells the operating system to retain the text segment of the application in swap space after the process exited. This speeds up subsequent executions as it allows the kernel to prevent multiple operations of moving the program from swap to real memory.

The most common use of the sticky bit today is related to directories. When the sticky bit is set, only the item’s owner, the directory’s owner, or the root can rename, delete, or modify files. Without the sticky bit set, any user with write and execute permissions for the directory can rename or delete contained files, regardless of owner. Typically this is set on the /tmp directory to prevent users from deleting or moving other users’ files.

In order to set the sticky bit, a simple command is used:

chmod +t /tmp

In order to check whether the sticky bit is set, here is another command:

ls -ld /tmp

If you r last bit is ‘t’, then the sticky bit is set to the directory. It is a common practice to protect this folder in such a way as many server applications store their temporary data in this directory. For example, default MySQL configuration is set to store its socket file in /tmp/mysql.sock. You can experience problems if you don’t set a sticky bit on your /tmp directory.