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

wsl2 ssh

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年了,这很酷。 您可以做很多事情!

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:

其他酷链接:

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

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows Subsystem for Linux 2(WSL2)是Windows 10操作系统中的一个功能,它允许用户在Windows中运行Linux操作系统。对于那些熟悉Linux的开发人员来说,WSL2是一种非常方便的方式来使用他们所熟悉的命令行界面和工具。 在WSL2中,用户可以使用多种方法进行远程访问。其中,其中之一是通过ssh远程登录。 SSH(Secure Shell)是一种安全的网络协议,允许用户在不安全的网络上进行加密通信。通过ssh远程登录WSL2,用户可以像在本地一样使用WSL2,同时还可以利用Windows的GUI界面。 要使用ssh远程登录WSL2,用户需要先启用ssh服务器。在Windows PowerShell或命令提示符中,可以使用以下命令来启动ssh服务器: ``` sudo apt update sudo apt install openssh-server sudo service ssh start ``` 安装和启动过程完成后,可以使用ssh客户端连接WSL2。在本地电脑上,打开一个终端窗口并输入以下命令: ``` ssh [username]@[IP address of WSL2] ``` 其中,[username]是WSL2中的用户名,[IP address of WSL2]是WSL2的IP地址。默认情况下,WSL2会动态分配IP地址,可以使用以下命令在WSL2中查找其IP地址: ``` ip addr show eth0 | grep inet | awk '{print $2}' | cut -d / -f 1 ``` 使用这种方法进行远程访问,需要确保WSL2Windows的防火墙设置正确。在Windows防火墙中,需要允许ssh流量通过。在WSL2中,需要允许ssh服务器通过WSL2的防火墙。可以使用以下命令配置WSL2防火墙: ``` sudo ufw allow ssh ``` 使用ssh远程登录WSL2时,还需要注意ssh客户端和服务端之间的证书问题。如果出现证书错误,需要检查证书是否正确配置。 总之,使用ssh远程登录WSL2是一种方便的方式,可让用户像在本地一样使用WSL2。通过配置WindowsWSL2的防火墙,并确保证书正确配置,用户可以安全、可靠地进行远程访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值