0

I would like to do the following in a one-liner (so I can alias it in my .bashrc)

0. before doing anything I am user1 on server0 
1. ssh to server1
2. impersonate user2 (**THIS HAS TO BE DONE BY `sudo -u user2 sudosh`**)
3. ssh from server1 to server2
4. cd dirOnServer2

so my prompt would be after

user@server0:/dirOnServer0) superSshCommand
user2@server2:/dirOnServer2)

Is this possible ?

5
  • ssh server1 "sudo -l user2 ssh server2"
    – netmonk
    Commented Jan 27, 2017 at 14:46
  • This is not working, you would need at least ssh -t -q and I cannot sudo I need tty (sudo -u user2 sudosh)
    – statquant
    Commented Jan 27, 2017 at 14:50
  • Why not ssh [email protected] 'ssh server2.example.com'?
    – DopeGhoti
    Commented Jan 27, 2017 at 15:54
  • @DopeGhoti impersonate user2 take a command...
    – statquant
    Commented Jan 27, 2017 at 16:24
  • @statquant now that you've edited your question to state that sudosh is mandatory, you cannot solve this problem because sudosh doesn't allow chaining of commands via the command line. Commented Jan 30, 2017 at 16:48

1 Answer 1

0

Most of this can be achieved in a single command, and therefore can be put into an alias:

ssh -t server1 sudo -u user2 ssh server2

However, changing directory is harder. If you can guarantee that user2 should always land in your target directory you could put the cd dirOnServer2 into your .profile (or .bash_profile) like this:

cd dirOnServer2

Be aware that the directory change will only happen if the .profile is sourced, and this only happens for an interactive shell.

2
  • Hello, as I wrote sudo -u user2 is not possible it has to be sudo -u user2 sudosh If that's what you meant, well this is not working, it appears that it does not do the last ssh
    – statquant
    Commented Jan 30, 2017 at 12:55
  • @statquant you cannot solve the problem if sudosh is involved because it doesn't allow chaining of commands via the command line. Commented Jan 30, 2017 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .