13

I'd like to edit code on a remote machine. The VSCode SSH extension offers this feature, but I can only get it to work for a single hop. Here's what I've tried so far:

  • Click on 'Remote Explorer' in VSCode sidebar
  • Click + for 'Add Target'
  • Enter the multiple-hop ssh command: ssh -A userA@hostA ssh userB@hostB
  • Select ~/.ssh/config as the SSH config file to update
  • When I do this, only the information from the first SSH hop is entered into the config file:
Host hostA
  HostName hostA
  ForwardAgent yes
  User userA

How can I get VSCode to correctly establish a multiple-hop ssh connection?

Jacob Stern
  • 861
  • 2
  • 8
  • 14

2 Answers2

12

I believe the cleanest way to do this would be to add the following in the ~/.ssh/config

Host HostA
  HostName hostA
  User userA

Host HostB
  HostName hostB
  User userB
  ProxyJump HostA

Now you just need to type ssh HostB to reach the remote machine.

HostA will act as middle man between you and HostB.

Cydouzo
  • 237
  • 2
  • 8
  • 1
    This seems a better answer. Fyi host A is middle man here between you and server. Just putting it here as I was confused and spent 20 minutes figuring why it didn't work. – Mandar Sadye May 20 '21 at 15:37
  • Happy I could help. I improved the answer according to your suggestion. – Cydouzo May 22 '21 at 13:04
  • I get an error : posix_spawn: No such file or directory – Andreas Foteas Sep 18 '21 at 21:23
  • Can one make this work if they also have to go through an extra first hop which is a plain telnet connection? – matt May 10 '22 at 11:44
  • You should be able to make as many hops as you need. You can have a HostC define a ProxyJump through a HostB, and have HostB define a ProxyJump through HostA. – Cydouzo May 10 '22 at 16:08
  • Thank you so much ! This seems to be the simplest and most elegant solution – programmer Aug 18 '23 at 11:02
6

After reading this answer, I updated the ~/.ssh/config file as follows:

Host hostB
  HostName hostB
  User userB
  ProxyCommand ssh userA@hostA -W %h:%p

and I was able to connect.

Jacob Stern
  • 861
  • 2
  • 8
  • 14