kmartinez5555
kmartinez5555

Reputation: 5

Commands will not pass to CLI after logging into new user with sudo su - user

Obligatory 'first post' tag. Issue: Commands will not pass to command line after entering password for a sudo su - userB

I am writing a script in bash that requires to be run as a specific user. Ideally we would like this script to be able to be run on our local workstations for ease of use. Here is the command I am running to test:

ssh -qt -p22 userA@hostname "whoami; sudo su - userB; whoami"

Expected:
userA
[sudo] password for userA:
userB

With this command I am able to get the prompt for sudo password but once it is entered I am presented with a normal terminal where I can manually run commands. Once I ctrl-D/exit it runs the second whoami as the userA and closes. I work in an extremely jailed environment so sudo su -c and similar "run-as-root" commands do not work and I cannot ssh directly to userB.

Is there any way to send the commands to userB by logging in with sudo su - userB?

Upvotes: 0

Views: 503

Answers (1)

Barmar
Barmar

Reputation: 780899

su creates a subshell that reads the commands from standard input by default. It executes whoami after that exits. You can use the -c option to pass a command to it.

ssh -qt -p22 userA@hostname "whoami; sudo su - userB -c 'whoami'"

You can also use the -u option to sudo instead of using su:

ssh -qt -p22 userA@hostname "whoami; sudo -u userB whoami"

Upvotes: 2

Related Questions