Windows 11 Pro 原生支持 OpenSSH Server。启用后,可以从树莓派、Linux 服务器或其他 SSH 客户端远程登录 Windows,并直接执行 PowerShell 命令。
本文记录一套完整流程,包括:
- 安装 OpenSSH Server
- 启动 SSH 服务并设为开机自启
- 检查 Windows 防火墙
- 解决局域网连接超时
- 配置 SSH 公钥登录
- 将远程默认终端改为 PowerShell
- 验证管理员权限
本文以日语界面的 Windows 11 Pro 为例,敏感信息均使用示例值。
一、安装 OpenSSH Server
Windows 可以通过 PowerShell 安装 OpenSSH Server,也可以通过图形界面安装。
在实际操作中,PowerShell 查询可选组件时可能长时间无响应:
Get-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
如果命令一直卡住,甚至 Ctrl + C 也无法终止,可以直接关闭 PowerShell 标签页,改用 Windows 设置安装。
日语界面路径:
設定
→ システム
→ オプション機能
→ 機能を表示
搜索:
OpenSSH サーバー
选择后点击:
次へ
→ 追加
注意不要选成:
OpenSSH クライアント
客户端通常已经预装,而远程登录 Windows 所需的是服务器组件。
二、启动 SSH 服务
安装完成后,以管理员身份打开 Windows Terminal。
推荐使用 Windows Terminal,而不是传统的 Windows PowerShell 黑色控制台窗口。两者功能上都可以使用,但 Windows Terminal 的字体、缩放和标签页体验更好。
在管理员 PowerShell 中执行:
Start-Service sshd
Set-Service sshd -StartupType Automatic
Get-Service sshd
正常结果:
Status Name DisplayName
------ ---- -----------
Running sshd OpenSSH SSH Server
这表示:
- SSH 服务正在运行
- 启动类型已经设为自动
- 下次启动 Windows 后,SSH 会自动运行
三、检查 Windows 防火墙
先检查 Windows 防火墙是否启用:
Get-NetFirewallProfile |
Format-Table Name, Enabled, DefaultInboundAction, DefaultOutboundAction
正常情况下,三个配置文件都应显示:
Enabled = True
分别是:
- Domain
- Private
- Public
再检查 OpenSSH 的入站规则:
Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" |
Format-List Name, DisplayName, Enabled, Direction, Action, Profile
正常结果应包含:
Enabled : True
Direction : Inbound
Action : Allow
Profile : Private
确认端口:
Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" |
Get-NetFirewallPortFilter |
Format-List Protocol, LocalPort
正常为:
Protocol : TCP
LocalPort : 22
还可以检查 SSH 是否正在监听:
Get-NetTCPConnection -LocalPort 22 -State Listen
四、解决 SSH 连接超时
即使 sshd 正常运行、防火墙规则也存在,Linux 客户端连接时仍可能出现:
ssh: connect to host 192.168.x.x port 22: Connection timed out
这种情况通常还没有进入身份验证阶段,因此不是密码或公钥错误,而是网络连接被防火墙拦截。
一个常见原因是:
- OpenSSH 防火墙规则只允许
Private - 当前 Windows 网络却被识别为
Public
检查网络配置文件:
Get-NetConnectionProfile |
Format-Table Name, InterfaceAlias, NetworkCategory, IPv4Connectivity
如果当前无线网卡显示:
NetworkCategory : Public
可以将家庭局域网改为私有网络:
Set-NetConnectionProfile `
-InterfaceAlias "当前网卡名称" `
-NetworkCategory Private
例如:
Set-NetConnectionProfile `
-InterfaceAlias "INTERNAL WLAN" `
-NetworkCategory Private
再次确认:
Get-NetConnectionProfile |
Format-Table InterfaceAlias, NetworkCategory
应显示:
Private
本机测试 SSH 端口:
Test-NetConnection 127.0.0.1 -Port 22
Test-NetConnection 192.168.x.x -Port 22
正常结果:
TcpTestSucceeded : True
此时,局域网中的 Linux 主机应可以访问 Windows 的 TCP 22 端口。
五、配置 SSH 公钥登录
公钥登录的基本结构是:
Linux 主机保存私钥
Windows 保存对应公钥
私钥不能复制到 Windows,也不应泄露。
假设 Linux 主机已经存在:
/root/.ssh/id_rsa
/root/.ssh/id_rsa.pub
查看公钥:
cat /root/.ssh/id_rsa.pub
输出类似:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ... root@linux-host
复制完整的一行。
六、管理员账户的公钥保存位置
Windows OpenSSH 对管理员账户和普通账户使用不同的公钥文件。
管理员账户使用:
C:\ProgramData\ssh\administrators_authorized_keys
普通账户使用:
C:\Users\用户名\.ssh\authorized_keys
如果远程登录账户属于 Windows 管理员组,应将公钥写入:
C:\ProgramData\ssh\administrators_authorized_keys
在管理员 PowerShell 中执行:
$key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ... root@linux-host'
$file = "$env:ProgramData\ssh\administrators_authorized_keys"
New-Item -ItemType Directory `
-Path "$env:ProgramData\ssh" `
-Force | Out-Null
if (!(Test-Path $file)) {
New-Item -ItemType File -Path $file | Out-Null
}
if (-not (Select-String -Path $file -SimpleMatch $key -Quiet)) {
Add-Content -Path $file -Value $key -Encoding ascii
}
然后设置权限:
icacls.exe $file /inheritance:r
icacls.exe $file /grant:r "*S-1-5-32-544:F" "*S-1-5-18:F"
其中:
S-1-5-32-544
表示本机 Administrators 组。
S-1-5-18
表示 SYSTEM。
使用 SID 的好处是不受 Windows 界面语言影响,适用于日语、英语和中文系统。
最后重启 SSH 服务:
Restart-Service sshd
查看已经写入的公钥:
Get-Content "$env:ProgramData\ssh\administrators_authorized_keys"
检查权限:
icacls "$env:ProgramData\ssh\administrators_authorized_keys"
七、从 Linux 测试登录
假设 Windows 局域网地址为:
192.168.x.x
Windows 用户名为:
windowsuser
在 Linux 中执行:
ssh -o IdentitiesOnly=yes \
-i /root/.ssh/id_rsa \
windowsuser@192.168.x.x
首次连接会提示确认主机指纹:
Are you sure you want to continue connecting?
输入:
yes
公钥配置正确时,不会要求输入 Windows 密码,而是直接进入远程终端。
验证:
whoami
hostname
输出类似:
windows-pc\windowsuser
WINDOWS-PC
八、将 SSH 默认终端改为 PowerShell
Windows OpenSSH 默认可能进入传统的:
cmd.exe
登录后提示符类似:
C:\Users\windowsuser>
为了方便远程管理,可以将默认 Shell 改成 Windows PowerShell。
在 Windows 本机的管理员 PowerShell 中执行:
New-ItemProperty `
-Path "HKLM:\SOFTWARE\OpenSSH" `
-Name DefaultShell `
-Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" `
-PropertyType String `
-Force
然后重启 SSH 服务:
Restart-Service sshd
退出并重新登录:
exit
再次通过 SSH 登录后,提示符应变成:
PS C:\Users\windowsuser>
说明远程默认环境已经是 PowerShell。
九、验证远程管理员权限
公钥能够登录管理员账户,并不代表所有情况下都会自动获得提升权限,因此应实际检查。
首先确认账户是否属于管理员组:
([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator
)
返回:
True
再检查完整性级别:
whoami /groups | Select-String "S-1-16-12288"
如果出现:
Mandatory Label\High Mandatory Level
说明当前 SSH PowerShell 是高完整性管理员会话,权限相当于“以管理员身份运行 PowerShell”。
这类会话可以管理:
- Windows 服务
- 防火墙
- 注册表
- 计划任务
- 磁盘和分区
- 设备状态
- 事件日志
- 系统配置
但它仍然不是:
NT AUTHORITY\SYSTEM
也不是:
NT SERVICE\TrustedInstaller
因此准确说法是:已经拥有完整的日常管理员权限,但不是 Windows 内部所有安全主体中的绝对最高权限。
十、远程执行命令
完成配置后,Linux 主机可以直接远程执行 PowerShell 命令:
ssh -o BatchMode=yes \
-o ConnectTimeout=10 \
-i /root/.ssh/id_rsa \
windowsuser@192.168.x.x \
'whoami; hostname; Get-Service sshd'
BatchMode=yes 可以避免自动化任务卡在密码输入界面。
也可以配置 Linux 的 SSH 别名:
Host windows-pc
HostName 192.168.x.x
User windowsuser
IdentityFile /root/.ssh/id_rsa
IdentitiesOnly yes
保存到:
/root/.ssh/config
设置权限:
chmod 600 /root/.ssh/config
以后可以直接连接:
ssh windows-pc
十一、安全建议
建议只在家庭局域网中使用 SSH,不要直接在路由器上将 TCP 22 端口映射到公网。
较稳妥的配置包括:
- Windows 网络设为
Private - 防火墙规则仅允许
Private - 使用 SSH 公钥登录
- 私钥只保存在 Linux 主机
- 不将 Windows 密码写入自动化脚本
- 为 Windows 设置固定局域网地址或 DHCP 地址保留
- 自动化检查默认只读执行
- 禁止代理自行修改注册表、服务、驱动和磁盘配置
公钥登录稳定后,还可以考虑关闭密码登录。不过在确认密钥连接长期可靠之前,不建议过早关闭备用登录方式。
总结
Windows 11 Pro 的 OpenSSH Server 已经足以承担局域网远程管理任务。完整配置链路为:
安装 OpenSSH Server
→ 启动 sshd 并设置自动启动
→ 检查 TCP 22 防火墙规则
→ 将局域网配置为 Private
→ 添加 Linux 公钥
→ 修正 authorized_keys 权限
→ 将默认 Shell 改为 PowerShell
→ 验证高完整性管理员权限
配置完成后,Linux 主机可以通过 SSH 无密码登录 Windows,并执行 PowerShell 系统检查。这为树莓派上的自动化代理远程检查 Windows 服务、设备、磁盘、事件日志和安全配置提供了稳定基础。