# How to Use SUID, SGID, and Sticky Bits on Linux

**URL:** https://www.compsmag.com/blogs/how-to-use-suid-sgid-and-sticky-bits-on-linux/
**Author:** Ayushi Chauhan
**Published:** 2022-04-06
**Updated:** 2022-04-06
**Categories:** Blogs
**Tags:** Blogs Daily, Computer Guides, guiderobert, Guides and Tutorials, guiding12, How To Guide, How To Guides, Linux Guides, Tips, Tricks
**Reading Time:** 4 min

---

This tip is about the how to Use SUID, SGID, and Sticky Bits on Linux. So read this free guide, How to Use SUID, SGID, and Sticky Bits on Linux step by step. If you have query related to same article you may contact us.

## How to Use SUID, SGID, and Sticky Bits on Linux - Guide

The sticky bit is used to indicate special permissions for files and directories.  When a sticky bit enabled directory restricts the deletion of the file it contains.  It can be removed by root, the file's owner, or whoever needs write access to it.  This is useful for public directories like /tmp.  Like the SUID, the process has the same group rights as the file being executed.  If the SGID bit is set to any directory, all subdirectories and files created within it will have the same group ownership as the root directory, no matter who creates them.  Let's start with Sticky Bit first.  Because that's the easiest to explain.  Setting the sticky bit tells Unix that once the application in question has run, it must remain in memory.

Remember that Unix is ​​a multi-user operating system and was primarily designed to allow multiple users to work at the same time.  Thus, the logic used is that when a new user requests the same program, an existing program in memory takes less time to start.  So if a user just used a program and a new user wants to use the same program, the second user doesn't need to put up with a time delay for the program to start.  It would be readily available to him.  The sticky bit concept was very useful long before fast disk access and other storage access technologies existed.

## Defined User ID (SUID)

Have you ever thought about how a non-root user can change their own password when they don't have write permission to the /etc/shadow file.  hmmm… interesting isn't it?  Well, to understand the trick check the permission of the /usr/bin/passwd command:

# ls -lrt /usr/bin/passwd

-r-sr-sr-x 1 root sys 31396 Jan 20, 2014 /usr/bin/passwd

If you check carefully, you will find the 2 S's in the permission field.  The first s represents the SUID and the second represents the SGID.

When a command or script with a SUID bitset is executed, its effective UID becomes that of the owner of the file, rather than the user executing it.

Another good example of SUID is the su command:

# ls -l /bin/su

-rwsr-xr-xx 1 root user 16384 Jan 12, 2014 /bin/su

## How to    set SUID in a file?

#chmod 4555 [path_to_file]

## Set group identification (SGID)

### SGID permission on executable file

SGID permission is similar to SUID permission, the only difference is – when the SGID-enabled script or command is executed, it is executed as if it were a member of the same group that the file is a member of.

# ls -l /usr/bin/write

-r-xr-sr-x 1 root tty 11484 Jan 15 5:55 PM /usr/bin/write

The setgid permission is displayed as an “s” in the group execution field.

## How to    set GUID in a file?

#chmod 2555 [path_to_file]

## SGID in a directory

When SGID permission is set on a directory, files created in the directory belong to the group the directory is a member of.

For example, if a user with write permission to the directory creates a file there, that file will be a member of the same group as the directory and not the user's group.

This is very useful when creating shared directories.

## How to    set SGID in a directory

#chmod g+s [path_to_directory]

## sticky bit

The sticky bit is mostly used in shared directories.

It is useful for shared directories like /var/tmp and /tmp because users can create files, read and execute files owned by other users, but are not allowed to remove files owned by other users.

For example, if user bob creates a file called /tmp/bob, another user tom cannot delete this file even when the directory /tmp has permission of 777. If the sticky bit is not set, tom can delete /tmp/ bob because the /tmp/bob file inherits permissions from the parent directory.

root user (Of course!) and file owner can remove their own files.

## Sticky bit example:

# ls -ld /var/tmp

drwxrwxrwt 2 sys sys 512 Jan 26 11:02 /var/tmp

– T refers to when execute permissions are disabled.

– t refers to when execute permissions are enabled.

## How to    set sticky bit permission?

#chmod +t [path_to_directory]or

#chmod 1777 [path_to_directory]

## Final note

I hope you like the guide How to Use SUID, SGID, and Sticky Bits on Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.

---

*End of Article*