microsoft / vscode-remote-release

Visual Studio Code Remote Development: Open any folder in WSL, in a Docker container, or on a remote machine using SSH and take advantage of VS Code's full feature set.
https://aka.ms/vscode-remote
Other
3.69k stars 299 forks source link

Remote-SSH: Fails to discover WSL SSH #937

Open UnicodeTreason opened 5 years ago

UnicodeTreason commented 5 years ago

Steps to Reproduce:

  1. Have Remote-SSH installed
  2. Connect to Host
  3. VSCode fails to find any SSH with error "An SSH installation couldn't be found"
  4. I checked the Remote - SSH Output and can't even see it attempting to look for WSL SSH Got error from ssh: spawn ssh ENOENT Testing ssh with C:\WINDOWS\System32\OpenSSH\ssh.exe -V Got error from ssh: spawn C:\WINDOWS\System32\OpenSSH\ssh.exe ENOENT Testing ssh with C:\Program Files\Git\usr\bin\ssh.exe -V Got error from ssh: spawn C:\Program Files\Git\usr\bin\ssh.exe ENOENT Testing ssh with C:\Program Files (x86)\Git\usr\bin\ssh.exe -V Got error from ssh: spawn C:\Program Files (x86)\Git\usr\bin\ssh.exe ENOENT Finding installed ssh failed: ssh installation not found ssh installation not found

Which is unexpected as the documentation here: https://code.visualstudio.com/docs/remote/troubleshooting#_installing-a-supported-ssh-client

Specifically states it'll use WSL SSH image

These closed issues also make it look as though it WAS added recently: https://github.com/microsoft/vscode-remote-release/issues/359 https://github.com/microsoft/vscode-remote-release/issues/448

Chuxel commented 5 years ago

@roblourens Did the update to dynamically find the SSH client on Windows include a WSL check or did that get cut/missed?

roblourens commented 5 years ago

Yeah I missed that entirely. Can you suggest how I should discover/invoke WSL ssh?

Also would have to figure out how config files will work. WSL ssh would not look at windows' ssh config files by default and vice versa.

davidwin commented 5 years ago

Enabling the WSL ssh would be awesome, since it should make it possible to take advantage of ControlMaster on Windows. We rely on WSL ssh heavily for a homegrown remote development solution, and connection multiplexing really helps to make things zippy.

Chuxel commented 5 years ago

@roblourens I've got a PR out to update docs in the mean time.

You should be able to detect WSL SSH with the following command line call:

where wsl && wsl which ssh

Non-zero exit means it's not there.

UnicodeTreason commented 5 years ago

Thanks for jumping onto this one guys, saves us having to run two SSH configurations.

diablodale commented 5 years ago

Additional test case as this is implemented...

I tried three alternate settings.json of

  "remote.SSH.path": "C:\\Windows\\System32\\wsl.exe ssh"
  "remote.SSH.path": "\\\\wsl$\\Ubuntu-18.04\\usr\\bin\\ssh"
  "remote.SSH.path": "/usr/bin/ssh"

They all failed with the Remote - SSH output log reporting for each their location...

Testing ssh with C:\Windows\System32\wsl.exe ssh -V
Got error from ssh: spawn C:\Windows\System32\wsl.exe ssh ENOENT
The specified path C:\Windows\System32\wsl.exe ssh is not a valid SSH binary

I have a very little tested workaround to force WSL ssh. Use a settings.json of

  "remote.SSH.path": "c:/njs/ssh.bat"

with an ssh.bat of

@echo off
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:c:=/mnt/c%
REM set v_params=%v_params:"=\"%
C:\Windows\system32\wsl.exe ssh %v_params%
diablodale commented 5 years ago

Hello. A regression in use of the setting remote.SSH.path to use WSL occurred in VSCode Windows release 1.37.0; where no problem existed in the previous 1.36.x. Putting it in this issue as it is highly related to this discussion.

The setting remote.SSH.path no longer accepts correctly a path like c:/njs/ssh.bat In 1.37 on Windows, the setting must be with backslashes.

