I have two servers: server1 and server2 I can connect to server2 only from server1 by a specific user. Currently, I connect like this:

  1. My computer ssh user@server1
  2. On server1: su - user2
  3. After this on server1 ssh user3@server2

How can I do use one command line from my computer?

  • Why don't you connect directly as user2 to server1?
    – Sven
    Feb 21 '19 at 12:20
  • I can't control ssh keys o this servers
    – tutunak
    Feb 21 '19 at 12:59

Use jump hosts:

 ssh -J host1 host2

Then in your ~/.ssh/config

Host host1
   hostname X.X.X.X
   user user1
Host host2
   hostname X.X.X.X
   user user2
  • This was cool. Never tried it before
    – Ingvar J
    Feb 21 '19 at 14:30
  • before connecting to host2 I need do su - user2 and next use ssh user3@host2 user1 -me user2 - user on host who can login like user3 on host2
    – tutunak
    Feb 21 '19 at 15:48
  • 1
    You should use ProxyJump in the configuration file and shorten the command line to ssh host2.
    – kasperd
    Feb 21 '19 at 22:33

as a start, try:

ssh user@server1 "su - user2 -c 'ssh user3@server2'"

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.