sticky bit on a directory

The output of the command ls -ld /tmp is:

drwxrwxrwt 30 root root 20480 Mar 11 14:17 /tmp

So I have two main questions:

  • What is the letter t after the permissions?
  • As far as I know /tmp is used to create temporary files related to different users in the system, so how come it has permission rwxrwxrwx (777)?

A sticky bit is a permission bit that is set on a directory that allows only the owner of the file within that directory or the root user to delete or rename the file. No other user has the needed privileges to delete the file created by some other user.

This is a security measure to avoid deletion of critical folders and their content (sub-directories and files), though other users have full permissions.

Why does /tmp have the t sticky bit?

The /tmp directory can be used by different Linux users to create temporary files. Now, what if an user deletes/rename a file created by some other user in this directory?

Well, to avoid these kind of issues, the concept of sticky bit is used. So for that a 777 is given but preserving the sticky bit is not a bad idea.

How can I setup the sticky bit for a directory?

I’ll set a sticky bit on a directory called test on my Desktop.

Symbolic way (t represents the sticky bit):

chmod o+t ~/Desktop/test

or

chmod +t ~/Desktop/test

Numerical/octal way (1, sticky bit bit as value 1 in the first position)

chmod 1757 ~/Desktop/test

Now let us test the results:

ls -li ~/Desktop/test

1551793 drwxrwxrwt 45 hadi hadi 20485 Mar 11 14:35 ~/Desktop/test

To delete/Remove a sticky bit

chmod o-t ~/Desktop/test

Now let us test the results:

ls -li ~/Desktop/test

1551793 drwxrwxrwx 45 hadi hadi 20485 Mar 11 14:35 ~/Desktop/test