Setup

Version: 1.37.0 (system setup) Commit: 036a6b1d3ac84e5ca96a17a44e63a87971f8fcc8 Date: 2019-08-08T02:33:50.993Z Electron: 4.2.7 Chrome: 69.0.3497.128 Node.js: 10.11.0 V8: 6.9.427.31-electron.0 OS: Windows_NT x64 10.0.18362

Repo

  1. Setup the ssh.bat file in a directory, and VSCode remote.SSH.path as in https://github.com/microsoft/vscode-remote-release/issues/937#issuecomment-514714615
  2. Run and connect using Remote SSH

Result

The connection fails. In the Remote SSH output window is the error

[10:35:33.341] Install and start server if needed
[10:35:33.404] > 
[10:35:33.405] Got some output, clearing connection timeout
[10:35:33.903] > ]0;C:\WINDOWS\SYSTEM32\cmd.exe
[10:35:33.912] >
...
[10:35:33.920] > 'c:' is not recognized as an internal or external command,
> operable program or batch file.

Expected

No error and a successful connection

Workaround

Change the setting of the path to use backslashes

"remote.SSH.path": "c:/njs/ssh.bat"    <--- fails
"remote.SSH.path": "C:\\njs\\ssh.bat"    <--- works
roblourens commented 5 years ago

Ugh windows. Forked that into https://github.com/microsoft/vscode-remote-release/issues/1148, thanks.

andrzejnovak commented 5 years ago

I can attest that using ssh through wsl would be amazing. Apart from the obvious non-duplication of configs, I need to connect to a host which only allows kerberos authentication.

Is there a documented workaround for now? I tried the ssh.bat file hack mentioned above, but got nowhere. There's an error stating The specified path is not a valid SSH binary

8549 commented 5 years ago

Setting remote.ssh.path to \\wsl$\Ubuntu-18.04\usr\bin\ssh doesn't work, either.

UnicodeTreason commented 5 years ago

Seems like this one's a doozy, everyone's efforts are appreciated.

sweepies commented 4 years ago

The script provided by @diablodale doesn't seem to work for me. When I go through the dialog to create a new remote SSH connection, it prompts me for the command and then the config file (of which I use a path to the WSL one) and then nothing happens. No new window opens up and no connection is added.

diablodale commented 4 years ago

@sweepyoface , contact me via email if you want my help diagnosing. It is a workaround, and I prefer not to muddy this issue w/ support convo on a workaround. :-)

andrzejnovak commented 4 years ago

@diablodale actually if you could that would be great, this clearly collected a bunch of ppl who'd like a fix but it doesn't work, presumably many more didn't bother to comment

diablodale commented 4 years ago

Install/Discussion for my BAT workaround is at https://gist.github.com/diablodale/54756043c395d712053cf0d50a86086a

andrzejnovak commented 4 years ago

Can confirm @diablodale 's shim works great. Presumably it something that could be added natively to the extensions?

cchan commented 4 years ago

Can also confirm @diablodale 's shim works great. Just wanted to additionally report that it doesn't play nice with Use Local Server, which might be a separate issue from this.

lancslingwood commented 4 years ago

Change the last line of @diablodale 's script to: C:\Windows\system32\bash.exe -ic "%~n0 %v_params%" Your ssh-agent will now work! This just loads your profile so the env vars are set.

cchan commented 4 years ago

That works great @lancslingwood!

I made a couple more changes to be able to use the same ssh config between vscode and WSL2, so here's the full instructions I've followed so far on Windows 10 v2004 if it's helpful to anyone:

All this feels like a massive Frankenstein but at least it's an invisibly scripted Frankenstein now :P

diablodale commented 4 years ago

If your specific setup needs to run .profile and/or .bashrc there are several combinations of bash features which cause those files to load but also creates side-affects. Your specific setup/needs can guide you on this. 👍 https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

