How to Use Sticky Bit on Directory and File

You would set the sticky bit primarily on directories in UNIX / Linux.If you set the sticky bit to a directory, other users cannot delete or rename the files (or sub-directories) within that directory. When the sticky bit is set on a directory, only the owner and the root user can delete / rename the files or directories within that directory

01. Set the sticky bit on Directory

The example below enables the sticky bit on a directory.Use chmod command to set the sticky bit. If you are using the octal numbers in chmod, give 1 before you specify other numbered privileges, as shown below. The example below, gives rwx permission to user, group and others (and also adds the sticky bit to the directory).

$ chmod 1777 dir

Or, you can assign only sticky bit to an existing directory (without touching any other user, group and other privileges) using chmod command as shown below.

$ chmod +t dir

Once the sticky bit is assigned to a directory, you’ll see (t) as the last character in the permission. In this example, it is drwxrwxrwt.

$ ls -ld /home/justgeek/dir
drwxrwxrwt 2 justgeek justgeek 4096 2011-01-28 14:09 /home/justgeek/dir

$ ls -l dir
total 8
-rwxrwxrwx 1 justgeek justgeek 20 2011-01-28 14:12 justgeek.txt
-rwxrwxrwx 1 guest guest 41 2011-01-28 14:13 guest.txt

In the above example, as dir has rwx permission to everybody, all other users are allowed to do create their files or directories under this directory. However, even when the sub-directories or files under dir is having rwx permission to everybody, only the owner of those can delete or rename those files and directory. Other users cannot delete or rename it because of sticky bit.

In the above example, as dir has rwx permission to everybody, all other users are allowed to do create their files or directories under this directory. However, even when the sub-directories or files under dir is having rwx permission to everybody, only the owner of those can delete or rename those files and directory. Other users cannot delete or rename it because of sticky bit.

In the above example, justgeek.txt has rwx to users, groups, and others. But, when guest user is trying to delete the file justgeek.txt, he’ll see the “Operation not permission” message as shown below. when guest user is trying to delete the file justgeek.txt, he’ll see the “Operation not permission” message as shown below.

$ su guest
Password:
$ cd /home/justgeek/dir1
$ rm justgeek.txt
rm: cannot remove `justgeek.txt': Operation not permitted