【问题标题】:VSCode: how to ssh remote connect to remote WSL2VSCode:如何通过 ssh 远程连接到远程 WSL2
【发布时间】:2020-08-24 15:07:28
【问题描述】:

在 VSCode 中是否可以在 remote 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 确实 解决了我所有的问题。我不确定为什么它以前不起作用。有关详细信息,请参阅下面的答案。

【问题讨论】:

    标签: visual-studio-code


    【解决方案1】:

    回答我自己的问题 我确信我之前尝试过,但它没有奏效,但现在我再次尝试它,它有效。也许我只需要重新启动 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
    

    如果您希望基于authorized_keys 登录到这台电脑并且您的帐户是管理员帐户,您需要按照此处的特殊说明进行操作:https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement(我听说必须在某个时候手动修复 administrator_authorized_keys 文件权限。https://stackoverflow.com/a/64868357/600360)

    1. 使用 Remote-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,您可以在其中找到更多详细信息。

    【讨论】:

    • 我找到了那篇博文,用同样的方法解决了这个问题!它有效!
    • 关闭,但没有雪茄。如果您使用 ssh-t 强制 tty,Windows 将尽职尽责地为您启动 conhost.exe。例如,这会阻止您成功重新连接到任何正在运行的 tmux 进程。 (我经常使用ssh -t winmachine wsl tmux a 重新连接到我正在运行的会话。太好了!)在这种情况下,ssh -t winmachine tmux a 开始一个新的 tmux 会话:(
    • 这让我可以连接一次,但不幸的是,随后的连接给了我The file cannot be accessed by the system. Connection to [IP] closed. 还没有弄清楚为什么!
    【解决方案2】:

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

    首先,在远程Windows上禁用sshd,使其不占用端口22。然后,安装并启动sshd 内部 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
    

    那么您需要通过 ssh 或(最好generate a key pair 使用enable password logins 并将您的公钥放入/home/your_name/.ssh/authorized_keys 上的PCB

    假设您安装了适用于 Windows 的 OpenSSH(这似乎是 Remote 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 遵循 this SE answer 的说明。

    上面的内容可能看起来有点吓人,但实际上是非常标准的 SSH 设置过程

    【讨论】:

    • 谢谢。一点也不令人生畏,我非常精通linux。然而,令人生畏的是您需要做的所有其他想法,因为 WSL 在私有 172.x 范围内被分配了一个显然是随机的 IP。 hanselman.com/blog/… 。因此,您必须在 Windows 端进行一些端口转发才能使其正常工作,并且可能会在 WSL2 IP 地址发生更改的情况下编写脚本。
    【解决方案3】:

    简单的步骤

    1. 只需将您的 vscode 更新到最新版本
    2. 安装Remote Development extension pack
    3. 在设置中允许 WSL2 连接
    4. 如果我的回答对你有帮助的话:D

    【讨论】:

    • 这将允许 PC-B 连接到它自己的 WSL。我不相信它会允许 PC-A 访问 PC-B 上的 WSL。
    • 不,它工作得非常好,我以前每天都在外面时连接到我的家庭服务器
    • 您能否提供更多详细信息。在 PC-A(您在键盘上工作的地方)上,您是制作 SSH 还是 WSL 目标来访问 PC-B?
    • 我在 PC-B 上使用 SSH 服务器,从 PC-A 通过 VScode 连接并输入 PC-B 配置,然后弹出对话框从 PC-B 中选择一个文件夹作为项目
    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 2017-11-24
    • 1970-01-01
    相关资源
    最近更新 更多