≡ Menu

Linux Sticky Bit Concept Explained with Examples

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.

Add your comment

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

Comments on this entry are closed.

  • Bob February 26, 2013, 9:17 am

    I like this. Learned something new today.

  • royalcharlie February 26, 2013, 9:31 am

    Cool stuff! Thanks

  • Deniz Gezmis February 26, 2013, 9:59 am

    Well explained. Thanks,.

  • Robert Ritter February 26, 2013, 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!

  • Ghasem Pahlavan February 26, 2013, 12:18 pm

    very very useful
    thanks a lot 🙂

  • Jalal Hajigholamali February 26, 2013, 12:55 pm

    Hi,

    Your blog is lovly

    Thanks a lot…

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

    Useful information that i learn today! Thanks man!

  • Tarun February 26, 2013, 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.

  • Prachi February 26, 2013, 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

  • Athul February 26, 2013, 11:33 pm

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

  • Rupam Roy February 27, 2013, 12:10 am

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

  • SureshG February 27, 2013, 12:16 am

    As usual, cool stufff…….

  • random February 27, 2013, 1:40 am

    Superb explanation and easy to read. Thanks!

  • Bothwell February 27, 2013, 4:24 am

    cool thanks …

  • atreyu February 27, 2013, 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.

  • TheFu February 27, 2013, 10:44 am

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

  • DoobiesGood March 1, 2013, 7:12 am

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

  • Ravikanth reddy March 2, 2013, 7:56 pm

    Short and simple.

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

    Nice article !!..new and useful learning

  • Ehan Chang March 19, 2013, 8:36 am

    good explaination

  • murugesh March 20, 2013, 9:30 am

    Nice explanation, love ur article

  • Afreen April 1, 2013, 9:40 am

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

  • safeer November 11, 2014, 10:13 pm

    Nicely explained..Good job

  • venkat February 27, 2015, 12:10 am

    nice

  • sagar March 16, 2015, 3:32 am

    what is the difference between applying read only (chmod 755 file_name) and sticky bit on file

  • Gautam July 31, 2015, 9:40 am

    But what if those files were made read only for other users?
    I didn’t get the use of sticky bit here. We could have modified access rights of individual files.

  • André October 2, 2015, 9:19 pm

    Thanks!!

  • Pridhvi January 22, 2016, 6:09 am

    Thank you Ramesh
    It’s nice article.

  • Asis April 24, 2016, 8:42 pm

    well explanation with example…..
    keep on….

  • AjayKumar Basuthkar January 3, 2017, 2:23 am

    Theory, Practical, History, Purpose all covered 🙂 well done!

  • Ashok April 25, 2017, 6:54 pm

    It’s the good Articles .

    Who has written this Thank you.

  • Sankar May 22, 2017, 10:20 pm

    Simple explanation with example which is easy to understand clearly.
    Thanks