简体   繁体   中英

How to forward an ssh port in vscode without the "ssh window"

Background

I have a MySQL server running on a remote machine and am trying to debug a Django application running locally that connects to that remote database. Our team does this by forwarding the remote port 3306 to the local port 3308 using PuTTY . I found out recently that you can do this within VS Code as well using the Remote - SSH VS Code extension created by Microsoft.

After the SSH connection is configured, this is done by adding the port to the ports tab as shown below: 在此处输入图像描述

An alternative, described in the Remote - SSH docs , is to add the port to the ssh config file. This opens the port automatically every time an SSH connection is made:

在此处输入图像描述

The Problem

The trouble with both of these methods is it seems that the SSH connection cannot be made without the entire VS Code window turning into an "SSH Window" that is connected to the remote machine. Since I am working on a the Django application locally, this means I then need a second window open just to forward the port.

I would really like to be able forward a port in the same window that I am working on the local Django application. I would prefer doing it with the Remote - SSH extension, but am open to other methods. Bonus points if the tunnel is created automatically when I open the Django app repo in VS Code.

I tried searching for extensions that would do this and seeing if the Remote - SSH can connect without a dedicated window, but I couldn't find anything.

Tunnel in Same Window

You don't need the "Remote - SSH" extension to connect over ssh. The ssh config file contains everything you need to make the connection, including the port forward.

Host MyServer
  HostName host.address.com
  IdentityFile C:\Users\user\identityFile
  User ubuntu
  LocalForward 127.0.0.1:3308 127.0.0.1:3306

Here I have named the host MyServer this name can be whatever you like so long as the HostName contains the right address to the server. With this, you just need to type:

ssh MyServer

into your terminal and you've set up your port forwarding! You can run this alongside your python manage.py runsever to develop your Django application using split terminals in VS Code.

Connect on Open Repo

The second part to this is automatically opening the split terminals, one of which runs the ssh command. This is covered in this question: Save terminal tabs to saved workspace VSCode

I found the first answer to the linked question easiest and implemented the ssh solution in the workspace settings.json file as follows:

{
  "restoreTerminals.terminals": [
    {
      "splitTerminals": [
        {
          "name": "Remote Server",
          "icon": "vm-connect",
          "commands":["ssh MyServer"],
        },
        {
          "name": "Django",
          "icon": "terminal",
          "commands":["python manage.py runserver"],
        }
      ]
    }
  ],
}

You can open an extra terminal tab in the same VS Code window (in the background) or open another terminal application not linked to VS Code (eg iTerm).

Then you can run ssh ubuntu@hostaddress -L 3308:127.0.0.1:3306 to port forward the remote port 3306 to your local port 3308.

Example here too: https://linuxize.com/post/how-to-setup-ssh-tunneling/#remote-port-forwarding

Then you can run your django application in another terminal tab in the same Window of VS Code. Split terminals could work better.

Example: VS 代码中的示例

For the bonus point: I would say add a go.sh bash script in your root folder and add the two commands in there. And when you open your repo, you just launch: bash go.sh . Note. The first thing that you will need is to run the ssh command in detached mode and then fire the django runserver command. I think this is a different question from the initial ask and rather more complicated [https://unix.stackexchange.com/questions/30400/execute-remote-commands-completely-detaching-from-the-ssh-connection]

# go.sh

ssh ubuntu@hostaddress -L 3308:127.0.0.1:3306 & -- need extra detach logic
python manage.py runserver 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM