Operating System - HP-UX
1753753 Members
4834 Online
108799 Solutions
New Discussion

Re: How to run a command as root using sudo (indicating the password) from a script.

 
psalm139
Occasional Contributor

How to run a command as root using sudo (indicating the password) from a script.

Hi all,

let me tell you what i have:

 

HP-UX B.11

my userid is :  myuser1

this user has to run /usr/sbin/lpshut and  /usr/sbin/lpsched.

the user myuser1 has privileges to use "sudo su -" , when myuser1 has privileges to use  "sudo su -" the password is required (pass of myuser1 to use sudo, i have the password of myuser1, it is "secret") , this a rule for the company for using the sudo.

when myuser1 uses sudo su - , then i can run lpshut and lpsched.

 

i am bulding a script in  which has to shutdown (/usr/sbin/lpshut)  the scheduler and then start it (/usr/sbin/lpsched.)

 

Could you please let me know how i can to run the comands lpshut and lpsched using myuser1 knowing it has sudo privileges and i have to enter the password?

how can i code that in a script?

Thank you in advance.

 

 

3 REPLIES 3
Hiren N Dave
Valued Contributor

Re: How to run a command as root using sudo (indicating the password) from a script.

Hi,

 

> How to run a command as root using sudo (indicating the password)  from a script.

# cat script.sh

ssh myuser1@localhost sudo /usr/sbin/lpshut

ssh myuser1@localhost sudo /usr/sbin/lpsched

#---End of script

 

This will ask you to enter password of myuser1 & then as myuser1 is having password-less rights to access sudo, it will directly execute lpshut & lpsched commands without asking for root user passwords.

 

Hope this is what u wants....Cheers....

 

I am an HP Employee

Was this post useful? - You may click the KUDOS! star to say thank you.
Dennis Handly
Acclaimed Contributor

Re: How to run a command as root using sudo (indicating the password) from a script.

>the user myuser1 has privileges to use "sudo su -"

 

Then he can do any root command.  Unless you tell sudo that he can only do lpshut and lpsched.

 

>Could you please let me know how I can to run the commands lpshut and lpsched using myuser1

 

The user will have to type the password in for each of those commands.  Unless you can combine them together in one script.

 

James R. Ferguson
Acclaimed Contributor

Re: How to run a command as root using sudo (indicating the password) from a script.

Hi:

 

You should be able to do something like:

 

myaccount ALL=(ALL) NOPASSWD:/usr/sbin/lpshut NOPASSWD:/usr/sbin/lpsched

 

The documentation on the 'sudo' home is your best source of information:

 

xhttp://www.gratisoft.us/sudo/man/1.8.1/sudoers.man.html

 

The idea of using SUDO is to provide accountability and often to confer privilege in a granular way.  Defining users who can run *anything* as root when the user needs only a specific function is making swiss cheeze of security.

 

Regards!

 

...JRF...