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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a slightly tricky sudo scenario 1

Status
Not open for further replies.

keak

Programmer
Sep 12, 2005
247
CA
I am writing a script that

1. ssh to a host
2. issue a sudo command in that hose to change from user1 to user2 (so sudo -H -u user2 bash)
3. execute a script as user 2 in bash

I have tried
Code:
scp script1.sh $host:~/
ssh $host <<END_SCRIPT
sudo -H -u user2 bash
./script1.sh
exit
END_SCRIPT

So in summary, I need this script 1 executed as user2.
I am trying to get this done in user2 in bash, instead of soemthing like
Code:
sudo -H -u user2 ./script1

is there any way I can accomplish this?
 
Personnally I would code it as
Code:
scp script1.sh $host:~/
ssh $host "sudo -H -u user2 script1.sh"
and then the sudoers entry would be
Code:
user1 = myhost (user2) = /my/path/to/script1

Columb Healy
 
The script1.sh was giving me a write permission error when running the command
Code:
sudo -H -u user2 script1.sh

but when I do
Code:
sudo -H -u user1 bash
./script1.sh
I don't have problems appending data to the file (the file is the SSH authorized_keys file). I just wanted to automate the process of adding a new key/pub_key pair between my machines.

 
If I were you I would try and figure out why, because that doesn't make sense. Have you tried:

[tt]ssh $host "sudo -H -u user2 bash ./script1.sh"[/tt]


Annihilannic.
 
Thanks for the reply.
Yeah, I figured it was something wrong with perhaps the way i am setting up my /etc/sudoers file.

However, I was wondering, if in general, we can sudo to another user's bash, and execute mutiple commands from there onwards (all from a shell script).

So basically something like

Code:
scp script1.sh $host:~/
ssh $host <<END_SCRIPT
sudo -H -u user2 bash
./script1.sh
./script2.sh
./script3.sh
.....
exit
END_SCRIPT

Or do we have to append a sudo -H -u user2 ./script*.sh
before every command?



 
And what about this ?
ssh $host "sudo -H -u user2 bash ./script1.sh;./script2.sh;./script3.sh"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
OOps, sorry, I meant this
ssh $host "sudo -H -u user2 bash -c './script1.sh;./script2.sh;./script3.sh'"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for that reply PHV !!
That was the type of shell command I was thinking about.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top