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.

Advertisement

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.

Share.

9 Comments

  1. I have a question : if a user can change his password with passwd command on which suid is set, means he get the root permission to make changes in /etc/passwdord and /etc/shadow file then how that user can not change the password of other users which permission prevents users to do that..

  2. Ganesh Bagde on

    Hi,

    Thanks for shared article with us.. It is really good, I cleared my confussion when I was read your document.

  3. I would like to add a more point on SB, SUID and SGIDd which is important to keep it in mind and for reference.

    small “s” – symbolically says the file has no execute persmission
    capital “S’ – has sticky bit , suid or sgid enables with execute permission

    for example:
    rwSrwxrwx — has no execute permission for the owner/normal user who runs a script if applicable

    where as

    rwsrwxrwx — is sound meaningful.

  4. Sir,

    Thank you for nice post,
    But I have question, I try to create a script under /usr/bin/ directory after that i created a file uder /etc dir and give the . And give the permision chmod u+s /etc/filename, now i try to run as a normal user , it is running but not able to edit this file /etc/filename ???
    where as you said after passwd command , there will some changes in /etc/shaddow and /etc/passwd file ..

    Please help me

Leave A Reply

Exit mobile version