Search code examples
sshsudoshsu

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


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 ?


Solution

  • Use $USER. That will give you the username you logged in as. Whoami returns the user you're currently operating as.