How can I pass Root Password to a script

6

I have the root password in one of my scripts and I want to launch another script which requires root permission from this script. How can I use the root password which I have so that I do not have to ask the user the root password which launching 2nd script.

Ashish

Posted 2009-08-20T07:46:10.390

Reputation:

if this is a programming question shouldn't this be on stackoverflow? – rzlines – 2009-08-20T08:00:08.350

@rzlines,: Administrative bash scripts are not really 'programming', it may be a better fit for ServerFault, or UnixSE... but really it should be just fine where it is. – J. M. Becker – 2012-05-23T20:04:22.073

Answers

18

There's a lot of security implications in this, but let's get right to the best way to handle this.

Don't use the root password directly. Use sudo to run the scripts. Sudo is installed by default on Ubuntu and is available on almost all popular Linux distributions in the package repositories. Once sudo is installed, you'll want to edit /etc/sudoers.

su -
visudo
# add something like the following:
Cmnd_Alias SCRIPT=/path/to/script1
script_user ALL=NOPASSWD: SCRIPT

Thus script_user can run the first script as root through sudo, which would then launch the other script as root. For more information about the sudoers file, see the sudoers(5) man page on your system.

But do your scripts absolutely have to run as root? Most times this isn't required at all, but is done out of convenience.

jtimberman

Posted 2009-08-20T07:46:10.390

Reputation: 20 109

1Don't you mean script_user ALL=NOPASSWD: SCRIPT ? – user1686 – 2009-08-20T12:40:55.503

@grawity thanks, thats what I get for posting at 2am or whatever it was ;) – jtimberman – 2009-08-20T13:41:05.420

I'm needing something similar, can you write a solution to me, I didin't understand how can I perform this correctly. My question.

– GarouDan – 2012-06-11T11:41:51.557

3

Another option would be to set suid on the file to root. Example: chmod u+s script.sh as root so when a normal user executes the script it runs with the file permissions of the owner aka root.

Just for your own knowledge if you don't want to set sudo to run without a password you can pass the password to sudo through stndin like echo PASSWORD|sudo -S ./script.sh

However this can be a security concern as it will show the password in history or anyone watching like "ps". You can also look into Expect and Empty.

Scott W.

Posted 2009-08-20T07:46:10.390

Reputation: 31