-i indicates an interactive shell and additional code/features happen when bash is marked interactive. One example: the default .bashrc has code near # If not running interactively which prevents running most of that file. If you want the fastest execution of the shim's command with a more "pure" non-interactive session, it is usually not appropriate to indicate it is interactive. I use the shim for many things that need fast execution, e.g. pylint, so I do not use the -i approach.

Instead, I use the approach as written in the shim using wsl.exe and use the bash environment variable BASH_ENV=~/.bashrc. I set that variable as a Windows user environment variable and WSLENV=BASH_ENV so it is passed to the WSL subsystem. Now when I run something with wsl.exe it is non-interactive non-login yet runs ~/.bashrc. I discovered in some circumstances of truly interactive and/or login sessions, the .bashrc will then load twice. I stopped that by putting an if test at the top of .bashrc to catch those circumstances. ðŸĪŠ

sweepies commented 4 years ago
  • Make sure bash knows what to do with the config file path when vscode passes it with ssh -F:
sudo ln -s / //wsl$/Ubuntu

Heads up, I had to first sudo mkdir //wsl$ to get this to work.

monado3 commented 4 years ago

These solutions don't work for me.

Here're my settings and codes. Can anyone fix?

RyanPersson commented 4 years ago

Well, my setup WAS working with the ssh.BAT shim. However it stopped working a couple days ago & I'm getting a similar error to @monado3 .

Checking ssh with "C:\\scripts\\ssh.BAT -V"
ssh exited with code: 1
The specified path C:\\scripts\\ssh.BAT is not a valid SSH binary

Running C:\\scripts\\ssh.BAT -V in cmd returns OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f 31 Mar 2020 & I am able to use ssh.BAT to open ssh in wsl FROM cmd. So I know that the shim works. But remote-ssh's check if it's a valid binary is stopping it from even getting a chance to run...

UnicodeTreason commented 4 years ago

@roblourens Any progress on this to report? I assume WSL 2 might shake things up a little.

fxzxmic commented 4 years ago

Look below ↓↓↓ https://github.com/microsoft/vscode-remote-release/issues/937#issuecomment-755440954


I used my poor knowledge of C language to write a program, according to your own needs to compile it into ssh.exe. Hope there are other awesome people can help me optimize, I'm just a novice. Because this program just forwards ssh.exe's parameters to WSL or other bash environment. So, you can fully use the configuration used in WSL or other bash environments before. For example, the key file uses the internal location of WSL rather than the absolute location of key file in Windows environment.

#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
  int count;
  char ch[100] = "bash.exe -c \"ssh";
  for (count = 1; count < argc; ++count) {
    char ch0[100] = " ";
    strcat_s(ch0, sizeof(ch0), argv[count]);
    strcat_s(ch, sizeof(ch), ch0);
  }
  strcat_s(ch, sizeof(ch), "\"");
  system(ch);

  return 0;
}
RyanPersson commented 4 years ago

@fxzxmic's program is working for me!

Note that if you're compiling it on WSL/WSL 2, you need to use a windows compiler like mingw.

Steps I used to get it working: 1: Install a windows C/C++ compiler: sudo apt-get install mingw-w64

2: Save @fxzxmic's code as ssh.c

3: Compile into a windows exectutable: x86_64-w64-mingw32-g++ -o ssh.exe ssh.c

4: Set the path to ssh.exe in the remote-ssh settings: C:\\some\\path\\ssh.exe

cihatyildiz commented 4 years ago

I had the same issue, I don't know why this showed up. But it has been fixed when I restart the system :)

fxzxmic commented 3 years ago

I have a better program to do this. https://github.com/fxzxmic/CMD_to_Bash

UnicodeTreason commented 3 years ago

@roblourens Anything we as the community can do to help get this one out of the backlog/resolved?

zatkins2 commented 3 years ago

I'm also having some trouble here, hopefully it can help debug or move things along from the production side.

I've essentially followed the steps outlined in this very helpful comment, and I succeed on one machine running WSL 1, and fail on another machine running WSL 2. I can't see any other relevant difference than WSL version.

