How to set sticky bit to .sh script in Linux for run program with owner permissions. For…
- 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
- 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
setuidpermission, 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
setuidbit on scripts. Modern Linux distributions often disable thesetuidbit 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
sudoorpkexec.Using
sudo-
Install
sudo: First, make suresudois installed on your system.apt update && apt install sudo # Debian/Ubuntu based systems yum install sudo # Red Hat/CentOS based systems -
Edit sudoers file: Use
visudoto safely edit the sudoers file. Don't edit/etc/sudoersdirectly.visudo -
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.shReplace
usernamewith the actual username and/path/to/your/script.shwith the absolute path to your script. -
Run the script using sudo: The non-root user can then run the script with:
sudo /path/to/your/script.sh
Using
pkexec-
Install
polkitutilities: 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 -
Create a policy file: Create a new file in
/etc/polkit-1/localauthority/50-local.d, for example10-my-script.policy.sudo nano /etc/polkit-1/localauthority/50-local.d/10-my-script.policy -
Add the following content:
[Allow user to run my script as root] Identity=unix-user:username Action=org.freedesktop.policykit.exec ResultActive=yesReplace
usernamewith the actual username. -
Run the script using
pkexec: The non-root user can then run the script with:pkexec /path/to/your/script.sh
Summary
Using
sudoorpkexecis generally a safer and more manageable way to allow non-root users to execute scripts with elevated permissions compared to using thesetuidbit on scripts. -
- User
Thank you. But I wanna to make script with setuid bit
- Assistant
I understand that you want to set the
setuidbit 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 thesetuidbit 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
setuidbit set and will execute the script.-
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); } -
Compile the C Program:
gcc -o mywrapper mywrapper.c -
Change Ownership and Set
setuidBit:- Change the owner of the compiled binary to root.
sudo chown root:root mywrapper - Set the
setuidbit on the binary.sudo chmod 4755 mywrapper
- Change the owner of the compiled binary to root.
-
Execute the Wrapper Binary:
- Non-root users can now run the wrapper, which will execute the script with root permissions.
./mywrapper
- Non-root users can now run the wrapper, which will execute the script with root permissions.
Detailed Steps
-
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); } -
Compile the Source Code:
gcc -o mywrapper mywrapper.c -
Change Ownership to Root:
sudo chown root:root mywrapper -
Set the
setuidBit:sudo chmod 4755 mywrapper -
Run the Wrapper: A non-root user can now execute the binary:
./mywrapper
Warnings and Caveats
- Security Risks: Setting the
setuidbit 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
sudoorpkexecfor such needs. If you absolutely must use thesetuidmethod, ensure that it is in a strictly controlled environment and only after thorough security assessments. -