1

I am trying to navigate from one server"USER1" to another using ssh as a user "USER2".

ssh -t $hostname "sudo su - USER2; whoami; pwd"

Post executing the above, I am able to successfully navigate to the server as USER2, but the trailing commands i.e "whoami and pwd" are not getting executed.However, while exiting the ssh session USER1 is rendered in the output followed by the present working directory.

I do not want to make any changes in the ssh known hosts file.

  • The command you wrote gives the behavior you got; su does not exit until you exit USER2's shell, at which point the next command(s) run (whoami and pwd). I don't see the "multiple sudo" commands from your post's title, though. What behavior do you want to see change? – Jeff Schaller Jul 8 '15 at 16:36
5

You're executing the command sudo su - USER2; whoami; pwd on the remote host. Let's decompose that:

  • Commands separated by a semicolon are executed in sequence. Thus the command sudo su - USER2 is executed first; then, when it finishes, whoami is executed, and finally pwd is executed.
  • The command sudo su - USER2 starts a login shell as user USER2. This shell reads commands from standard input until it's told to exit (e.g. with the exit builtin) or until it detects an end of file on standard input. Since standard input is connected to the terminal, it waits for you to type commands.

If you want to execute commands as USER2, you need to pass them as arguments to sudo. Running su isn't useful here, sudo already knows how to do this. You don't need the -t option to SSH unless you want to interact with the remote commands (this includes the case when you need to type a password for sudo).

ssh -t "$remotehost" 'sudo -i -u USER2 sh -c "whoami; pwd"'

If your sudo configuration only allows you to run programs as root, not as an arbitrary user (which doesn't change anything security-wise), then you'll need to stick to sudo su. In that case, pass the -c option to su to run the specified shell command.

ssh -t "$remotehost" 'sudo su -c "whoami; pwd" USER2'

If you need to read the user's .profile, do so explicitly.

ssh -t "$remotehost" 'sudo su -c ". ~/.profile; whoami; pwd" USER2'

Alternatively, you can pass the commands as standard input to an interactive shell. The plumbing is a little delicate since ssh's standard input must remain the terminal. An advantage of this approach with a here document is that quoting in the shell command remains relatively simple: escape ' as '\'' for the remote shell, no escaping is needed for the remote shell.

ssh -t "$remotehost" 'sudo su - USER2 <<\EOF
whoami
pwd
EOF'
  • After running the command which you have suggested got following error. sudo: no tty present and no askpass program specified I tried ssh -t "$remotehost" 'sudo -i -u USER2' and got the following error Sorry, user USER1 is not allowed to execute '/bin/ksh' as USER2 on remotehost. The restrictions are as follows I want to switch to USER2 first and then execute a set of commands on the remote host. – dnup1092 Jul 9 '15 at 6:48
  • 1
    @dnup1092 If you need to type a password for sudo, then you do need the -t option. As for the error you're getting from sudo, that seems like a strange sudo configuration where you're only allowed to run programs as root. Change the configuration from dnup0192 ALL=(root) ALL to dnup0192 ALL=(ALL:ALL) ALL, the security impact is exactly the same. – Gilles 'SO- stop being evil' Jul 9 '15 at 9:09
  • I don't have the privilege to change the configuration.Is there any way out – dnup1092 Jul 9 '15 at 9:36
  • @dnup1092 Then you'll need to keep using sudo su (see my edit). – Gilles 'SO- stop being evil' Jul 9 '15 at 9:42
  • @dnup1092 If you have the ability to use sudo to switch to root, then you have the ability to change the configuration. – Jenny D Jul 9 '15 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.