使用外网穿透 ssh 命令连接 windows 时:ssh username@ip:port,提示:Connection reset by peer;
转为使用内网连接来查找问题,在内网中,使用 ssh 连接时,提示:Operation timed out;
接着,使用命令 ping ip 时,提示:Request timeout for icmp_seq;
google后发现是防火墙的问题,打开 windows 防火墙后, 能 ping 通,但用 ssh 连接时,提示:port 22:Connection refused.
参考博客:如何使用 SSH 控制连接 Windows 服务器 解决问题。
1. 在设置->应用->应用和功能->可选功能里,发现只安装了 OpenSSH Clinet,没有安装 OpenSSH Server,
接下来通过 shell 工具 PowerShell 来安装 OpenSSH Server,因为可选功能里只有 OpenSSH Client,没有 OpenSSH Server,
(1)在安装 PowerShell 并用管理员启动后(类似于 DOS 界面),输入:
Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
提示:
Name : OpenSSH.Client~~~~0.0.1.0
State : Installed
Name : OpenSSH.Server~~~~0.0.1.0
State : NotPresent
(2)用 PowerShell 安装 OpenSSH Server:
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
[如果读条完成之后,没反应,按回车键]
此时,再次输入:
Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
提示:
Name : OpenSSH.Client~~~~0.0.1.0
State : Installed
Name : OpenSSH.Server~~~~0.0.1.0
State : Installed
表示 OpenSSH Server 安装成功。
(3)重点来了:重启!重启!重启!重要的事情说三遍,不然后续的命令都会提示找不到。
(4)在完成 OpenSSH Server 的安装之后,用管理员打开 PowerShell 执行界面,开启 sshd 服务(无提示):
Start-Service sshd
并设为自动启动(无提示):
Set-Service -Name sshd -StartupType 'Automatic'
(5)验证防火墙是否允许 OpenSSH 服务通过:
Get-NetFirewallRule -Name *ssh*
提示:
Name : OpenSSH-Server-In-TCP
DisplayName : OpenSSH SSH Server (sshd)
Description : Inbound rule for OpenSSH SSH Server (sshd)
DisplayGroup : OpenSSH Server
Group : OpenSSH Server
Enabled : True
Profile : Any
Platform : {}
Direction : Inbound
Action : Allow
EdgeTraversalPolicy : Block
LooseSourceMapping : False
LocalOnlyMapping : False
Owner :
PrimaryStatus : OK
Status : 已从存储区成功分析规则。 (65536)
EnforcementStatus : NotApplicable
PolicyStoreSource : PersistentStore
PolicyStoreSourceType : Local
Enable 状态是 True,Action 状态是 Allow,表示允许通过,OpenSSH 可用。
2. 内网远程连接:ssh username@ip,成功。
在安装 OpenSSH Sever 之前,尝试关闭 Windows 防火墙,并在防火墙高级设置中添加入站规则,允许通过 port: 22 进行连接,并没有解决问题,安装 OpenSSH Sever 之后,开启防火墙,并删除入站规则,发现不影响连接[疑惑~];之后发现是:Windows Defender 防火墙->允许应用或功能通过 Windows Defender 防火墙,其中 OpenSSH 已添加到列表。