LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Granting root permissions to a bash script than can be run by a user? (https://www.linuxquestions.org/questions/linux-newbie-8/granting-root-permissions-to-a-bash-script-than-can-be-run-by-a-user-649327/)

SirTristan 06-14-2008 06:22 PM

Granting root permissions to a bash script than can be run by a user?
 
How might I be able to grant root permissions to a bash script (such that the bash script can for instance execute root commands in /usr/sbin), but give a user permissions to execute this bash script? So thus, the user can execute the bash script, and the bash script can execute a /usr/sbin command as if it was root.

(BTW I'm aware this can be very dangerous)

pinniped 06-14-2008 06:48 PM

If I'm not too senile, scripts with SUID don't behave the same as binaries - basically SUID is not allowed. You can try to have a binary execute a script - I have no idea whether the script will run as intended though, or if privileges will be dropped because you're not really root.

unSpawn 06-14-2008 07:00 PM

Using Sudo is your only solution.

matthewg42 06-14-2008 07:57 PM

suid works for scripts on some unix-likes OSes, but not Linux. This is for security reasons (see The Secure Programming for Linux and Unix HOWTO).

I believe sudo will do it. For example, if your script is:
Code:

/usr/local/sbin/myscipt
Add a line like this to your sudoers file (remember to use the visudo program to edit the sudoers file - do not edit it directly).
Code:

username ALL = NOPASSWD: /usr/local/sbin/myscript
(where username is the user name of the user who should be able to execute the script with root privileges.

By the way, you should be very careful about this. Scripts are easy to trick into doing the bidding of a malicious user (environment attacks for example).

SirTristan 06-14-2008 10:29 PM

Thank you, that's a good solution :)

To execute the commands as root, in my bash script I put 'sudo -u root /path/to/command' for the commands and executed the bash script normally, and it worked.


All times are GMT -5. The time now is 08:21 AM.