30

I am using WSL on Windows 10 to program, and recently discovered that VSCode can be used to edit programs through SSH.

However, I normally SSH into my university's computers through WSL so I can access the files on those systems and use their GPUs.

Is there any way to SSH into WSL using VSCode, and then SSH into my university's systems, and have access to their resources and filesystems from with within VSCode?

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Dhruva Gowda Storz
  • 411
  • 1
  • 4
  • 5
  • VSCode uses normal ssh, so if you can set it up to work from the command line, it should work in VSCode. I am unclear what you are trying to do. Are you trying to ssh into WSL from the host Windows or from another computer? Is there a 3rd computer involved? On which computer is VSCode running? – bitinerant Jun 13 '20 at 12:37
  • Can't you directly SSH from Windows to your University system instead of proxying through your WSL instance? – tHeSiD Jun 14 '20 at 23:22
  • 8
    I think the asker has WSL set up with all the SSH credentials they need, and would like VSCode to use the SSH from inside WSL instead of installing another SSH client. This would be my preference, too - I SSH and SCP into a vast number of clients, and having to set them up in multiple places is not ideal. SSH from WSL is my preference as it is effectively Linux, and offers a number of improved interface aspects for those familiar with Linux. – SEoF Oct 13 '20 at 14:07

9 Answers9

20

You can create ssh.bat file under windows with the content:

C:\Windows\system32\wsl.exe ssh %*

And then set VS Code setting "remote.SSH.path" to point to that bat file.
You’ll have the same ssh configuration and credentials in VS Code as you have in WSL.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Hazzard17
  • 633
  • 7
  • 14
  • If you get an error `/bin/sh: ssh: not found` in the Remote SSH output, the problem may be that your WSL installation has no default distribution configured. Fix e.g. via `wsl --setdefault ubuntu-20.04` in the Windows Terminal. – leopold.talirz Feb 08 '23 at 13:21
4

If writing C:\Windows\system32\wsl.exe ssh %* in an ssh.bat file mostly works but gives you issues where your ~/.bashrc is not sourced, replace the command with

C:\Windows\system32\wsl.exe bash -ic 'ssh %*'

This tweak runs the ssh command in an interactive bash shell, which sources your ~/.bashrc. Using the above fixed an issue I had with the Ubuntu ssh-agent not forwarding keys on VSCode remote terminal.

Julius Tao
  • 41
  • 2
3

To connect to a linux machine from WSL running in VScode, I did the following:

Host <your_host>
    HostName <your_host>
    User <your_username>
    IdentityFile C:\Users\<your_username>\.ssh\<your_private_key>

  • Copy <your_private_key> from WSL to windows. In WSL terminal, with default paths:
cp /home/<your_wsl_username>/.ssh/<your_private_key> /mnt/c/Users/<your_windows_username>/.ssh
  • In VScode, press F1 and select: Remote-SSH:connect to host and choose your distant server.
matleg
  • 618
  • 4
  • 11
1

If you can SSH into your university system from your WSL, I assume you can directly SSH from you Windows system. In that case, you can setup VS Code Remote SSH from Windows to directly access your university systems. Given that, I used a Git Bash terminal, as it supports all Linux commands right from your Windows OS.

Step 1: Setup your SSH access (password-less or not) to your university's systems. You can refer articles online, I assume you already know how to do that since you did it from within WSL.

Step2: Install the remote development package in VS Code extensions in local machine.

Step 3: Go to VS Code Settings and expand the Extensions drop down and select 'Remote - SSH'. On the settings page, set the "Remote - SSH: Config File" to the an ssh config file where you will store remote system details. E.g.: ~\vscode.ssh Also select the checkbox for ‘Always Reveal the SSH terminal’.

Step 4: Add the remote system details to the file ~\vscode.ssh like:

Host <hostname>
    User <ID for SSH login> # e.g. your university ID
    HostName <full qualified address for your remote system> # e.g. system.subnet.university.edu
    IdentityFile <your SSH pvt key> #e.g. ~/.ssh/id_rsa

Step 5: Select Remote Explorer on left navigation bar and "SSH Targets" from the dropdown. Your hostname should appear here. Right click and connect to Host.

Step 6: Carefully watch the logs of your first connection attempt from VSCode terminal logs if there are any failures to extract tar files.

By default, VS Code installs the vscode-server in your remote system's home directory, which may run out of space based on your university's space restrictions. You need to move your vscode-server directory to a work area where there's less space restrictions, usually a project area. Use below steps to do this, I created these for my organisation's use:

