无法连接XShell,出现Could not connect to....情况

根据我最近遇到的情况,大概率是搭建服务器中,系统没有开启SSH服务,导致无法RDP

使用 Windows 设置安装 OpenSSH

这两个 OpenSSH 组件都可以使用 Windows Server 2019 和 Windows 10 设备上的 Windows 设置进行安装。

要安装 OpenSSH 组件:

  1. 打开设置,选择应用程序 > 应用程序和功能,然后选择可选功能

  2. 扫描列表以查看是否已安装 OpenSSH。如果没有,请在页面顶部选择Add a feature,然后:

    • 找到OpenSSH Client,然后点击Install
    • 找到OpenSSH Server,然后点击Install

设置完成后,返回应用程序 > 应用程序和功能以及可选功能,您应该会看到列出的 OpenSSH。

 笔记

安装 OpenSSH 服务器将创建并启用名为OpenSSH-Server-In-TCP. 这允许端口 22 上的入站 SSH 流量。如果未启用此规则且此端口未打开,则连接将被拒绝或重置。

使用 PowerShell 安装 OpenSSH

要使用 PowerShell 安装 OpenSSH,请以管理员身份运行 PowerShell。要确保 OpenSSH 可用,请运行以下 cmdlet:

电源外壳复制
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

如果两者都没有安装,这应该返回以下输出:

复制
Name  : OpenSSH.Client~~~~0.0.1.0
State : NotPresent

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

然后,根据需要安装服务器或客户端组件:

电源外壳复制
# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# Install the OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

这两个都应该返回以下输出:

复制
Path          :
Online        : True
RestartNeeded : False

启动和配置 OpenSSH 服务器

要启动和配置 OpenSSH 服务器以供初始使用,请以管理员身份打开 PowerShell,然后运行以下命令来启动sshd service

电源外壳复制
# Start the sshd service
Start-Service sshd

# OPTIONAL but recommended:
Set-Service -Name sshd -StartupType 'Automatic'

# Confirm the Firewall rule is configured. It should be created automatically by setup. Run the following to verify
if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
    Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
    New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
    Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
}

连接到 OpenSSH 服务器

安装后,您可以使用 PowerShell 安装 OpenSSH 客户端,从 Windows 10 或 Windows Server 2019 设备连接到 OpenSSH 服务器,如下所示。请务必以管理员身份运行 PowerShell:

电源外壳复制
ssh username@servername

连接后,您会收到类似于以下内容的消息:

复制
The authenticity of host 'servername (10.00.00.001)' can't be established.
ECDSA key fingerprint is SHA256:(<a large string>).
Are you sure you want to continue connecting (yes/no)?

选择yes将该服务器添加到 Windows 客户端上的已知 SSH 主机列表中。

此时会提示您输入密码。作为安全预防措施,您的密码不会在您键入时显示。

连接后,您将看到 Windows 命令 shell 提示符:

复制
domain\username@SERVERNAME C:\Users\username>

OpenSSH 配置文件

OpenSSH 具有服务器和客户端设置的配置文件。OpenSSH 是开源的,从 Windows Server 2019 和 Windows 10(内部版本 1809)开始添加到 Windows Server 和 Windows Client 操作系统。因此,此处不再重复 OpenSSH 配置文件的文档。客户端配置文件可以在ssh_config 手册页上找到,OpenSSH 服务器的配置文件可以在sshd_config 手册页上找到。更多特定于 Windows 的OpenSSH 服务器配置在适用于 Windows的 OpenSSH 服务器配置中有详细说明。

在 Windows 中,OpenSSH 客户端 (ssh) 按以下顺序从配置文件中读取配置数据:

  1. 通过使用 -F 参数启动 ssh.exe,指定配置文件的路径和该文件中的条目名称。
  2. %userprofile%\.ssh\config 中的用户配置文件。
  3. %programdata%\ssh\ssh_config 中的系统范围配置文件。

Open SSH Server (sshd) 默认从 %programdata%\ssh\sshd_config 读取配置数据,或者可以通过使用 -f 参数启动 sshd.exe 来指定不同的配置文件。如果该文件不存在,则 sshd 会在服务启动时使用默认配置生成一个。

使用 Windows 设置卸载 OpenSSH

使用 Windows 设置卸载 OpenSSH:

  1. 打开设置,然后转到应用程序 > 应用程序和功能
  2. 转到可选功能
  3. 在列表中,选择OpenSSH ClientOpenSSH Server
  4. 选择卸载

使用 PowerShell 卸载 OpenSSH

要使用 PowerShell 卸载 OpenSSH 组件,请使用以下命令:

电源外壳复制
# Uninstall the OpenSSH Client
Remove-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# Uninstall the OpenSSH Server
Remove-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

如果该服务在卸载时正在使用中,您可能需要在之后重新启动 Windows。

出处:

wg​​​​​​​Get started with OpenSSH | Microsoft Docs


版权声明:本文为m0_49670860原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。