≡ Menu

UNIX / Linux: How to Use Sticky Bit on Directory and File

You would set the sticky bit primarily on directories in UNIX / Linux.

If you set the sticky bit to a directory, other users cannot delete or rename the files (or subdirectories) within that directory.

When the sticky bit is set on a directory, only the owner and the root user can delete / rename the files or directories within that directory.

1. Set the sticky bit on Directory

The example below enables the sticky bit on a directory.

Use chmod command to set the sticky bit. If you are using the octal numbers in chmod, give 1 before you specify other numbered privileges, as shown below. The example below, gives rwx permission to user, group and others (and also adds the sticky bit to the directory).

$ chmod 1777 dir

Or, you can assign only sticky bit to an existing directory (without touching any other user, group and other privileges) using chmod command as shown below.

$ chmod +t dir

Once the sticky bit is assigned to a directory, you’ll see (t) as the last character in the permission. In this example, it is drwxrwxrwt.

$ ls -ld /home/bala/dir
drwxrwxrwt 2 bala bala 4096 2011-01-28 14:09 /home/bala/dir
$ ls -l dir
total 8
-rwxrwxrwx 1 bala   bala   20 2011-01-28 14:12 bala.txt
-rwxrwxrwx 1 guest guest 41 2011-01-28 14:13 guest.txt

In the above example, as dir has rwx permission to everybody, all other users are allowed to do create their files or directories under this directory. However, even when the sub-directories or files under dir is having rwx permission to everybody, only the owner of those can delete or rename those files and directory. Other users cannot delete or rename it because of sticky bit.

In the above example, bala.txt has rwx to users, groups, and others. But, when guest user is trying to delete the file bala.txt, he’ll see the “Operation not permission” message as shown below.

