What is sticky bit permission, and how to use it

Apart from the regular permission, we can also use special permission, so what is special permission? Special permission is the kind of permission that allows or restricts a user to perform an action while following regular permission.

There are multiple permissions that come under special permission, like

  • SUID, and GUID
  • Sticky bit.

And in this article, you will see what sticky bit is and how to set and remove sticky bits from directories and files.

What is Sticky bit?

sticky bit is special file permission that you can use to protect files from being deleted by anyone except the file owner.

This special permission can be very helpful if you are sharing a similar directory for your work. There may be a scenario occurs when someone deletes files that were not meant to do, so to protect this action, you should use a sticky bit.

A sticky bit is denoted with “t” in permission, and the permission bit is “1”.

How to set sticky bit on a directory or file

You can simply use the chmod command to attach a sticky bit to a directory or file. As you know, chmod does not require sudo privileges unless you are not the owner of a file.

Command syntax:

$ chmod 1XXX or o+t [FILE-NAME]
  • chmod: it is used to manipulate file permissions.
  • 1XXX: number one is defined as a “sticky bit” and XXX is the regular file permission.
  • o+t: alternatively, you can use this to get a sticky bit on a file.
  • [FILE-NAME]: specify the filename to attach.

I have created a directory named “trendoceans” and modified the file permissions with a sticky bit. Except for me, no one can delete this directory as long as the sticky bit is attached.

$ mkdir -v trendoceans
$ chmod 1755 trendoceans
$ chmod o+t trendoceans

After that run the ls -dl command to print permission details.

$ ls -ld trendoceans

drwxr-xr-t 2 trendoceans trendoceans 4096 Feb 28 15:22 trendoceans

As you can see, the directory is now protected with a sticky bit. After that, no other user can delete “trendoceans” from your drive unless you do something.

$ su ankit
$ rm -rf trendoceans

rm: cannot remove 'trendoceans/': Permission denied

How to unset or remove sticky bit from a directory or file

Similarly, you can follow the same steps to create sticky bit files.

It is quite simple to unset or remove a sticky bit from a directory. You can run any of the following commands to remove the sticky bit character.

$ chmod o-t trendoceans
$ chmod 774 trendoceans

Wrap up

The prime example of sticky bit is /tmp directory, where all users can create a file without any restriction but no other user can delete it.

That’s all for sticky bit permission, and how to use it.

Leave a Reply