remove sticky bit in linux

How to Remove Sticky Bit in Linux

Linux is a multi-user operating system that allows you to easily share files & folders between several users. However, in such scenarios it is possible that one of the users accidentally deletes or renamed this shared file/folder. In order to prevent this from happening, Linux system provides an s-bit, also known as sticky bit that allows only the file/folder owner to delete it. Once the sticky bit of a file/folder is set, no other user will be able to delete/rename file/folder, other that its owner. In this article, we will look at how to remove sticky bit in Linux.


How to Remove Sticky Bit in Linux

Let us look at how sticky bit works first. Normally, when you run ls -l command, the output will look like the following

$ ls -l
-rwxrwxrwx 1 ubuntu1 ubuntu1    0 Oct 24 16:11 user.txt
drwxrwxrwx 2 ubuntu2 ubuntu2 4096 Oct 24 15:43 data/

If you notice the string in first column, it is -rwxrwxrwx for file user.txt while it is drwxrwxrwx for folder data/ where r-read, w-write, x-execute, d-directory.

But note that the file user.txt is created by user ubuntu1 while the folder data/ is created by user ubuntu2. However, ubuntu1 user can delete/rename folder data/ and user ubuntu2 can delete/rename file user.txt.

To prevent this from happening, we enable sticky bit to both these file & folder using +t option of chmod as shown below.

Here is how to set sticky bit for file.

$ sudo chmod +t user.txt

Here is how to set sticky bit for folder.

$ sudo chmod +t data/

Now when you run ls command you will see the following output.

$ ls -l
-rwxrwxrwxt 1 ubuntu1 ubuntu1    0 Oct 24 16:11 user.txt
drwxrwxrwxt 2 ubuntu2 ubuntu2 4096 Oct 24 15:43 data/

Notice the t bit after -rwxrwxrwx and drwxrwxrwx permissions above, indicating that the sticky bit is set for these documents. Now, only ubuntu1 will be able to delete/rename user.txt file and only ubuntu2 will be able to delete/rename folder data/.

If you want to remove the sticky bit in Linux just use -t option with chmod as shown below.

Here is how to remove sticky bit for file.

$ sudo chmod -t user.txt

Here is how to remove sticky bit for folder.

$ sudo chmod -t data/

Now when you run ls command you will see the following output. The t bit is not present, indicating that sticky bit is removed.

$ ls -l
-rwxrwxrwx 1 ubuntu1 ubuntu1    0 Oct 24 16:11 user.txt
drwxrwxrwx 2 ubuntu2 ubuntu2 4096 Oct 24 15:43 data/

That’s it. In this article, we have learnt what is sticky bit, how to add sticky bit to file/folder, and how to remove sticky bit from file/folder.

Also read:

How to Truncate File in Linux
How to Do Reverse DNS Lookup in Linux
How to SSH Using PEM File in Linux
SCP Command to Copy File Between Servers
How to Read YAML File to Dict in Python

Leave a Reply

Your email address will not be published. Required fields are marked *