(My $0.02: this clearly seems to be a feature with demand from the community side. I'd vote for prioritizing its development so it may be available "out-of-the-box," rather than relying on workarounds which tend to break :) )

Key steps:

  1. Create a file ssh.BAT in Windows, with the following contents (at least):
    C:\Windows\system32\wsl.exe ssh %*
  2. Create the following soft link in wsl:
    sudo ln -s / //wsl$/Ubuntu
  3. In VS Code, add the following settings:
    "remote.SSH.path": "\\path\\to\\ssh.BAT",
    "remote.SSH.configFile": "//wsl$/Ubuntu/home/zatkins/.ssh/config"

If I do this, my "SSH targets" appropriately populate in VSCode (VSCode can read my ssh config), and, as others have noted, I can ssh using my ssh.BAT file from within CMD (in principle, everything works). On my WSL 1 machine, connecting to an SSH target works fine. On my WSL 2 machine, connecting seems to hang after the password prompt:

[14:34:48.580] Log Level: 2
[14:34:48.592] remote-ssh@0.65.4
[14:34:48.592] win32 x64
[14:34:48.593] SSH Resolver called for "ssh-remote+daq", attempt 1
[14:34:48.594] "remote.SSH.useLocalServer": false
[14:34:48.594] "remote.SSH.showLoginTerminal": false
[14:34:48.594] "remote.SSH.remotePlatform": {"daq":"linux"}
[14:34:48.594] "remote.SSH.path": D:\ssh.BAT
[14:34:48.594] "remote.SSH.configFile": //wsl$/Ubuntu/home/zatkins/.ssh/config
[14:34:48.594] "remote.SSH.useFlock": true
[14:34:48.595] "remote.SSH.lockfilesInTmp": false
[14:34:48.595] "remote.SSH.localServerDownload": auto
[14:34:48.595] "remote.SSH.remoteServerListenOnSocket": false
[14:34:48.595] "remote.SSH.showLoginTerminal": false
[14:34:48.595] "remote.SSH.defaultExtensions": []
[14:34:48.595] "remote.SSH.loglevel": 2
[14:34:48.595] SSH Resolver called for host: daq
[14:34:48.595] Setting up SSH remote "daq"
[14:34:48.620] Using commit id "054a9295330880ed74ceaedda236253b4f39a335" and quality "stable" for server
[14:34:48.623] Install and start server if needed
[14:34:48.648] Checking ssh with "D:\ssh.BAT -V"
[14:34:48.704] stdout> 
C:\Users\zatki\AppData\Local\Programs\Microsoft VS Code>C:\Windows\system32\wsl.exe ssh -V 

[14:34:48.752] > OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f  31 Mar 2020

[14:34:48.847] Using SSH config file "//wsl$/Ubuntu/home/zatkins/.ssh/config"
[14:34:48.847] Running script with connection command: "D:\ssh.BAT" -T -D 62299 -F "//wsl$/Ubuntu/home/zatkins/.ssh/config" daq bash
[14:34:48.851] Terminal shell path: C:\Windows\System32\cmd.exe
[14:34:49.070] > ]0;C:\Windows\System32\cmd.exe
[14:34:49.070] Got some output, clearing connection timeout
[14:34:49.091] > C:\Users\zatki>C:\Windows\system32\wsl.exe ssh -T -D 62299 -F "//wsl$/Ubuntu/hom
> e/zatkins/.ssh/config" daq bash
[14:34:49.506] > zatkins@xyz's password: 
[14:34:49.506] Showing password prompt
[14:34:53.603] Got password response
[14:34:53.604] "install" wrote data to terminal: "*************"
[14:34:56.245] > 
> C:\Users\zatki>C:\Windows\system32\wsl.exe ssh -T -D 62299 -F "//wsl$/Ubuntu/home/zatkins/.ssh/config" daq bash
> zatkins@xyz's password:
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
[14:34:56.246] Showing password prompt
[14:35:04.044] Password dialog canceled
[14:35:04.045] "install" terminal command canceled
[14:35:04.046] Resolver error: Error: Connecting was canceled
    at Function.Canceled (c:\Users\zatki\.vscode\extensions\ms-vscode-remote.remote-ssh-0.65.4\out\extension.js:1:64837)
    at c:\Users\zatki\.vscode\extensions\ms-vscode-remote.remote-ssh-0.65.4\out\extension.js:1:413357
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
[14:35:04.060] ------

