Linux Sticky Bit Concept Explained with Examples

by Himanshu Arora on February 26, 2013

Think of a scenario where you create a Linux directory that can be used by all the users of the Linux system for creating files. Users can create, delete or rename files according to their convenience in this directory. For all those who think that why would such a directory be created? There exists, for example, /tmp directory in the Linux system that can be used by different Linux users to create temporary files.

Now, what if an user accidentally or deliberately deletes (or 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.

A Sticky bit is a permission bit that is set on a file or a directory that lets only the owner of the file/directory or the root user to delete or rename the file. No other user is given privileges to delete the file created by some other user.

History of Sticky Bit

Before explaining the sticky bit further, lets discuss the history of sticky bit as this information is worth discussing.

The sticky bit is not a new concept. In fact, it was first introduced in in 1974 in the Unix operating system. The purpose of sticky bit back then was different. It was introduced to minimize the time delay introduced every time when a program is executed.

When a program is executed, it takes time to load the program into memory before the user can actually start using it. If a program, for example an editor is used frequently by users the the start-up time delay was an overhead back then.

To improve this time delay, the sticky bit was introduced. The OS checked that if sticky bit on an executable is ON, then the text segment of the executable was kept in the swap space. This made it easy to load back the executable into RAM when the program was run again thus minimizing the time delay.

Though this method proved successful in minimizing the start-up time delay but there was a major problem that surfaced due to this operation. The problem was that if some kind of patch was applied to the executable as a bug fix or a new feature then the following steps were to be carried to out :

  • First remove the sticky bit from the executable
  • Now, run the executable and exit it so that the existing text segment from the swap is flushed
  • Now, again set the sticky bit on the executable and re-run the executable so that new text segment is stored in swap memory

The above steps were required so that the program reflect the new features or bug fixes that were added to the executable.

So this was one of the main problems. Also, with the evolution of technology, fast memory access techniques evolved which kind of obsoleted the requirement of sticky bit for this purpose.

Sticky bit Examples

In this section, we will discuss how to set and unset sticky bit using some examples.

A basic example

Create a directory and provide all the users read-write-execute access to it :

# mkdir allAccess

# chmod 777 allAccess/

# ls -ld allAccess/
drwxrwxrwx 2 himanshu himanshu 4096 Oct 24 15:43 allAccess/

So we see that a directory named ‘allAccess’ is created and read-write-execute access to this directory is given to all the users through chmod command.

Now, create multiple files in this directory (with different users) such that all users have read-write-execute access to them.

For example:

# ls -l allAccess/
total 0
-rwxrwxrwx 1 himanshu himanshu 0 Oct 24 15:48 user1
-rwxrwxrwx 1 guest    guest    0 Oct 24 16:11 user_file_0
-rwxrwxrwx 1 guest-2  guest-2  0 Oct 24 16:15 user_file_1

The files user_file_0 and user_file_1 are created by different users but have read-write-execute access on for all the users. This means that the user ‘guest’ can delete or rename the file created by user ‘guest-2′.

In order to avoid this, sticky bit can be set on the directory allAccess.

Now, turn ON the sticky bit on the directory by using +t flag of chmod command.

# chmod +t allAccess/

# ls -ld allAccess/
drwxrwxrwt 2 himanshu himanshu 4096 Oct 24 16:19 allAccess/

As can be observed, a permission bit ‘t’ is introduced in the permission bits of the directory.

Now, if the user ‘guest’ tries to rename the file ‘user_file_1′, here is what happens :

$ mv /home/himanshu/allAccess/user_file_1 /home/himanshu/allAccess/user_file_0
mv: cannot move `/home/himanshu/allAccess/user_file_1' to `/home/himanshu/allAccess/user_file_0': Operation not permitted

So we see that the operation was not permitted.

Remove sticky bit using -t option

Sticky bit can be removed from a directory permissions through the -t option of the chmod command.

Here is an example :

# chmod -t allAccess/

# ls -ld allAccess/
drwxrwxrwx 2 himanshu himanshu 4096 Oct 24 16:19 allAccess/

So we see that the permission bit ‘t’ is removed from directory.

Different OS behave differently with sticky bits as explained in this wikipedia article. For example, Linux only looks for sticky bit if a user tries to rename a file. It will not check the sticky bit if a file is being deleted.

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 23 comments… read them below or add one }

1 Bob February 26, 2013 at 9:17 am

I like this. Learned something new today.

2 royalcharlie February 26, 2013 at 9:31 am

Cool stuff! Thanks

3 Deniz Gezmis February 26, 2013 at 9:59 am

Well explained. Thanks,.

4 Robert Ritter February 26, 2013 at 10:04 am

Actually, the sticky bit in Linux only works on directories. It is ignored on files. On those operating systems that respect the sticky bit on files, it does not prevent the file from being deleted from the filesystem, but rather determines what the kernel will do with the file in RAM (this varies among OSes.)

Love your blog. I read it regularly and pass your information on to my Linux students. Keep up the good work!

5 Ghasem Pahlavan February 26, 2013 at 12:18 pm

very very useful
thanks a lot :)

6 Jalal Hajigholamali February 26, 2013 at 12:55 pm


Your blog is lovly

Thanks a lot…

7 Asyraf Aziz February 26, 2013 at 7:19 pm

Useful information that i learn today! Thanks man!

8 Tarun February 26, 2013 at 10:17 pm

Great Article :)

Can you please look into arch linux setup once, and write an article about it. I know there are many on internet, but your articles are more friendly and easy to understand.

9 Prachi February 26, 2013 at 11:02 pm

Thank you for this..this is my favorite blog.. its so user friendly..easy to understand.. thanks a lot..btw, I always get confused with set uid and set gid bits, can you explain

10 Athul February 26, 2013 at 11:33 pm

Expecting similar posts for “setuid” and “setgid” bits

11 Rupam Roy February 27, 2013 at 12:10 am

Well Explained with Examples…. Great Job.. Keep it up…

12 SureshG February 27, 2013 at 12:16 am

As usual, cool stufff…….

13 random February 27, 2013 at 1:40 am

Superb explanation and easy to read. Thanks!

14 Bothwell February 27, 2013 at 4:24 am

cool thanks …

15 atreyu February 27, 2013 at 8:01 am

“Different OS behave differently with sticky bits as explained in this wikipedia article. For example, Linux only looks for sticky bit if a user tries to rename a file. It will not check the sticky bit if a file is being deleted.”

This comment made me curious, so I just tested this on RHEL4. I set the sticky bit on my directory, and then was NOT able to `rm` the file. It gave the same error as a `mv` command: Operation not permitted.

16 TheFu February 27, 2013 at 10:44 am

I look forward to the follow up article on setuid and setgid.
UNIX/Linux file permissions are deceptively complex AND simple.

17 DoobiesGood March 1, 2013 at 7:12 am

Excellent article, clearly explained.
I learned something useful today!

18 Ravikanth reddy March 2, 2013 at 7:56 pm

Short and simple.

19 Praveen Kumar March 19, 2013 at 5:51 am

Nice article !! and useful learning

20 Ehan Chang March 19, 2013 at 8:36 am

good explaination

21 murugesh March 20, 2013 at 9:30 am

Nice explanation, love ur article

22 Afreen April 1, 2013 at 9:40 am

its really awsome blog….
i learnt very interesting thing…

23 safeer November 11, 2014 at 10:13 pm

Nicely explained..Good job

Leave a Comment

Previous post:

Next post: