简体   繁体   English

从 windows 10 SSH 到 wsl2 ubuntu

[英]SSH'ing from windows 10 into wsl2 ubuntu

I am fairly new to this business and I fail to understand how to SSH from my win10 machine into my installed wsl2 ubuntu 20.4我对这项业务相当陌生,我不明白如何将 SSH 从我的 win10 机器到我安装的 wsl2 ubuntu 20.4

Basically, I followed this tutorial , But I keep getting the following errors:基本上,我遵循了本教程,但我不断收到以下错误:

  • when I try to SSH using the public port (using curl ifconfig.me ) gives me the error "connection timed out"当我尝试使用公共端口 SSH (使用curl ifconfig.me )给我错误“连接超时”

  • when I try to SSH using the private port (using ip route get 1.2.3.4 | awk '{print $7}' ) it gives me the error "Permission denied"当我尝试使用私有端口 SSH 时(使用ip route get 1.2.3.4 | awk '{print $7}'给我错误)

at some point I got the error "sshd: no hostkeys available -- exiting" so I followed this fix but then I got the errors mentioned before.在某些时候,我收到错误“sshd:没有可用的主机密钥 - 正在退出”,所以我遵循了这个修复,但后来我得到了前面提到的错误。 Should I delete any from the /etc/ssh folder?我应该从/etc/ssh文件夹中删除任何内容吗?

The end-goal is ssh'ing through vs-code, but I guess once I could do it from powershell, it's the same from vs-code.最终目标是通过 vs-code 进行 ssh'ing,但我想一旦我可以从 powershell 做到这一点,它与 vs-code 相同。

It appears that you need to enter /etc/ssh/sshd_config (with sudo permissions) and change the following lines:看来您需要输入/etc/ssh/sshd_config (具有sudo权限)并更改以下行:

  • ChallengeResponseAuthentication yes
  • PasswordAuthentication yes

Since you seem to have fixed your issue with ssh, let me propose that your ultimate goal ("ssh into WSL from VSCode) might be better accomplished using Microsoft's "Remote Development" extension pack, which includes several extensions. While it sounds like you are considering using the "Remote - SSH" extension, you can also use the "Remote - WSL" extension directly.由于您似乎已经解决了 ssh 的问题,因此我建议您使用 Microsoft 的“远程开发”扩展包来更好地实现您的最终目标(“从 VSCode 进入 WSL),其中包括几个扩展。虽然听起来你是考虑使用“Remote - SSH”扩展,也可以直接使用“Remote - WSL”扩展。

After installing either the extension pack or the WSL extension directly, just open your WSL instance, cd to the directory with your code and then code.直接安装扩展包或 WSL 扩展后,只需打开您的 WSL 实例, cd到包含您的代码的目录,然后code. (including the period). (包括期间)。 This will open VSCode and install a shim into the WSL instance which will allow communication between the two.这将打开 VSCode 并将 shim 安装到 WSL 实例中,这将允许两者之间的通信。

See the docs from Microsoft for more detail.有关详细信息,请参阅Microsoft 的文档

Also, on the topic of your original question, you said that you edited sshd_config to permit password authentication (I don't think the ChallengeResponseAuthentication change was necessary).此外,关于您最初问题的主题,您说您编辑了 sshd_config 以允许密码身份验证(我不认为 ChallengeResponseAuthentication 更改是必要的)。 That's one way to go, but ultimately I'd recommend generating an SSH key pair, copying the private key to something like C:\Users\yourid\.ssh\id_rsa and using that instead of a password login.这是 go 的一种方法,但最终我建议生成一个 SSH 密钥对,将私钥复制到C:\Users\yourid\.ssh\id_rsa之类的东西,而不是使用密码。

And you mentioned in your original question that you were unable to access SSH on the public port.您在原始问题中提到您无法在公共端口上访问 SSH。 This is because WSL2 does not do NAT, so it also won't be accessible from a second computer without (a lot of) additional effort (manual port-forwarding from Windows to WSL, which will have to be reset on reboot since the WSL interface address will change).这是因为 WSL2 不执行 NAT,因此如果没有(大量)额外的工作(从 Windows 到 WSL 的手动端口转发,必须在重新启动时重置,因为 WSL,它也无法从第二台计算机访问)接口地址会改变)。

As you've discovered, the WSL interface address will work, but remember that it will change on each reboot of Windows (technically, I think, any time the WSL subsystem is shut down and restarted).正如您所发现的,WSL 接口地址将起作用,但请记住,它会在 Windows 每次重新启动时更改(从技术上讲,我认为,只要 WSL 子系统关闭并重新启动)。 IMHO, you're better off using 127.0.0.1 or localhost.恕我直言,您最好使用 127.0.0.1 或 localhost。

But really, my preferred method of accessing WSL remotely is to install OpenSSH on Windows 10, port 22. Then you can simply do something like ssh -t windowsusername@mycomputername.local wsl to get access to the WSL instance.但实际上,我远程访问 WSL 的首选方法是在 Windows 10 端口 22 上安装 OpenSSH。然后您可以简单地执行ssh -t windowsusername@mycomputername.local wsl的操作来访问 WSL 实例。 You can even do this when you have multiple WSL instances on your machine with ssh -t windowsusername@mycomputername.local wsl -d WSLInstanceName .当您的机器上有多个 WSL 实例时,您甚至可以使用ssh -t windowsusername@mycomputername.local wsl -d WSLInstanceName

If you use this technique, of course, and you still want to run an SSH server in a WSL instance, you'll need to use a different port.当然,如果您使用这种技术,并且仍想在 WSL 实例中运行 SSH 服务器,则需要使用不同的端口。 But I really think you should do this anyways when running SSH under WSL.但我真的认为在 WSL 下运行 SSH 时无论如何都应该这样做。 Otherwise, you are likely to spin up a second WSL instance at some point and run into port conflicts anyway.否则,您可能会在某个时候启动第二个 WSL 实例并无论如何都会遇到端口冲突。

The downside is that the Windows OpenSSH -> WSL hack won't allow you to run things like VSCode through SSH, but it does provide super-simple access to WSL through SSH, and works remotely (if you ever need that) as well.缺点是 Windows OpenSSH -> WSL hack 不允许您通过 SSH 运行 VSCode 之类的东西,但它确实提供了通过 Z765553E6C7AC85050AZ 对 WSL 的超级简单访问,并且可以远程工作(如果您需要),

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

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