简体   繁体   English

VSCode:如何通过 ssh 远程连接到远程 WSL2

[英]VSCode: how to ssh remote connect to remote WSL2

Is it possible in VSCode to edit files within WSL2 of a remote PC.是否可以在 VSCode 中编辑远程PC 的 WSL2 中的文件。 (This is like a combination of Remote-WSL and Remote-SSH.) I can connect to that remote PC via ssh and RDP. (这就像 Remote-WSL 和 Remote-SSH 的组合。)我可以通过 ssh 和 RDP 连接到那台远程 PC。

The path \\\\wsl$\\ does not seem to be available in my remote-ssh connection.路径\\\\wsl$\\在我的远程 ssh 连接中似乎不可用。

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.) (我目前在远程 PC 上设置了 Windows OpenSSH,使用默认的 CMD shell。我尝试将 shell 设置为 Bash,但随后无法安装远程扩展。)

Update Setting PC-B's ssh server shell to bash.exe does solve all my problems.更新将 PC-B 的 ssh 服务器外壳设置为bash.exe确实解决了我的所有问题。 I'm not sure why it didn't work before.我不确定为什么它以前不起作用。 See my answer below for more details.有关更多详细信息,请参阅下面我的回答。

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.也许我只需要重新启动 Windows 和 WSL2。

  1. Enable Windows SSH server on PC-B and set the shell to be bash.exe在 PC-B 上启用 Windows SSH 服务器并将 shell 设置为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
  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).使用远程 SSH 将 VSCode 从 PC-A 连接到 PC-B(使用 Windows 凭据)并告诉 VSCode 它是一个linux服务器(因为您正在连接到 WSL2 中的 bash.exe)。

    • If you're behind a proxy server, add the proxy to your ~/.wgetrc .如果您在代理服务器后面,请将代理添加到您的~/.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.这些步骤取自THE EASY WAY how to SSH into Bash and WSL2 on Windows 10 from an external machine , you can find more details.

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.如果您想连接到远程 WSL,您可能应该将其设置为运行自己的sshd而不是依赖托管 Windows 来执行隧道。 If I have got your problem statement right, the flow seems to be outlined in one of VS Code blog posts .如果我对您的问题陈述正确,那么 VS Code博客文章之一中似乎概述了流程。 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 :首先,在远程 Windows 上禁用sshd ,使其不占用端口 22。然后,PCB安装并启动sshd

# 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 .那么您需要通过 ssh 启用密码登录,或者(最好生成一个密钥对并将您的公钥放入PCB /home/your_name/.ssh/authorized_keys

Assuming you have OpenSSH for Windows installed (this seems to be the client Remote SSH relies on), on your PCA follow something like so:假设您安装了适用于 Windows 的 OpenSSH(这似乎是远程 SSH 所依赖的客户端),在您的PCA如下操作:

# 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.注意权限:ssh 密钥被视为机密,因此除非您从密钥材料中删除所有权限,否则客户端和服务器都不会启动。 On linux do chmod 600 .ssh/authorized_keys , and for windows ssh-agent follow instructions from this SE answer .在 linux 上执行chmod 600 .ssh/authorized_keys ,对于 windows ssh-agent遵循这个 SE answer 的说明

The above may seem a bit daunting but is in fact very standard SSH setup procedure以上可能看起来有点令人生畏,但实际上是非常标准的 SSH 设置程序

easy steps简单的步骤

  1. just update your vscode to the latest version只需将您的 vscode 更新到最新版本
  2. install Remote Development extension pack安装远程开发扩展包
  3. allow WSL2 connection in the settings在设置中允许 WSL2 连接
  4. if my answer helped you upvoted :D如果我的回答帮助你投票:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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