Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Tutorials»What is Sticky Bit, SUID and SGID in Linux

    What is Sticky Bit, SUID and SGID in Linux

    RahulBy RahulMarch 14, 20133 Mins ReadUpdated:April 26, 2022

    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.

    SGID Sticky Bit SUID SUID and SGID in Linux
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleAll about Linux shell
    Next Article Installing HTTPD Server on CentOS 6

    Related Posts

    What is the /etc/aliases file

    2 Mins Read

    What is the /etc/nsswitch.conf file in Linux

    2 Mins Read

    How to Setup Squid Proxy Server on Ubuntu and Debian

    Updated:June 17, 20225 Mins Read

    How to Delete a Let’s Encrypt Certificate using Certbot

    Updated:June 3, 20222 Mins Read

    How to Install Latest Git on Ubuntu 22.04

    Updated:May 31, 20222 Mins Read

    How To Install LibreOffice on Ubuntu 22.04

    Updated:May 23, 20222 Mins Read

    9 Comments

    1. ankit on February 8, 2021 11:13 am

      Why ‘SUID’ need if other user has execute permission already

      Reply
    2. sanjeev on July 10, 2019 3:29 pm

      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..

      Reply
    3. Ganesh Bagde on April 16, 2015 6:32 am

      Hi,

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

      Reply
    4. Rathin on October 26, 2013 3:23 am

      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.

      Reply
    5. Sahil on September 8, 2013 1:39 pm

      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

      Reply
      • Rahul on September 9, 2013 2:52 am

        Hi Sahil,

        First thing you need to SUID to your script created under /usr/bin/. Can you send me the result of ls -l of both files ?

        Reply
        • Sahil on September 9, 2013 3:14 am

          Hi Rahul Sir,

          [[email protected] ~]# ls -l /usr/bin/mytest
          -rwsr-xr-x 1 root root 52 Sep 9 08:41 /usr/bin/mytest
          [[email protected] ~]# ls -l /etc/myfile
          -rw-r–r– 1 root root 24 Sep 9 08:41 /etc/myfile

          Reply
          • iceman94 on January 27, 2014 4:27 am

            Hi.
            In some patterm, SUID not work for shell script
            http://www.faqs.org/faqs/unix-faq/faq/part4/section-7.html

            Reply
    6. QuickBooks Hosting on September 2, 2013 3:46 am

      Great explanation it really saved my time, keep doing the nice work.

      Thank you very much.
      David

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.