The Purpose of Sticky Bit in Linux

Nilesh Katuwal Feb 22, 2022
  1. Introduction to the Sticky Bit in Linux
  2. History of the Sticky Bit in Linux
  3. Example of the Sticky Bit in Linux
The Purpose of Sticky Bit in Linux

A sticky bit is a permission bit on a file or directory that allows only the file/directory owner or the root user to remove or rename the file. No other user can delete a file that another user-generated.

Introduction to the Sticky Bit in Linux

The sticky bit is a Unix-like system’s own access rights flag that can be given to files and directories.

There are two different definitions: one for files and the other for directories.

Superusers could mark files, particularly executables, as being kept in main memory even after their usage has ended to avoid the swapping that would occur if another requirement occurred.

The file had to be reloaded from relatively slow secondary memory. Due to swapping optimization, this function has become useless.

When the sticky bit for a directory is set, the file system treats the files in that directory differently, allowing only the file’s owner, directory’s owner, or root user to rename or delete the file.

Without the sticky bit set, any user writing and executing access to the directory can rename or remove its contents regardless of the file’s owner.

This is typically put in the /tmp directory to prevent regular users from deleting or transferring files belonging to other users.

History of the Sticky Bit in Linux

In the Fifth Edition of Unix (in 1974), the sticky bit was introduced for usage with pure executable files. When set, it told the operating system to keep the program’s text segment in swap space after the operation finished.

This speeds up subsequent executions by allowing the kernel to move the application from swap to actual memory in a single operation.

As a result, commonly used programs, such as editors, would load substantially faster.

Changing the executable in stickied programs requires deleting the sticky bit from the executable, running the program and exiting to clear the cache, replacing the binary executable, and restoring the sticky bit.

Example of the Sticky Bit in Linux

Using some examples, we’ll go over how to set and unset sticky bits in this section.

Create a directory and grant all users read, write, and execute permissions to it:

$ mkdir thanos

As a result, a directory named thanos is created.

$ mkdir chmod 777 thanos/

All users are given read-write-execute access with the chmod command.

Create multiple files with different users in this directory to read, write, and execute the files.

$ ls -ld thanos/

Output:

drwxrwxrwx 2 user user 4096 Feb  2 14:05 thanos/

Using the +t flag of the chmod command, enable the sticky bit on the directory.

$ chmod +t thanos/
$ ls -ld thanos/

Output:

drwxrwxrwt 2 user user 4096 Feb  2 14:05 thanos/

As can be seen, a permission bit t is added to the directory’s permission bits. Sticky bits function differently in every operating system.