As you can see, it prompted me twice for my password, before I stopped the connection attempt. No matter how many times I enter my password, whether it's right or wrong, nothing progresses past this point.

My primary motivation is to be able to use a persistent connection via "controlmasters" via a ProxyJump. However, it also makes sense to me that with a fully functional ssh in WSL, one should be able to use it in VSCode.

zatkins2 commented 3 years ago

@roblourens just following up, as @UnicodeTreason wrote above, please let us know what we can do to help move this along!

zatkins2 commented 3 years ago

Bumping this! :/

zatkins2 commented 3 years ago

Bumping this again :(

@roblourens Maybe this is a niche-ask since there doesn't seem to be much follow-up from the community, but it is very well-defined and I think well-motivated. What can we do to keep it on the docket?

JtMotoX commented 3 years ago

Improving upon the solutions by: @diablodale, @lancslingwood, and @cchan (thank you so much guys!)

With this improvement, there is no need to create a symbolic link. Just set the configurations below and you are done.

settings.json

    "remote.SSH.path": "C:\\wsl-helpers\\vsc-ssh.bat",
    "remote.SSH.configFile": "\\\\wsl$\\Ubuntu\\home\\yourusername\\.ssh\\config",

vsc-ssh.bat

@echo off
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:c:=/mnt/c%
set v_params=%v_params:"=\"%
for /f "delims=" %%a in ('powershell -Command "& {'%v_params%' -replace '\/\/wsl\$\/[^\/]*',''}"') do set "v_params=%%a"
C:\Windows\system32\wsl.exe ssh %v_params%

Note: If you want to use a non-default distro, you can edit the vsc-ssh.bat file with something like ... wsl.exe -d Ubuntu ...

CoolCold commented 3 years ago

@jtmotox

C:\Windows\system32\bash.exe -ic "ssh %v_params%"

Shouldn't be this part something like of wsl.exe -d Ubuntu ...bash -ic "ssh .." , otherwise it's unclear for me how specific distro is called?

JtMotoX commented 3 years ago

Shouldn't be this part something like of wsl.exe -d Ubuntu ...bash -ic "ssh .." , otherwise it's unclear for me how specific distro is called?

Good catch. I use my default distro for ssh connections so I didn't think of this. I have updated my comment to use wsl instead of bash so you can pass the -d flag now to specify which distro to use.

tarynblack commented 3 years ago

I was having trouble getting a passphrase prompt and was able to get there by following @JtMotoX solution above (except I used "remote.SSH.configFile": "/home/username/.ssh/config" as the "\\\\wsl$\..." version did not work for me). However, like @zatkins2, after entering my passphrase remote-SSH hangs (with both correct and incorrect passwords), although I do not get multiple prompts:

[13:05:07.995] Log Level: 2
[13:05:08.004] remote-ssh@0.65.8
[13:05:08.005] win32 x64
[13:05:08.007] SSH Resolver called for "ssh-remote+<server>", attempt 1
[13:05:08.008] "remote.SSH.useLocalServer": false
[13:05:08.009] "remote.SSH.showLoginTerminal": true
[13:05:08.009] "remote.SSH.remotePlatform": {}
[13:05:08.009] "remote.SSH.path": C:\Users\taryn\bin\vsc_ssh.bat
[13:05:08.009] "remote.SSH.configFile": /home/taryn/.ssh/config
[13:05:08.010] "remote.SSH.useFlock": true
[13:05:08.010] "remote.SSH.lockfilesInTmp": false
[13:05:08.010] "remote.SSH.localServerDownload": auto
[13:05:08.010] "remote.SSH.remoteServerListenOnSocket": false
[13:05:08.011] "remote.SSH.showLoginTerminal": true
[13:05:08.011] "remote.SSH.defaultExtensions": []
[13:05:08.011] "remote.SSH.loglevel": 2
[13:05:08.012] SSH Resolver called for host: <server>
[13:05:08.012] Setting up SSH remote "<server>"
[13:05:08.048] Using commit id "83bd43bc519d15e50c4272c6cf5c1479df196a4d" and quality "stable" for server
[13:05:08.055] Install and start server if needed
[13:05:08.683] Checking ssh with "C:\Users\taryn\bin\vsc_ssh.bat -V"
[13:05:09.108] > OpenSSH_8.2p1 Ubuntu-4ubuntu0.2, OpenSSL 1.1.1f  31 Mar 2020

[13:05:09.198] Using SSH config file "/home/taryn/.ssh/config"
[13:05:09.198] Running script with connection command: "C:\Users\taryn\bin\vsc_ssh.bat" -T -D 53841 -F "/home/taryn/.ssh/config" "<server>" bash
[13:05:09.205] Terminal shell path: C:\WINDOWS\System32\cmd.exe
[13:05:10.044] > 
> 
> 
> ]0;C:\WINDOWS\System32\cmd.exe
> 
> 
[13:05:10.045] Got some output, clearing connection timeout
[13:05:10.360] > 
> 
> 
[13:05:11.155] > Enter passphrase for key '/home/taryn/.ssh/id_rsa': 
[13:05:18.889] > ^Enter passphrase for key '/home/taryn/.ssh/id_rsa': ^CTerminate batch job (Y/N)?
> 
> 
[13:05:18.914] > ^C
[13:05:20.259] "install" terminal command done
[13:05:20.260] Install terminal quit with output: ^C

