适用于 Windows 的 OpenSSH 入门

OpenSSH 是一款用于远程登录的连接工具,它使用 SSH 协议。 它会加密客户端与服务器之间的所有流量,从而遏止窃听、连接劫持和其他攻击。

OpenSSH 兼容的客户端可用于连接到 Windows Server 和 Windows 客户端设备。

Important

If you downloaded the OpenSSH beta from the GitHub repo at PowerShell/Win32-OpenSSH, follow the instructions listed there, not the ones in this article. Win32-OpenSSH 存储库中的一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 Microsoft 不对此处提供的信息作任何明示或默示的担保。

Prerequisites

在开始之前,计算机必须满足以下要求:

  • 至少运行 Windows Server 2019 或 Windows 10(内部版本 1809)的设备。

  • PowerShell 5.1 或更高版本。

  • 作为内置管理员组成员的帐户。

Prerequisites check

若要验证环境,请打开提升的 PowerShell 会话并执行以下操作:

  • Enter winver.exe and press enter to see the version details for your Windows device.

  • 运行 $PSVersionTable.PSVersion。 验证主要版本至少为 5,次要版本至少为 1。 详细了解如何在 Windows 上安装 PowerShell

  • 运行以下命令。 当你是内置 Administrator 组的成员时,输出将显示 True

    (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
    

启用适用于 Windows Server 2025 的 OpenSSH

从 Windows Server 2025 开始,OpenSSH 现在默认安装。 还可以在服务器管理器中启用或禁用 sshd 服务。

若要使用服务器管理器启用 SSHD,请执行以下操作:

  1. In Server Manager, on the navigation pane to the left, select Local Server.

  2. In the Properties window, locate Remote SSH Access.

  3. Select Disabled to enable the OpenSSH service.

Note

If you need to allow or restrict specific users or groups from using OpenSSH for remote access, add them to the OpenSSH Users user group.

安装适用于 Windows Server 的 OpenSSH

要在 Windows 10 设备上安装 OpenSSH 组件,请执行以下操作:

  1. Open Settings, select System, then select Optional Features.

  2. 扫描列表,查看是否已安装 OpenSSH。 如果未安装,请在页面顶部选择“添加功能”,然后

    • Find OpenSSH Client, then select Install

    • Find OpenSSH Server, then select Install

  3. Open the Services desktop app. (Select Start, type services.msc in the search box, and then select the Service app or press ENTER.)

  4. 在详细信息窗格中,双击“OpenSSH SSH 服务器”

  5. On the General tab, from the Startup type drop-down menu, select Automatic and then select Ok.

  6. To start the service, select Start.

要在 Windows 11 设备上安装 OpenSSH 组件,请执行以下操作:

  1. Open Settings, select System, then select Optional Features.

  2. 扫描列表,查看是否已安装 OpenSSH。 If not, at the top of the page, select View Features, then:

    • Search for OpenSSH Client, select Next, then select Install

    • Search for OpenSSH Server, select Next, then select Install

  3. Open the Services desktop app. (Select Start, type services.msc in the search box, and then select the Service app or press ENTER.)

  4. 在详细信息窗格中,双击“OpenSSH SSH 服务器”

  5. On the General tab, from the Startup type drop-down menu, select Automatic and then select Ok.

  6. To start the service, select Start.

要在 Windows Server 设备上安装 OpenSSH 组件,请执行以下操作:

  1. Open Settings, select System, then select Optional Features (also referred to as Manage optional features).

  2. 扫描列表,查看是否已安装 OpenSSH。 如果未安装,请在页面顶部选择“添加功能”,然后

    • Search for OpenSSH Client, then select Install

    • Search for OpenSSH Server, then select Install

  3. Open the Services desktop app. (Select Start, type services.msc in the search box, and then select the Service app or press ENTER.)

  4. 在详细信息窗格中,双击“OpenSSH SSH 服务器”

  5. On the General tab, from the Startup type drop-down menu, select Automatic and then select Ok.

  6. To start the service, select Start.

Note

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

连接到 OpenSSH 服务器

安装后,可以从安装了 OpenSSH 客户端的 Windows 或 Windows Server 设备连接到 OpenSSH 服务器。 在 PowerShell 提示符下,运行以下命令。

ssh domain\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)?

Entering yes adds that server to the list of known SSH hosts on your Windows client.

此时,服务会提示你输入密码。 作为安全预防措施,输入密码时不会显示密码字符。

连接后,你将看到以下 Windows 命令行界面提示符:

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

卸载适用于 Windows 的 OpenSSH

若要使用 Windows 设置来卸载 OpenSSH:

  1. Open Settings, select System, then select Optional Features (also referred to as Manage optional features).

  2. In the list, select OpenSSH Client or OpenSSH Server.

  3. Select Uninstall.

如果卸载服务时正在使用该服务,则应重启 Windows。

Next steps

现在,你已经完成了适用于 Windows 的 OpenSSH 服务器的安装,以下是一些可以帮助你学习如何使用它的文章: