繁体   English   中英

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

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

是否可以在 VSCode 中编辑远程PC 的 WSL2 中的文件。 (这就像 Remote-WSL 和 Remote-SSH 的组合。)我可以通过 ssh 和 RDP 连接到那台远程 PC。

路径\\\\wsl$\\在我的远程 ssh 连接中似乎不可用。

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

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

更新将 PC-B 的 ssh 服务器外壳设置为bash.exe确实解决了我的所有问题。 我不确定为什么它以前不起作用。 有关更多详细信息,请参阅下面我的回答。

回答我自己的问题,我确信我以前试过这个,但没有奏效,但现在我再试一次,它就奏效了。 也许我只需要重新启动 Windows 和 WSL2。

  1. 在 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. 使用远程 SSH 将 VSCode 从 PC-A 连接到 PC-B(使用 Windows 凭据)并告诉 VSCode 它是一个linux服务器(因为您正在连接到 WSL2 中的 bash.exe)。

    • 如果您在代理服务器后面,请将代理添加到您的~/.wgetrc
  2. 瞧。


这些步骤取自THE EASY WAY how to SSH into Bash and WSL2 on Windows 10 from an external machine , you can find more details.

如果您想连接到远程 WSL,您可能应该将其设置为运行自己的sshd而不是依赖托管 Windows 来执行隧道。 如果我对您的问题陈述正确,那么 VS Code博客文章之一中似乎概述了流程。 在这里,我将提到我认为您需要达到目标状态的步骤。

首先,在远程 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

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

假设您安装了适用于 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

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

以上可能看起来有点令人生畏,但实际上是非常标准的 SSH 设置程序

简单的步骤

  1. 只需将您的 vscode 更新到最新版本
  2. 安装远程开发扩展包
  3. 在设置中允许 WSL2 连接
  4. 如果我的回答帮助你投票:D

暂无
暂无

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

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