LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Security (https://www.linuxquestions.org/questions/linux-security-4/)
-   -   How to ssh as root to a workstation and then run commands as another user. (https://www.linuxquestions.org/questions/linux-security-4/how-to-ssh-as-root-to-a-workstation-and-then-run-commands-as-another-user-731922/)

martinhb 06-10-2009 08:22 AM

How to ssh as root to a workstation and then run commands as another user.
 
Senario is we have a system where root has authorised keys set up so that it can do a passwordless ssh to $WORKSTATION. I then need to run a script on $WORKSTATION as user "bob" and NOT as user "root". I do not want to set up user "bob" to be allowed passwordless ssh so any ideas how I can do this?

I have tried variations of (as user "root"):

ssh $WORKSTATION "su - bob; ./my_script"

Just hangs unfortunately.

Thanks

theYinYeti 06-10-2009 08:51 AM

ssh $WORKSTATION 'su - bob -c "$PWD/my_script"'

martinhb 06-10-2009 10:22 AM

ssh $WORKSTATION 'su - bob -c "$PWD/my_script"' appears to run the command on the local machine as it says it cannot find the script which is located on the $WORKSTATION.

Should the command be:

ssh $WORKSTATION 'su - bob -c "$WORKSTATION:/my_script"`

Thanks

anomie 06-10-2009 08:56 PM

No, the command should not be that. Try providing the fully qualified path (rather than a relative path) for your script.

example: /foo/bar/my_script

chrism01 06-10-2009 09:04 PM

I think the $PWD is getting read before the ssh cmd is issued.
You'll have to check the bash docs for order of interpolation.
Using $WORKSTATION won't change that.
If the script is in a constant place, just use the absolute value of the path, or set it as a variable (maybe an input var) in the call to the script that runs ssh.

jamescondron 06-10-2009 10:04 PM

Code:

ssh $WORKSTATION "su -c <command> bob"
Where command is the full path to the command.


All times are GMT -5. The time now is 11:41 PM.