wsl2 ssh_如何从外部计算机在Windows 10上SSH进入WSL2

wsl2 ssh_如何从外部计算机在Windows 10上SSH进入WSL2_第1张图片

wsl2 ssh

Cool blog post eh? Good title, right?

很酷的博客文章,是吗? 好标题吧?

不要在此岗位上做说明 (DO NOT DO THE INSTRUCTIONS IN THIS POST )

until you have read the FOLLOW UP THE EASY WAY how to SSH into Bash and WSL2 on Windows 10 from an external machine and made the right decision for YOU!

直到您阅读了简易的后续操作,如何从外部计算机上将SSH SSH到Windows 10上的Bash和WSL2,并为您做出了正确的决定!

OpenSSH has shipped in Windows for 5 years now, so that's cool. You can do lots of things!

OpenSSH在Windows中已经发行了5年了,这很酷。 您可以做很多事情!

  • How to use Windows 10's built-in OpenSSH to automatically SSH into a remote Linux machine

    如何使用Windows 10的内置OpenSSH自动SSH到远程Linux计算机

  • How to SSH into a Windows 10 Machine from Linux OR Windows OR anywhere

    如何从Linux或Windows或任何地方通过SSH进入Windows 10计算机

But often folks want to SSH not into their Windows 10 machine, but rather, into WSL2 running within/behind their Windows 10 machine. While WSL2 can forward ports from the inside out (for example, localhost:8000 within a WSL2 instance being made available from the local Windows 10 machine) if you want to build a path to a WSL2 port from completely outside a machine, you'll need to be a lot more explicit.

但往往人们想SSH进他们的Windows 10的机器,而是进入WSL2运行中的/他们的Windows 10机器后面。 如果WSL2可以从内向外转发端口(例如,本地Windows 10机器可以使用WSL2实例中的localhost:8000),但是如果您想从一台机器外部完全构建到WSL2端口的路径,需要更加明确。

在WSL中安装OpenSSH-Server (Install OpenSSH-Server in WSL)

First, install OpenSSH server inside your Linux Distro:

首先,在Linux Distro中安装OpenSSH服务器:

scott@IRONHEART:~$ sudo apt install openssh-server
[sudo] password for scott:
Reading package lists... Done
Building dependency tree
Reading state information... Done
openssh-server is already the newest version (1:7.6p1-4ubuntu0.3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

确定您的SSH端口号 (Decide on your SSH port number)

Next, in WSL2, edit /etc/ssh/sshd_config and uncomment out the Port line.

接下来,在WSL2中,编辑/ etc / ssh / sshd_config并取消注释Port行。

I edited it with sudo nano /etc/ssh/sshd_config, no shame!

我用sudo nano /etc/ssh/sshd_config对其进行了编辑,不要感到遗憾!

SSH is usually 22, but I like to use something like 2222 so it's less obvious but still easy to remember AND is different from your Window's machine's 22. Note that I told it to listen on 0.0.0.0, so, any adapter. You can also set PasswordAuthentication to "no" if you want to use SSH keys rather than passwords for authentication. Set it to "yes" if you know what you're doing and don't know how to use ssh keys.

SSH通常为22,但我喜欢使用2222之类的东西,因此它不太明显,但仍易于记住,并且与Window的计算机22不同。请注意,我告诉它在0.0.0.0上监听,因此可以在任何适配器上监听。 如果要使用SSH密钥而不是密码进行身份验证,也可以将PasswordAuthentication设置为“ no”。 如果您知道自己在做什么并且不知道如何使用ssh键,请将其设置为“是”。

/etc/ssh/sshd_config

...STUFF ABOVE THIS...
Port 2222
#AddressFamily any
ListenAddress 0.0.0.0
#ListenAddress ::

...STUFF BELOW THIS...

From within WSL2 get your IP address using "ifconfig." Mine is 172.23.129.80, yours will likely be 172.SOMETHINGELSE and it will change when WSL2 starts up cold.

在WSL2中,使用“ ifconfig”获取您的IP地址。 我的是172.23.129.80,您的可能是172.SOMETHINGELSE,并且当WSL2启动冷时它将改变

You may want to ensure it's running, considering WSL2 has no systemd.

考虑到WSL2没有systemd,您可能要确保它正在运行。

service ssh start

将端口转发到WSL2 (Forward Ports into WSL2)

Now, from an Administrator Windows prompt - that can be cmd.exe or powershell.exe, it doesn't matter, use the net shell "netsh" to add a portproxy rule. Again, change connectaddress to YOUR WSL2 ipaddress, which is an internal address to your machine.

现在,从Administrator Windows提示符下开始-可以是cmd.exe或powershell.exe,这无关紧要,请使用net shell“ netsh”添加portproxy规则。 再次,将connectaddress更改为您的WSL2 ipaddress,这是计算机的内部地址。

netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=2222 connectaddress=172.23.129.80 connectport=2222

打开防火墙 (Open the Firewall)

Next, from the same Administrator Windows prompt, open an incoming Firewall Port. You can do it from the Advanced Firewall Settings, but even easier you can use netsh again!

接下来,在相同的Administrator Windows提示符下,打开一个传入的防火墙端口。 您可以从“高级防火墙设置”中执行此操作,但更轻松的话,您可以再次使用netsh!

netsh advfirewall firewall add rule name=”Open Port 2222 for WSL2” dir=in action=allow protocol=TCP localport=2222

You can list all your portproxy rules like this if you're concerned:

如果您担心,可以列出所有这样的portproxy规则:

netsh interface portproxy show v4tov4

You can remove them all if you want with

您可以根据需要删除所有内容

netsh int portproxy reset all

脚本解决方案? (A scripted solution?)

GitHub user and community member Daehahn is working on a PowerShell Script to automate this process. The comment thread starts here and the gist for the PowerShell script for wsl2-network.ps1 is here. It resets firewall and portproxies, finds your default distro's new IP, and sets you up again. Save this .ps1 somewhere, read it, and run "unblock-file wsl2-network.ps1" on it so you can set up your system quickly for Shushing into your WSL2 instance!

GitHub用户和社区成员Daehahn正在使用PowerShell脚本来自动化该过程。 注释线程从此处开始,并且wsl2-network.ps1的PowerShell脚本的要点在此处。 它将重置防火墙和端口代理,找到默认发行版的新IP,然后再次进行设置。 将该.ps1保存在某个地方,阅读它,然后在其上运行“ unblock-file wsl2-network.ps1 ”,以便您可以快速设置系统以将其Shushing到WSL2实例中!

Note the $Ports variable that likely opens up more than you want or need, remembering that WSL and VS Code will automatically forward ports to localhost when needed for development.

注意$ Ports变量可能会打开超过您想要或需要的变量,请记住WSL和VS Code在开发需要时会自动将端口转发到localhost。

Hope this helps! It would be nice if WSL2 didn't change it's internal IP address every time it starts up so that this could be made even easier and more automated.

希望这可以帮助! 如果WSL2每次启动时都没有更改其内部IP地址,那将是一个很好的选择,这样它可以变得更加简单和自动化。

To conclude and sum up:

总结和总结:

  • This blog post - the one you are reading now, has Windows only forwarding ports, and uses WSL2's Linux OpenSSH and authenticates against Linux. Windows is only involved peripherally. The WSL2 IP address changes on reboot and you'll need to maintain your portproxy rules and firewall rules with the script listened at the end of that post.

    这篇博客文章-您正在阅读的文章,仅具有Windows转发端口,并使用WSL2Linux OpenSSH并针对Linux进行身份验证。 Windows仅在外围涉及。 WSL2 IP地址在重新启动时会更改,您需要使用该文章结尾处侦听的脚本来维护您的portproxy规则和防火墙规则。
  • This other blog post - over here - uses Windows' OpenSSH and authenticates with Windows and then runs WSL2. WSL2 starts up, uses bash, and Windows handles the TCP traffic.

    另一篇博客文章-在此处-使用Windows的OpenSSH并通过Windows进行身份验证,然后运行WSL2。 WSL2启动,使用bash,Windows处理TCP通信。

Understand what you want and use the right one for you.

了解您想要的东西,并为您使用合适的东西。

Other cool links:

其他酷链接:

  • Docker Desktop for WSL 2 integrates Windows 10 and Linux even closer

    适用于WSL 2的Docker桌面更紧密地集成了Windows 10和Linux

  • Remote Debugging a .NET Core Linux app in WSL2 from Visual Studio on Windows

    从Windows上的Visual Studio远程调试WSL2中的.NET Core Linux应用

  • Cool WSL (Windows Subsystem for Linux) tips and tricks you (or I) didn't know were possible

    您(或我)不知道的Cool WSL(Linux的Windows子系统)提示和技巧

  • Ruby on Rails on Windows is not just possible, it's fabulous using WSL2 and VS Code

    Windows上的Ruby on Rails不仅可行,而且使用WSL2和VS Code很棒

  • Easily move WSL distributions between Windows 10 machines with import and export!

    通过导入和导出轻松在Windows 10计算机之间移动WSL发行版!

  • What's the difference between a console, a terminal, and a shell?

    控制台,终端和外壳之间有什么区别?

Hope this helps! Also, please do subscribe to my YouTube channel!

希望这可以帮助! 另外,请订阅我的YouTube频道!

Sponsor: Never miss a bug — send alerts to Slack or email, and find problems before your customers do, using Seq 2020.1.

赞助者:永远不会错过一个错误-使用Seq 2020.1将警报发送到Slack或通过电子邮件发送,并在客户之前发现问题。

翻译自: https://www.hanselman.com/blog/how-to-ssh-into-wsl2-on-windows-10-from-an-external-machine

wsl2 ssh

你可能感兴趣的:(linux,java,python,ubuntu,面试)