8

Is it possible in VSCode to edit files within WSL2 of a remote PC. (This is like a combination of Remote-WSL and Remote-SSH.) I can connect to that remote PC via ssh and RDP.

The path \\wsl$\ does not seem to be available in my remote-ssh connection.

PCA - me, local, VSCode
^
|
ssh and/or RDP
|
V
PCB - remote, WSL2

(I currently have Windows OpenSSH set up on the remote PC, with the default CMD shell. I tried setting the shell to Bash but then the remote extension wouldn't install.)

Update Setting PC-B's ssh server shell to bash.exe does solve all my problems. I'm not sure why it didn't work before. See my answer below for more details.

Mort
  • 3,379
  • 1
  • 25
  • 40

4 Answers4

7

Answering my own question I was certain I'd tried this before and it had not worked, but now that I try it again it works. Perhaps I just needed a reboot of Windows and WSL2.

  1. Enable Windows SSH server on PC-B and set the shell to be bash.exe
# Powershell as Administrator
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Set-Service -Name sshd -StartupType 'Automatic'
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\WINDOWS\System32\bash.exe" -PropertyType String -Force

If you want authorized_keys-based login to this PC and your account is an Administrator account, you will need to follow the special instructions here: https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement (I have heard of having to manually fix the administrator_authorized_keys file permission at some point too. https://stackoverflow.com/a/64868357/600360 )

  1. Using Remote-SSH connect VSCode from PC-A to PC-B (using Windows credentials) and tell VSCode that it is a linux server (because you're connecting to bash.exe within WSL2).

    • If you're behind a proxy server, add the proxy to your ~/.wgetrc.
  2. Voila.


These steps taken from THE EASY WAY how to SSH into Bash and WSL2 on Windows 10 from an external machine where you can find more details.

Mort
  • 3,379
  • 1
  • 25
  • 40
  • 2
    I found that blog article and solved this problem with the same method! It works! – mattbell87 Nov 08 '20 at 02:39
  • Close, but no cigar. If you force a tty using _ssh_'s `-t`, windows will dutifully fire up `conhost.exe` for you. This stops you successfully re-connecting to any running _tmux_ processes for instance. (I regularly do `ssh -t winmachine wsl tmux a` to re-connect to my running session. Nice!) In this case, `ssh -t winmachine tmux a` starts a new tmux session :( – bobbogo Sep 09 '21 at 08:45
  • 1
    This allowed me to connect _once_, but unfortunately, subsequent connections give me `The file cannot be accessed by the system. Connection to [IP] closed.` Haven't gotten to the bottom of why yet! – jimjamslam Dec 31 '21 at 07:12
  • I'm getting the same issue. Did you ever solve it @jimjamslam ? – Iain Galloway May 14 '23 at 15:39
  • I didn't, sorry @IainGalloway! I moved to a mac pretty soon after this and never got to test it – jimjamslam May 16 '23 at 07:17
4

If you want to connect to remote WSL, you probably should set it up to run own sshd instead of relying on hosting Windows to do the tunneling. If I have got your problem statement right, the flow seems to be outlined in one of VS Code blog posts. Here I'll mention steps I think you will need to get to your target state.

Firstly, disable sshd on remote Windows so it does not occupy port 22. then, install and start sshd inside PCB:

# from PCB command prompt
# something like that, depending on your choice of distro
sudo apt remove openssh-server && sudo apt install openssh-server
# this would again depend on your chosen distro
sudo /etc/init.d/ssh start # after i do this - windows pops up a firewall prompt to allow me create a rule. you might need to add it manually

then you'd need to either enable password logins via ssh or (preferably) generate a key pair and put your public key into /home/your_name/.ssh/authorized_keys on PCB.

Assuming you have OpenSSH for Windows installed (this seems to be the client Remote SSH relies on), on your PCA follow something like so:

# something along these lines on your PCA
PS C:\WINDOWS\system32>ssh-keygen
# note location of .pub file and copy its contents into remote ./ssh/authorized_keys
# add generated private key to ssh-agent service
PS C:\WINDOWS\system32> Start-Service ssh-agent # if this fails - ensure service is installed and enabled
PS C:\WINDOWS\system32> ssh-add path\to\your\private_key # ensure you have dropped all permission except your own user

Watch out for permissions: ssh keys are considered secret, so neither client nor server would start unless you drop all permissions from your key material. On linux do chmod 600 .ssh/authorized_keys, and for windows ssh-agent follow instructions from this SE answer.

The above may seem a bit daunting but is in fact very standard SSH setup procedure

timur
  • 14,239
  • 2
  • 11
  • 32
  • Thanks. Not daunting at all, I'm quite linux-savvy. What is daunting, however, are all the other thinkgs you'll need to do because WSL gets assigned an *apparently random* IP in the private 172.x range. https://www.hanselman.com/blog/HowToSSHIntoWSL2OnWindows10FromAnExternalMachine.aspx . So you have to do some port forwarding in the windows side to make it work, and probably script it given that the WSL2 IP address changes. – Mort Sep 22 '20 at 17:58
0

I know it's been a while since the post, but here is a useful answer that uses Remote -- Tunnels extension. https://stackoverflow.com/a/75389647

It appears that, for now, you need to manually install the CLI in WSL in order for it to work. The downloads can be found here. Since you are on Windows, I would recommend the x64 CLI download. When you extract the tar.gz file, you will get a file named code. I would recommend moving it to your home directory for ease. To open the tunnel, run ./code tunnel (from wherever you put the code file) to force the new CLI. If you use code tunnel it will still use the Windows version.

Source: https://github.com/microsoft/vscode/issues/171196

  • see also https://stackoverflow.com/questions/74850508/visual-studio-code-remote-access-to-a-wsl2ubuntu-on-my-desktop-from-my-macbook – Max Reeder Mar 18 '23 at 21:18
-1

easy steps

  1. just update your vscode to the latest version
  2. install Remote Development extension pack
  3. allow WSL2 connection in the settings
  4. if my answer helped you upvoted :D
Mort
  • 3,379
  • 1
  • 25
  • 40
ElMobark
  • 492
  • 4
  • 10
  • This would allow PC-B to connect to its own WSL. I do not believe it would allow PC-A to access the WSL on PC-B. – Mort Sep 22 '20 at 17:34
  • no it's work really fine i used to connect to my home server when i'm outside every day – ElMobark Sep 22 '20 at 23:22
  • Can you give some more details. On PC-A (where you are working on your keyboard), do you make a SSH or a WSL target to get to PC-B? – Mort Sep 23 '20 at 12:47
  • i use SSH server on PC-B and from PC-A connect by VScode and enter PC-B config after that there is pop dialog to choose a folder from PC-B as project – ElMobark Sep 23 '20 at 14:35