Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

How to set sticky bit to .sh script in Linux for run program with owner permissions. For…

  1. User

    How to set sticky bit to .sh script in Linux for run program with owner permissions. For example if I created .sh script from root, non-root user can run it but it will execute with root permissions

  2. Assistant

    The "sticky bit" is actually different from what you're describing. The sticky bit is typically used to prevent users from deleting files they don't own in directories where they have write permissions (like /tmp).

    What you are looking for is the setuid permission, which allows users to run an executable with the permissions of the file's owner.

    However, there are important security implications to consider when setting the setuid bit on scripts. Modern Linux distributions often disable the setuid bit for scripts due to the security risks involved. Scripts are generally not considered as safe to run with elevated permissions as compiled binaries.

    That said, you can achieve similar functionality more securely using alternatives like sudo or pkexec.

    Using sudo

    1. Install sudo: First, make sure sudo is installed on your system.

      apt update && apt install sudo   # Debian/Ubuntu based systems
      yum install sudo                 # Red Hat/CentOS based systems
      
    2. Edit sudoers file: Use visudo to safely edit the sudoers file. Don't edit /etc/sudoers directly.

      visudo
      
    3. Add an entry to allow a user to execute the script as root without a password: Add the following line:

      username ALL=(ALL) NOPASSWD: /path/to/your/script.sh
      

      Replace username with the actual username and /path/to/your/script.sh with the absolute path to your script.

    4. Run the script using sudo: The non-root user can then run the script with:

      sudo /path/to/your/script.sh
      

    Using pkexec

    1. Install polkit utilities: This package might already be installed, but you can install it if not.

      apt update && apt install policykit-1   # Debian/Ubuntu based systems
      yum install polkit                      # Red Hat/CentOS based systems
      
    2. Create a policy file: Create a new file in /etc/polkit-1/localauthority/50-local.d, for example 10-my-script.policy.

      sudo nano /etc/polkit-1/localauthority/50-local.d/10-my-script.policy
      
    3. Add the following content:

      [Allow user to run my script as root]
      Identity=unix-user:username
      Action=org.freedesktop.policykit.exec
      ResultActive=yes
      

      Replace username with the actual username.

    4. Run the script using pkexec: The non-root user can then run the script with:

      pkexec /path/to/your/script.sh
      

    Summary

    Using sudo or pkexec is generally a safer and more manageable way to allow non-root users to execute scripts with elevated permissions compared to using the setuid bit on scripts.

  3. User

    Thank you. But I wanna to make script with setuid bit

  4. Assistant

    I understand that you want to set the setuid bit on a script to enable it to run with the permissions of its owner (in your case, root). However, it's important to note that setting the setuid bit on scripts is inherently insecure and is often not supported by modern Unix-like operating systems due to the risks involved.

    For educational purposes or controlled environments, if you still need to pursue this, here are some steps and considerations you should be aware of:

    Using a Wrapper Binary

    One common workaround is to use a small C program that acts as a wrapper around your script. The wrapper can have the setuid bit set and will execute the script.

    1. Create a C Wrapper Program: Write a small C program to execute your script.

      #include <stdlib.h>
      #include <unistd.h>
      
      int main() {
          // Define the script to be executed
          char *script = "/path/to/your/script.sh";
          
          // Use 'execl' to run the script
          execl(script, script, (char *) NULL);
          
          // If execl fails, exit with an error code
          perror("execl");
          exit(EXIT_FAILURE);
      }
      
    2. Compile the C Program:

      gcc -o mywrapper mywrapper.c
      
    3. Change Ownership and Set setuid Bit:

      • Change the owner of the compiled binary to root.
        sudo chown root:root mywrapper
        
      • Set the setuid bit on the binary.
        sudo chmod 4755 mywrapper
        
    4. Execute the Wrapper Binary:

      • Non-root users can now run the wrapper, which will execute the script with root permissions.
        ./mywrapper
        

    Detailed Steps

    1. Create the C Source File (mywrapper.c):

      #include <stdlib.h>
      #include <unistd.h>
      
      int main() {
          char *script = "/path/to/your/script.sh";
          execl(script, script, (char *) NULL);
          perror("execl");
          exit(EXIT_FAILURE);
      }
      
    2. Compile the Source Code:

      gcc -o mywrapper mywrapper.c
      
    3. Change Ownership to Root:

      sudo chown root:root mywrapper
      
    4. Set the setuid Bit:

      sudo chmod 4755 mywrapper
      
    5. Run the Wrapper: A non-root user can now execute the binary:

      ./mywrapper
      

    Warnings and Caveats

    • Security Risks: Setting the setuid bit on executables, especially scripts, can create significant security vulnerabilities. Ensure that both the wrapper and the script are secure from being altered by non-privileged users.
    • Audit and Logs: Regularly audit the usage of such scripts and binaries, and keep detailed logs to monitor any unauthorized attempts or misuse.
    • Environment Variables: Be cautious with environment variables, as they might be manipulated by the user to exploit the script or the system.

    Given these points, it is generally advisable to use sudo or pkexec for such needs. If you absolutely must use the setuid method, ensure that it is in a strictly controlled environment and only after thorough security assessments.