Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

su -l and password in script

Status
Not open for further replies.

ddrillich

Technical User
Jun 11, 2003
546
US
Good Day,

In my script I would like to run a command as another user.
How can I do it?

su -l userB -c "command args" logs me as userB but prompts me for the password.
How can I place the password in my script?

Thanks,
Dan
 
You could change the owners id to the one you want using chown and then execute a chmod 6755 (I think 6755 is correct; I don't have a Unix box on site to check) on the file to set the setuid bit on execution. Not a recomemended method for root owned shell scripts though. Bit of a security hole.

alternatively call su within the script. working from memory here so check the syntax.

This script simple runs if you are the root user or prompts for the password and then calls itself if you are not.

#!/bin/sh
# If I'm not root login and run script
# otherwise just run the script
if ( `uid` != 0 )
su - root -c ${0} # call myself
# The password prompt appears here
else
# run the commands
....
....
fi


good luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top