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

I've been shushing all over the place lately. I SSH into Linux from Windows using the built-in OpenSSH Client that Windows 10 has shipped for years that you didn't know about. ;) You don't need Putty to SSH with Windows (unless it makes you happy, then putty on, my friend.)

最近我到处都是嘘。 我使用Windows 10已交付多年的内置OpenSSH客户端从Windows SSH到Linux ,您可能并不了解。 ;)您不需要Putty在Windows上使用SSH(除非我让您高兴,然后再使用Putty,我的朋友。)

将OpenSSH服务器添加到Windows (Adding OpenSSH Server to Windows)

From an Administrative PowerShell I'll see what OpenSSH stuff I have enabled. I can also do this with by typing "Windows Features" from the Start Menu.

从管理PowerShell中,我将看到我已启用的OpenSSH内容。 我也可以通过在“开始”菜单中键入“ Windows功能”来完成此操作。

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

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

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

Looks like I have the OpenSSH client stuff but not the server. I can SSH from Windows, but not to.

看起来我有OpenSSH客户端的东西,但没有服务器。 我可以从Windows进行SSH,但不能。

I'll add it with a similar command with the super weirdo but apparently necessary version thing at the end:

我将使用与超级怪人相似的命令添加它,但最后似乎是必需的版本:

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

在Windows即服务上启动SSHD (Starting SSHD on Windows as a Service)

Once this has finished (and you can of course run this with OpenSSH.Client as well to get both sides if you hadn't) then you can start the SSH server (as a Windows Service) with this, then make sure it's running.

完成此操作后(当然您也可以使用OpenSSH.Client来运行它,如果没有的话也可以获取双方),则可以以此启动SSH服务器(作为Windows服务),然后确保它正在运行。

Start-Service sshd
Get-Service sshd

Since it's a Windows Service you can see it as "OpenSSH SSH Server" in services.msc as well as set it to start automatically on Startup if you like. You can do that again, from PowerShell if you prefer

由于它是Windows服务,因此您可以在services.msc中将其视为“ OpenSSH SSH Server”,并根据需要将其设置为在“启动”时自动启动。 如果愿意,可以从PowerShell再做一次

Set-Service -Name sshd -StartupType 'Automatic'

Remember that we SSH over port 22 so you'll have a firewall rule incoming on 22 at this point. It's up to you to be conscious of security. Maybe you only allow SSHing into your Windows machine with public keys (no passwords) or maybe you don't mind. Just be aware, it's on you, not me.

请记住,我们通过端口22进行SSH,因此此时您将在22上收到防火墙规则。 注意安全性取决于您。 也许您只允许使用公共密钥(没有密码)通过SSH进入Windows计算机,或者您不介意。 请注意,它是在您身上,而不是我。

Now, from any Linux (or Windows) machine I can SSH into my Windows machine like a pro! Note I'm using the .local domain suffix to make sure I don't get a machine on my VPN (staying in my local subnet)

现在,从任何Linux(或Windows)计算机上,我都可以像专业人士一样通过SSH进入Windows计算机! 请注意,我使用的是.local域后缀,以确保我的VPN上没有计算机(保持在本地子网中)

$ ssh scott@ironheart.local
Microsoft Windows [Version 10.0.19041.113]
(c) 2020 Microsoft Corporation. All rights reserved.

scott@IRONHEART C:\Users\scott>pwsh
PowerShell 7.0.0
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell
Type 'help' to get help.

Loading personal and system profiles took 1385ms.
⚡ scott@IRONHEART>

Note that when I SSH'ed into Windows I got the default cmd.exe shell. Remember also that there's a difference between a console, a terminal, and a shell! I can ssh with any terminal into any machine and end up at any shell. In this case, the DEFAULT was cmd.exe, which is suboptimal.

请注意,当我SSH进入Windows时,我得到了默认的cmd.exe shell。 还请记住,控制台,终端和外壳之间是区别的! 我可以用任何终端将ssh放入任何机器,并以任何shell结尾。 在这种情况下,默认值为cmd.exe,这不是最佳选择。

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

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.

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

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\PowerShell\7\pwsh.exe" -PropertyType String -Force

Now when I ssh into my Windows machine from elsewhere (even my iPad!) I get the shell I want:

现在,当我从其他地方(甚至是iPad!)插入Windows机器时,我得到了所需的外壳:

$ ssh scott@ironheart.local
PowerShell 7.0.0
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell
Type 'help' to get help.

Loading personal and system profiles took 1854ms.
⚡ scott@IRONHEART>

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

更好的是,如果我想在Windows终端中添加一个菜单项(配置文件),并为Windows机器添加一个条目,该条目将使用公钥从其他位置自动将其登录,我也可以这样做!

Additionally, now that this is set up I can use WinSCP (available on the Window Store) as well as scp (Secure Copy) to transfer files.

另外,既然已经设置好了,我就可以使用WinSCP(在Window Store上可用)以及scp(安全副本)来传输文件了。

Of course you can also use WinRM or PowerShell Remoting over SSH but for my little internal network I've found this mechanism to be simple and clean. Now my shushing around is non-denominational!

当然,您也可以通过SSH使用WinRM或PowerShell Remoting,但是对于我小的内部网络,我发现此机制非常简单干净。 现在,我悄无声息的是非教派的!

翻译自: https://www.hanselman.com/blog/how-to-ssh-into-a-windows-10-machine-from-linux-or-windows-or-anywhere

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值