In the Linux filesystem, all the files have 3 special permission used for different purposes. In this tutorial, we will discuss about Sticky bit, SUID, and SGID file permissions in the Linux file systems.

What is Sticky Bit?

The sticky bit is used to indicate special permissions for files and directories. If a directory with sticky bit enabled will restrict deletion of the file inside it.

Any file has the sticky bit set, can be removed by its owner, the root, or who has to write permission on it. This is useful for shared or publically accessible directories like /tmp.

How to set the sticky bit to a file in the Linux file system.

Method 1:

chmod +t file1.txt 

##View the file permissions 
ls -l file1.txt 
-rw-r--r-T 1 root root 0 Mar  8 02:06 file1.txt

Mothod 2:

chmod 1777 file1.txt

##View the file permissions 
ls -l file1.txt 
-rwxrwxrwt 1 root root 0 Mar  8 02:06 file1.txt

The above output shows that the sticky bit is set with character t or T in the permissions filed. The lowercase t represents that execute permission is also enable and uppercase T represent that execute permission are not set.

What is SUID (setuid)?

If SUID bit is set on a file and a user executed it. The process will have the same rights as the owner of the file being executed.

For example: passwd command have SUID bit enabled. When a normal user changes his password this script update a few system files like /etc/passwd and /etc/shadow which can’t be updated by non-root account. So that passwd command process always run with root user rights.

Here is the implementation of SUID on file under the Linux system.

Mehtod 1:

chmod u+s file2.txt 

##View the file permissions 
ls -l file2.txt 
-rwsr-xr-x 1 root root 0 Mar  8 02:06 file2.txt

Method 2:

chmod 4655 tecadmin.txt 

##View the file permissions  
ls -l tecadmin.txt 
-rwSr-xr-x 1 root root 0 Mar  8 02:06 tecadmin.txt

What is SGID (setgid)?

Same as SUID, The process will have the same group rights of the file being executed. If the SGID bit is set on any directory, all subdirectories and files created inside will get the same group ownership as the main directory, it doesn’t matter who is creating it.

How to set the SGID on a directory in the Linux system.

chmod g+s /test 

##View the file permissions 
ls -ld /test 
drwxrwsrwx 2 root root 4096 Mar  8 03:12 /test

Now switch to another user and create a file in the /test directory.

su - tecadmin 
cd /test/ 
touch file3.txt 

Next check the group ownership of the newly created file. It must be same as the /test directory group owner. 

ls -l file3.txt 

-rw-rw-r-- 1 tecadmin root 0 Mar  8 03:13 file3.txt

Yes, it's the same. The file3.txt is created with root group ownership.

Thanks for reading this article, I hope it will help you to understand the sticky bit, SUID, and SGID in Linux.