As far as I can tell (I know very little about how this stuff works), the problem is arising with Running script with connection command: "C:\Users\taryn\bin\vsc_ssh.bat" -T -D 53841 -F "/home/taryn/.ssh/config" "<server>" bash. If I run this in cmd, it hangs after passphrase -- but if I remove bash from the end, I can connect. I can also connect from bash. So I think I need to either remove bash from the end of that connection command, or change the terminal shell path to bash instead of cmd. I haven't been able to figure that out yet...

JtMotoX commented 3 years ago

Running script with connection command: "C:\Users\taryn\bin\vsc_ssh.bat" -T -D 53841 -F "/home/taryn/.ssh/config" "" bash. If I run this in cmd, it hangs after passphrase

@tarynblack It might not actually be hung but you might unknowingly be at a bash prompt. When it "hangs", can you try typing "whoami" (without the quotes) and hit enter and see if it returns with anything?

tarynblack commented 3 years ago

@JtMotoX yes, whoami returns my username for the server (in cmd).

In VS Code though, after I enter my passphrase it just continues to say "Opening Remote" in the status bar, and on the right there's a popup that says "Setting up SSH Host : Initializing VS Code Server"

JtMotoX commented 3 years ago

@tarynblack since it returns your username, then it is doing exactly what it should.

