Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-14-2008, 07:22 PM   #1
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Rep: Reputation: 15
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)
Old 06-14-2008, 07:48 PM   #2
Senior Member
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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.
Old 06-14-2008, 08:00 PM   #3
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Using Sudo is your only solution.
Old 06-14-2008, 08:57 PM   #4
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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:
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).
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).
Old 06-14-2008, 11:29 PM   #5
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Original Poster
Rep: Reputation: 15
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Root user check in bash script doesn't work tawalker Programming 6 12-18-2011 04:06 AM
How can I have a script owned as root and run as root by a user: setuid? stickey bit? abefroman Linux - Newbie 9 04-19-2008 06:15 PM
How to run script of another user from root? sikandar Linux - Software 4 09-05-2007 04:42 AM
Getting bash script files to run as root user helptonewbie Programming 5 04-13-2007 10:09 PM
how do I make sure that the user that is going to run the script is a root user??? nikold01 Linux - General 3 09-10-2004 08:54 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:35 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration