wsl bash_简便的方法如何从外部计算机在Windows 10上SSH进入Bash和WSL2

这篇博客介绍了如何配置Windows 10的OpenSSH,以便从外部计算机通过SSH直接进入WSL2的Bash,而不必进行复杂的端口转发和IP管理。通过设置默认Shell为WSL2的Bash,可以简化过程,同时利用Windows的SSH服务和身份验证。
摘要由CSDN通过智能技术生成
wsl bash

wsl bash

Some folks always trying to ice skate up hill

This is an interesting blog post on How to SSH into WSL2 on Windows 10 from an external machine. Read it. Know how it works. Learn it. AND DO NOT DO IT BECAUSE IT'S TOO COMPLEX.

这是一篇有关如何从外部计算机将SSH插入Windows 10上的WSL2的有趣博客文章。 阅读。 知道它是如何工作的。 学习吧。 并且不要这样做,因为它太复杂了

DO NOT DO THIS. It's fine. It works. But it's baroque. You're forwarding ports into a little VM'ed local subnet, you're dealing with WSL2 IP addresses changing, you'll have to keep your VM running, and you're generally trying to ice skate up hill.

不要这样做。 没关系。 有用。 但这是巴洛克式的。 您正在将端口转发到VM的一个小本地子网中,正在处理WSL2 IP地址的更改,必须保持VM的运行,并且通常试图溜冰。

Here's the thing. In that post - which you should not do - you're turning off the Windows Firewall for your port, forwarding to an internal subnet, and then letting WSL take over.

这是东西在那篇文章中(您不应该这样做),您要关闭端口的Windows防火墙,转发到内部子网,然后让WSL接管。

BUT! Windows 10 already knows how to accept SSH connections. In fact, it's shipped OpenSSH as a "Feature on Demand" for years. The issue is that you (Mac and Linux switchers) don't like the default shell - PowerShell.

但! Windows 10已经知道如何接受SSH连接。 实际上,它已经将OpenSSH作为“按需功能”交付了多年。 问题是您(Mac和Linux切换台)不喜欢默认外壳-PowerShell。

那么,为什么不将SSH的默认Windows Shell更改为WSL2的Bash呢? (So why not change the default Windows shell for SSH to WSL2's Bash? )

Boom. Now you have no port forwarding, firewalls are only opening for one process, and your WSL2 instance starts up on entry. Literally the best of all worlds.

繁荣。 现在您没有端口转发,防火墙只为一个进程打开,WSL2实例在启动时启动。 从字面上看是世界上最好的。

如何在Windows 10计算机上将SSH设置为进入WSL2 (How do you set up SSH'ing into WSL2 on your Windows 10 machine)

First, open an admin PowerShell prompt (Start menu, type PowerShell, hold ctrl+shift, and hit enter) type this:

首先,打开管理员PowerShell提示符(“开始”菜单,键入PowerShell,按住Ctrl + Shift,然后按Enter键),输入以下命令:

> Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'

Name : OpenSSH.Client~~~~0.0.1.0
State : Installed

Name : OpenSSH.Server~~~~0.0.1.0
State : NotPresent

See how I have the Client and not the OpenSSH Server? Add it:

看看我有客户端而不是OpenSSH Server吗? 添加:

Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

Now either start the SSHD service, or set it to start automatically:

现在,要么启动SSHD服务,要么将其设置为自动启动:

Start-Service sshd
Get-Service sshd

or automatic:

或自动:

Set-Service -Name sshd -StartupType 'Automatic'

在Windows 10中为OpenSSH配置默认Shell (Configuring the Default Shell for OpenSSH in Windows 10)

On my server (the Windows machine I'm SSHing into) I will set a registry key to set the default shell. In this case, I'll use open source cross platform PowerShell Core. You can use whatever makes you happy and WSL2's bash makes me happy.

在服务器(我正在SSH接入的Windows计算机)上,我将设置一个注册表项来设置默认的shell 。 在这种情况下,我将使用开源跨平台PowerShell Core。 您可以使用任何使您感到快乐的东西,而WSL2的重击使我感到高兴。

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\WINDOWS\System32\bash.exe" -PropertyType String -Force

Note that bash.exe in this context starts up "wsl -d YOURDEFAULTDISTRO" so you'll want to know what your Default is, and if you don't like it, change it with wsl --set-default DISTRO.

请注意,在这种情况下wsl -d YOURDEFAULTDISTRO会启动“ wsl -d YOURDEFAULTDISTRO ”,因此您将想知道默认值是什么,如果不喜欢它,请使用wsl --set-default DISTRO

HEADS UP: You need the FULL AND CORRECT PATH in that command above. It works for any shell. Since I'm using bash.exe, I get WSL2 starting up for free but SSH with this solution is using Windows's SSH keys and Windows auth. Note that when you're entering your password for authentication!

注意:您需要在上面的命令中使用“完整且正确的路径”。 它适用于任何外壳。 由于我使用的是bash.exe,因此我可以免费启动WSL2,但是使用此解决方案的SSH使用Windows的SSH密钥和Windows身份验证。 请注意,当您输入密码进行身份验证时!

Even better if I wanted to add a menu item (profile) to one local Windows Terminal with an entry to ssh into my WSL on my remote Windows Machine that would automatically log me into it from elsewhere using public keys, I could do that also!

甚至更好的是,如果我想向一个本地Windows终端添加菜单项(配置文件),并在远程Windows机器上输入ssh进入我的WSL的条目,它将使用公钥从其他地方自动将其登录,我也可以这样做!

To conclude and sum up:

总结和总结:

  • This blog post - the one you are reading 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通信。

  • This other blog post - over here - 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规则和防火墙规则。

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

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

Enjoy!

请享用!

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

wsl bash

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值