Search code examples
vscode-debuggervscode-remote

VSCode: How to connect to Remote SSH Host, inside an SSH Host?


I have hardware available through my university, to which I connect to through ssh in the following way in the command line:

ssh username@login.university.edu
//prompts for password
ssh hardware1
//prompts for password

I can get a window to login.university.edu with VSCode Remote SSH Connect to Host, but then once there I use the terminal to SSH into hardware1, therefore I can't use the debugger on hardware1.

How could I get a window to hardware1? Or else how can I customize launch.json in order to run ssh hardware1 and input my password?


Solution

  • This one works for me with Flask debugging.

    On your laptop:

    • Create or open ~/.ssh/config and set your ssh connection (on windows: c:\Users\username\.ssh\config)
    Host university
        HostName login.university.edu
        User username
        IdentityFile ~/.ssh/university_key
    
    Host hardware1
        HostName hardware1
        User username
        IdentityFile ~/.ssh/hardware1_key
        ProxyJump university
    
    • Create university_key on your laptop
    cd ~/.ssh
    ssh-keygen -f university_key # enter - enter
    ssh-copy-id -i university_key.pub username@login.university.edu
    

    On your jump server

    • login to the jump server ssh username@login.university.edu
      Create key on login.university.edu
    cd ~/.ssh
    ssh-keygen -f hardware1_key # enter - enter
    ssh-copy-id -i hardware1_key.pub username@hardware1.university.edu # if this isn't copy the key try username@hardware1
    

    Two files created in the folder: hardware1_key and hardware1_key.pub Copy this two file to your local (laptop) ~/.ssh folder.

    Now you can login

    • to the jump with: ssh university
    • to the tunnel: ssh hardware1 -v

    If you are able to connect from CLI to your hardware1, open the VS Code.
    If not, your hardware1_key.pub is not copied to hardware1, try to fix this one: username@hardware1.university.edu <- this is maybe wrong

    On VS Code

    Check your ~/.ssh/config file

    Press CTRL+SHIT+P Choose Remote-SSH: Open SSH Configuration file... From the dropdown menu choose your config file: ~/.ssh/config Check if everything right (must be... if you can login from CLI)

    Connect to host

    • Press CTRL+SHIT+P
    • Choose Remote-SSH: Connect to Host...
    • From the dropdown menu choose university or hardware1
    • A new window opened. Click on the Open Folder button, choose a folder you want.
    • Click on Ok

    Now you can use even debug mode, but the interpreter must be installed on the host you connected.
    For example if you want debugging a Python code on hardware1,
    you need to install Python 3.7 or higher on hardware1...