Windows 服务器核心(Server-Core)配置 SSH 远程登录

Windows Server Core 是微软从 Windows Server 2008 版开始推出的新版本。它只有命令行,没有图形桌面(当然,基本的图形界面还是具备的,可以运行部分图形界面软件,如 Notepad3),也没有 MMC(管理控制台)。对服务器的管理,需要通过 PowerShell 或 CMD 的命令行来进行。

要管理服务器,有三种方法。一是通过 RDP 远程登录到服务器上,然后在服务器上运行命令。二是使用 Azure 门户提供的基于网页的图形界面 Windows Admin Center。三是是通过 SSH 远程连接到服务器上,然后在 SSH 终端上运行命令。第三种方法对带宽和网速要求最低,所以更受网络管理员青睐。本文就介绍在 Windows Server Core 上配置 SSH 服务器并实现远程登录。

本文所讲的方法适用于 Windows Server 2016 至今的所有版本。

1 安装 OpenSSH

首先需要通过 RDP 连接到服务器,然后通过命令行操作在服务器上安装 OpenSSH Server。文中在服务器上的所有操作,都是在 RDP 远程桌面中进行的。

操作步骤如下。

若要使用 PowerShell 安装 OpenSSH,请先在服务器上以管理员身份运行 PowerShell 并执行以下命令:

1
2
3
4
5
Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
# Name : OpenSSH.Client~~~~0.0.1.0
# State : Installed
# Name : OpenSSH.Server~~~~0.0.1.0
# State : NotPresent

然后在服务器上根据需要安装 Server 或 Client 组件(上面可以看见 Client 已经安装),下面我们直接安装Server端即可:

1
2
3
4
# 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

安装完成会返回以下输出:

1
2
3
# Path          :
# Online : True
# RestartNeeded : False

在服务器上启动并配置 SSH 服务端自动启动:

1
2
3
4
5
Get-Service sshd | Start-Service -PassThru 
Get-Service sshd | Set-Service -StartupType 'Automatic'
# Status Name DisplayName
# ------ ---- -----------
# Running sshd OpenSSH SSH Server

在服务器上查看防火墙规则,应该有一个名为“OpenSSH Server In TCP”的防火墙规则,应该启用该规则:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
Get-NetFirewallRule -Name *ssh* | Get-NetFirewallPortFilter
# Protocol : TCP
# LocalPort : 22
# RemotePort : Any
# IcmpType : Any
# DynamicTarget : Any

如果这条防火墙规则不存在,请创建一个:

1
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

在本地测试是否通过 SSH 正常连接服务器(假设用户名是 Kukmoon,服务器的 IP 是 999.999.999.999):

1
ssh -T Kukmoon@999.999.999.999

2 改端口

把默认的端口 22 改成不常用的端口,例如 22345,以避免恶意扫描。

操作步骤如下。

在服务器上打开 C:\ProgramData\ssh\sshd_config 文件,找到 #Port 22,删除 # 号,另起一行,输入 Port 22345。保存退出。

在服务器上重启 sshd 服务:

1
Restart-Service sshd

在服务器上新建一条防火墙规则:

1
New-NetFirewallRule -Name sshd2 -DisplayName 'OpenSSH Server (sshd) on Port 22345' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22345

在 VPS 的 Web 管理页面(我用的是 Azure),新建一条规则,开放 22345 端口。

在本地测试通过 22345 端口是否正常连接到服务器(假设用户名是 Kukmoon,服务器的 IP 是 999.999.999.999):

1
ssh -T Kukmoon@999.999.999.999 -p 22345

确定可以在本地通过 SSH 连接 22345 端口远程登录服务器以后,服务器上原来的 22 端口需要关闭。

1
Set-NetFirewallRule -DisplayName 'OpenSSH Server (sshd)' -Action Block

3 配置免密登录

操作步骤如下。

在本地生成一对公钥和私钥:

1
ssh-keygen -t rsa -C "applied for Windows Server Core"

用 Notepad3 打开公钥文件 id_rsa.pub,将换行符从 CRLF 改成 LF,如果有必要的话把编码也改成无 BOM 的 UTF-8。

在服务器上建立 C:\Users\Kukmoon\.ssh 文件夹((假设用户名是 Kukmoon),在该文件夹下建立 authorized_keys 文件,用 Notepad3 打开它,把本地的 id_rsa.pub 文件的所有内容,复制粘贴到服务器上的 C:\Users\Kukmoon\.ssh\authorized_keys 文件。

在服务器上修改 authorized_keys 文件的权限:

1
icacls.exe "C:\Users\Kukmoon\.ssh\authorized_keys" /inheritance:r /grant "Kukmoon:F" /grant "SYSTEM:F"

打开服务器的 C:\ProgramData\ssh\sshd_config 文件,找到以下三条,去掉它们前面的 # 号(注意 PasswordAuthentication no 是禁止密码登录):

1
2
3
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys
#PasswordAuthentication no

确保以下两条前面有 # 号:

1
2
#Match Group administrators
# AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

在服务器上重启 sshd 服务:

1
Restart-Service sshd

在本地测试通过 22345 端口是否正常连接到服务器(假设用户名是 Kukmoon,服务器的 IP 是 999.999.999.999):

1
ssh -T Kukmoon@999.999.999.999 -p 22345

在服务器上的 PowerShell 中,以管理员身份执行以下命令,可以把 SSH 的默认 shell 设置为 PowerShell。

1
2
3
4
5
6
7
8
$NewItemPropertyParams = @{
Path = "HKLM:\SOFTWARE\OpenSSH"
Name = "DefaultShell"
Value = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
PropertyType = "String"
Force = $true
}
New-ItemProperty @NewItemPropertyParams

4 图片版权

题图:https://4sysops.com/archives/windows-server-2012-server-core-part-5-tools/

头图:https://pxhere.com/zh/photo/1164378

5 参考文献

  1. Windows服务器核心(Server-Core)安装与基础配置使用
  2. SSH 免密登录 Windows 10 服务器
  3. 解决Windows自带OpenSSH服务器无法实现免密登录
  4. 适用于 Windows Server 和 Windows 的 OpenSSH Server 配置

求扫码打赏
“我这么可爱,请给我钱 o(*^ω^*)o”

Windows 服务器核心(Server-Core)配置 SSH 远程登录
https://blog.kukmoon.com/3a76b4140bd7/
作者
Kukmoon谷月
发布于
2025年12月3日
许可协议