Once you start VSCode remote, the connection may fail or succeed based on the free space in your remote home directory as by default, VSCode setups your remote server in home directory, and this cannot be changed.

  1. If success, still move vscode-server to another large free space directory:

    a. Navigate to a desired project space directory from remote desktop terminal
    b. mv ~/.vscode-server /your/big/disk/project/space
    c. ln -s  /your/big/disk/project/space/.vscode-server ~/.vscode-server
    d. Use full absolute paths in previous command to avoid cyclic links. Confirm with below command, it shoud not return anything.
        $ find -L ./ -mindepth 15
    e. Reconnect from your VSCode again
    
  2. If failed, manually setup vscode-server on remote large free space directory:

    a. Get vscode-server commit ID on remote server using below command, which would be like 'e2d4cc38bb5da82wb67q86fd50f84h67bb340987'
        $ ls ~/.vscode-server/bin
    b. Download tarball replacing $COMMIT_ID with the the commit number from the previous step on local system: https://update.code.visualstudio.com/commit:$COMMIT_ID/server-linux-x64/stable
    c. Move tarball to remote server disk from local system:
        $ scp -P 22 vscode-server-linux-x64.tar.gz remoteID.remote.system.url.com:~/
    d. Move tarball to large free space directory as below:
        $ mkdir -p /your/big/disk/project/space/.vscode-server/bin/$COMMIT_ID/
        $ mv ~/vscode-server-linux-x64.tar.gz /your/big/disk/project/space/.vscode-server/bin/$COMMIT_ID/
    e. Extract tarball in this directory
        $ cd /your/big/disk/project/space/.vscode-server/bin/$COMMIT_ID
        $ tar -xvzf vscode-server-linux-x64.tar.gz --strip-components 1
    f. Create symlink of .vscode-server in your home directory
        $ ln -s  /your/big/disk/project/space/.vscode-server ~/.vscode-server
    g. Connect again
    
sgX
  • 696
  • 1
  • 8
  • 16
1

I found this post which worked fine for me to use VSCode to connect to remote host through ssh, using the ssh configuration in the wsl ubuntu in windows 10.

https://zitseng.com/archives/20325

As I am using a passphrase protected key, I had also to enable the ssh agent and add to it the key, to make it work.

update: Unfortunately, the ssh service for WSL does not start automatically, and I had to add another .bat file, as explained here https://superuser.com/a/1506722/239839

Besides that, I still need to open first a WSL console, use ssh-add mykey and then perform a first ssh connection to the host. If I don't do this, I keep being asked to type my password for the ssh key.

Best

0

If you're looking to set up an SSH Remote through WSL, I'm not sure that's possible yet. However, you can set up WSL Remotes (https://code.visualstudio.com/docs/remote/wsl-tutorial) and SSH Remotes (https://code.visualstudio.com/docs/remote/ssh-tutorial).

The downside here is your WSL uses it's own SSH command, separate to the SSH VSCode uses, and both use different configuration files by default. Whilst you can point VSCode to use the SSH configuration file used by WSL's SSH command, it is inadvisable due to probable different versions and the conflicts there of, plus the issues with editing files in the WSL storage from outside of WSL (I'll try to find a reference).

SEoF
  • 1,092
  • 14
  • 26
0

In a .bat file of your choice, write C:\Windows\system32\wsl.exe ssh %* In VSCode configurations, change remote.ssh.path to the path of the file created in step 1, such as c:\Users\goyuninfo\ssh.bat Find your WSL SSH config file, assuming ~/.ssh/config Get that file Windows path, assuming \wsl$\Ubuntu-20.04\home<username>.ssh\config Change remote.SSH.configFile to the path found in step 4. enter link description here

GoYun.Info
  • 1,356
  • 12
  • 12
  • I tried your solution, unfortunately I cannot leave my Remote.SSH.configFile empty, openSSH requires me to choose a config file. So I tried your second approach. But since the settings are in JSON, I have to escape my file path, which results that my wsl.exe cannot find the path. – TijnvdEijnde Aug 11 '21 at 18:03
0

The answer provided by Hazzard17 no longer worked for me. I think the problem was with VSCode and inputting the password. I fixed the problem using sshpass.

Installing:

sudo apt-get install sshpass

SSH.Remote.path file contents

C:\Windows\system32\wsl.exe sshpass -p <password> ssh %*

Note: I couldn't find a way to input the password through VSCode, so I hardcoded the password into the file (which means you have to change the password every time).

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Aditya Kendre
  • 136
  • 2
  • 9
0

After using Hazzard17's answer, I also had to activate the flag remote.SSH.lockfilesInTmp in VSCode -> Settings. That made it work for me.

Source: https://github.com/microsoft/vscode-remote-release/issues/885

Skrt
  • 85
  • 6