$ su guest
Password:
$ cd /home/bala/dir1
$ rm bala.txt
rm: cannot remove `bala.txt': Operation not permitted

Please note that /tmp has sticky bit enabled by default. You might have not noticed that until now. Now you know why /tmp directory is supposed to have sticky bit enabled.

$ ls -ld /tmp
drwxrwxrwt 3 root root 4096 Jan 31 08:29 /tmp

To remove the sticky bit from a directory, do the following.

$ chmod -t dir

2. Set the sticky bit on File

Setting the sticky bit on a file is pretty much useless, and it doesn’t do anything. On some of the older *nix flavors, a sticky bit enabled executable file will be loaded to the swap memory after 1st execution, which speeds up all subsequent execution. This is not true anymore.

From Wikipedia:

Currently, this behavior is only operative in HP-UX, NetBSD, and UnixWare. 
Solaris appears to have abandoned this in 2005.[citation needed] The 4.4-Lite 
release of BSD retained the old sticky bit behavior but it has been subsequently 
dropped from OpenBSD (as of release 3.7) and FreeBSD (as of release 2.2.1); 
it remains in NetBSD. 

No version of Linux has ever supported the traditional behavior.
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.

  • chmurli February 1, 2011, 3:19 am

    thanks, great article. I known that alredy but its good to refresh memory 🙂

  • chmurli February 1, 2011, 3:24 am

    oh, I forgot. Are you sure about sticy bit on files? I thought that on modern Linux distros it do nothing, is just ignored or forbidden.

  • ashwin February 1, 2011, 4:21 am

    A very good refresher. These are very important basics for any *nix user. Thanks, Bala.

  • rod February 1, 2011, 6:52 am

    Nice article.
    It`s very clarifying.
    Keep more coming, dude!

  • SaveTheRbtz February 1, 2011, 5:53 pm

    From FreeBSD’s man 8 sticky:
    DESCRIPTION
    A special file mode, called the sticky bit (mode S_ISTXT), is used to indicate special treatment for directories. It is ignored for regular files.

  • rod February 2, 2011, 4:24 am

    STICKY(8) BSD System Manager’s Manual STICKY(8)

    NAME
    sticky — sticky text and append-only directories

    DESCRIPTION
    A special file mode, called the sticky bit (mode S_ISVTX), is used to indicate special treatment for shareable executable files and directories. See
    chmod(2) or the file /usr/include/sys/stat.h for an explanation of file modes.

    STICKY TEXT EXECUTABLE FILES
    The sticky bit has no effect on executable files. All optimization on whether text images remain resident in memory is handled by the kernel’s virtual mem-
    ory system.

  • Ramesh Natarajan February 2, 2011, 10:59 am

    @chmurli, @SaveTheRbtz, @rod,

    Thanks for pointing it out. I have updated the article accordingly.

  • magesh Bhaskaran February 3, 2011, 12:17 am

    Hi sir,

    I just want to say one thing after reading your book linux 101 hacks and that is “Hats off ” really its intresting book.
    Can you give me any suggestions /instructions to increase my knowldege furthur more in linux

    Thanks,
    Magesh Bhaskaran

  • Atul Singh February 7, 2011, 3:16 am

    Hi
    This is very useful info, thanks for this.
    But I tried this technique in my system.. where this is the dir hierarchy..
    /atul (drwxrwxrwx)
    /atul/dir1 (drwxdrwxdrwxt)

    another user, who is my grp mate, is not able to remove my dir when he use
    rm /atul/dir1

    but he deleted my dir when he uses..
    rm -rf /atul/dir1

    now, what is the use of “Sticky Bit” ????

    If I m doing wrong somewhere please let me know

    thanks
    Atul Singh

  • Sameer Ambekar February 8, 2011, 8:20 am

    Hi Ramesh,

    Nice article on Sticky bit. I am waiting for article on set userid and group id from you.

  • Felix Frank February 11, 2011, 6:32 am

    Atul: You must set the sticky bit on /atul if you want /atul/dir1 to be irremovable.

  • Atul Singh February 14, 2011, 1:02 am

    : Felix : Thanks Felix, this worked… 🙂

  • Ashish Sood February 23, 2011, 2:22 am

    Knowledgeable
    Thanks

  • Ajit February 28, 2011, 5:43 am

    Your blogs are always helpful….:)

  • pradeep December 6, 2011, 12:21 am

    good one

  • sivakumar August 10, 2012, 9:24 am

    really i am very thank full to you . Could you please tell me how inode will map to a file ?

  • bai September 25, 2012, 8:41 am

    /filesystems

    all i am using sunsolaris platform in that i have my glassfish server. My question is my server creates some dynamic folders and files under one main folder. how can i give permission to all the files and folders created by glassfish server permanently to rwx rwx rw i am really worried can some one help me.
    thanks in advance.

  • Meduri sam November 4, 2012, 7:55 pm

    Sticky bit gives security to our unix direbtory. Thanks sir…

  • Paul April 15, 2013, 2:59 am

    The information about the sticky bit on files isn’t entirely correct:

    > gunzip somefile
    gzip: somefile has the sticky bit set – file ignored

  • karthik October 11, 2013, 10:30 am

    Great , was useful thanks Bala

  • Sandeep Patade November 30, 2013, 4:57 am

    Hi, i’ve one query. how to assign sticky bit automatically when creating new folder..
    1) i have create one folder called “test” and assign the stick bit for that folder. but when users create the new folder under the “test” folder, sticky bit will not applicable for the new folder. so when create files into that new folder whoever will be deleted that files.

    so any idea where we can set sticky bit automatically when users create new folder ??

  • Nina Steiger January 25, 2014, 12:00 pm

    Any idea how to automatically set the sticky bit on new folders?
    Using ACLs and setting a default ACL for others with

    setfacl -d -m o::–T folder

    does unfortunately not work!

  • Suresh September 19, 2015, 12:26 pm

    Hi Sri

    I am the owner on a directory(taste) having sticks bit I and I want to delete a file but I am unable to delete files can any one tell me the answer

  • Anonymous January 6, 2016, 9:39 am

    qmail uses sticky bit on home directory to temporarily pause delivery