I think now you are running into a different issue which I have found others reporting here (https://github.com/microsoft/vscode-remote-release/issues/434) and here (https://github.com/microsoft/vscode-remote-release/issues/1202). Do you have a "RemoteCommand" section in your config? Are you able to paste the config section for your \<server> with any IP's or hostnames redacted?

tarynblack commented 3 years ago

Thanks @JtMotoX! I'll take a look at those issues. I do not have a "RemoteCommand" section in my config. My entire config is:

Host <server>
  HostName <server>
  User <username>
  IdentityFile ~/.ssh/id_rsa
JtMotoX commented 3 years ago

@tarynblack yeah that is a pretty simple config so I'm not sure what is going on. Are you using wsl (original) or wsl2?

On another note.

... (except I used "remote.SSH.configFile": "/home/username/.ssh/config" as the "\\\\wsl$\..." version did not work for me).

I didn't realize that using the underlying filepath would work for this setting but I tested it and it works. I have updated my original post with this since it is simpler. Thanks.

tarynblack commented 3 years ago

@JtMotoX I really appreciate you helping me troubleshoot this. I'm using WSL2 with Ubuntu-20.04.

It looks like #434 and #1202 both ended up having other issues, although I did try to set "remote.SSH.allowLocalServerDownload": true per #1202 with no luck.

nickolasclarke commented 3 years ago

I'm also seeing this issue @roblourens. Any idea as to when it would be resolved. I cannot use the remote-ssh extension with my configured keypairs under WSL.

zatkins2 commented 3 years ago

I have the same behavior as you actually, @tarynblack . Might be because of a version update since I first reported the problem.

datproto commented 3 years ago

settings.json

    "remote.SSH.path": "C:\\wsl-helpers\\vsc-ssh.bat",
    "remote.SSH.configFile": "\\\\wsl$\\Ubuntu\\home\\yourusername\\.ssh\\config",

... Note: If you want to use a non-default distro, you can edit the vsc-ssh.bat file with something like ... wsl.exe -d Ubuntu ...

Amazing job guys! I want to improve this a little bit, for anyone who are using non-default distro or had changed your wsl installation folder like me, you will need to change the last line of vsc-ssh.bat as shown below:

C:\Windows\system32\wsl.exe -d your-distro-name ssh %v_params% For example: C:\Windows\system32\wsl.exe -d Ubuntu-20.04 ssh %v_params%

And in the settings.json:

"remote.SSH.path": "C:\\wsl-helpers\\vsc-ssh.bat",
"remote.SSH.configFile": "\\\\wsl$\\home\\yourusername\\.ssh\\config",

Hope this will help you guys!

jtele2 commented 3 years ago

All- If I use \\\\wsl$\\Ubuntu\\home\\ubuntu\\.ssh\\config, I cannot open the config file in VS Code. But if I use \\wsl$\Ubuntu\home\ubuntu\.ssh\config, I can open the config file.

However, with the latter, I get the error when trying to connect: Can't open user config file \\wsl$\\Ubuntu\\home\\ubuntu\\.ssh\\config: No such file or directory

What's going on here?

JtMotoX commented 3 years ago

@jtele2 Since this is a setting in a json config file, you have to escape any backslashes, which means you have to add a backslash before any backslash. If you do not have the double backslashes in your settings.json config, then VSC will not parse it correctly. If you are trying to open the file directly in VSC, then yes you use the single slashes in your file path. If you are adding the file path to the settings.json file, then you need to use double slashes.

PathosV commented 3 years ago

I was able to get the options suggested by @JtMotoX working for me with WSL1, replicated below. @CoolCold comment also gave me the idea I needed to get X11 pass-through working as well and I wanted to point out that specific benefit for anyone else looking for that functionality.

json settings:

    "remote.SSH.path": "C:\\Users\\username\\Documents\\ssh.bat",
    "remote.SSH.configFile": "\\\\wsl$\\Ubuntu\\home\\username\\.ssh\\config",

bat file:

@echo off
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:c:=/mnt/c%
set v_params=%v_params:"=\"%
for /f "delims=" %%a in ('powershell -Command "& {'%v_params%' -replace '\/\/wsl\$\/[^\/]*',''}"') do set "v_params=%%a"
C:\Windows\system32\wsl.exe -e bash -ic "ssh -Y %v_params%"

The final bat command will launch ssh with your bash profile environment settings, so if you've configured your display variable in WSL that will be forwarded to the remote connection. Note that I had to use the -Y flag, the x11 forwarding flag in the vscode remote module did not affect this. Thanks for the help all.

rvveber commented 2 years ago

Also facing this issue, i dont want to have two directories where my keys are, since WSL is far easier to use for actual development, i'm only using WSL to develop and managing SSH Keys.
Keeps everything organized and seperated, preventing cluttering windows with for example nodejs.

But VS Code is still a windows executable and i'd like to connect to my configured wsl ssh hosts through my wsl ssh private keys, please 👍ðŸŧ