Operating System - HP-UX
1833780 Members
2511 Online
110063 Solutions
New Discussion

su to user too in a shell script

 
Schneider_10
Occasional Contributor

su to user too in a shell script

Do anyone know a solution for following problem:
I start a shell script as a normal user. In this script I have to execut a command with root users rights. I tested this command to run from my script with a su to user root but this doesn't work!!
Thanks for any help in advance!!
6 REPLIES 6
harry d brown jr
Honored Contributor

Re: su to user too in a shell script

Can you post the command you were using and the error message?

live free or die
harry
Live Free or Die
Trond Haugen
Honored Contributor

Re: su to user too in a shell script

You may want to take a look at the sudo package.
http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/sudo-1.6.2b1/

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Jason VanDerMark
Trusted Contributor

Re: su to user too in a shell script

Sudo is definitely the way to go. With sudo installed and your sudoers file correctly modified(using visudo command) you can simply do the following in your scripts:

sudo su - infa -c "cd directory;command -arguments"

I hope that this helps.

Regards,
Jason V.
Tie two birds together, eventhough they have four wings, they cannot fly.
Sanjay_6
Honored Contributor

Re: su to user too in a shell script

Hi,

You cannot run a script as a user and do a su to root in the script. You do not have the valid permissions. download and configrue sudo which will allow you to run some restricted (root allowed) commands as a normal user.

You can download sude from this link,

http://www.courtesan.com/

Hope this helps.

Regds
MANOJ SRIVASTAVA
Honored Contributor

Re: su to user too in a shell script

Hi Schneider

You cannot su to root from a user ,in the script alternatively you can copy the command in the users directory and mke it executable by the user unless there are other files required.


Manoj Srivastava
Rusty Sapper
Frequent Advisor

Re: su to user too in a shell script

If this is a one time thing then you may want to set the SUID bit. this essentialy runs the the script as the UID of the owner. So if you change the ownership to root and set the SUID bit, no matter which user runs it, it will execute just like the root user ran it. you have to be careful with this because there are security issues here.

to set the SUID, use:

#chmod 4755 script.sh

HTH

-Rusty