Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I switch to the user jenkins in the middle of a ssh script?

Tags:

sh

ssh

sudo

su

I run

ssh root@myhost "sh -x" < myremotecommands.sh

where myremotecommands.sh contains:

#!/bin/sh
sudo su
apt-get update
sudo su -l -p jenkins
whoami

however the command whoami returns 'root'. I need to be user jenkins to perform some installations.

How can I switch to the user jenkins in the middle of the script ?

like image 475
user77115 Avatar asked Aug 07 '11 22:08

user77115


1 Answers

You just have to use "su" command with "-s /bin/bash" argument. It´s needed because jenkins user was not supposed to be used interactively, so it doesn´t have the bash defined.

su jenkins -s /bin/bash

After this, the "whoami" command will report you as "jenkins" user.

like image 157
Fernando Vieira Avatar answered Sep 28 '22 15:09

Fernando Vieira