Windows Server 内置了 Microsoft 移植的 OpenSSH Server,因此你可以像管理 Linux 一样管理 Windows 服务器:通过 SSH 执行 PowerShell 命令、用脚本自动化运维,并使用 scp 或 WinSCP、FileZilla 等 SFTP 客户端安全地传输文件,无需打开远程桌面会话。本文介绍如何在 Windows Server 2019、2022 和 2025 上安装并启动 SSH 服务器、将 PowerShell 设为默认 Shell、为管理员配置密钥登录、修改端口以及传输文件。
以管理员身份打开 PowerShell。下面的命令可以查看客户端和服务器端是否已安装;在 Windows Server 2025 上,服务器端通常已经预装,只需启用即可。
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
# Install the server if its State is NotPresent
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Windows Server 2016 没有内置该组件,请从 GitHub 上 Microsoft 的 PowerShell/Win32-OpenSSH 发布页面下载 Win32-OpenSSH 的 MSI 安装包进行安装。
# Start sshd now and at every boot
Start-Service sshd
Set-Service -Name sshd -StartupType Automatic
# The installer normally creates an inbound rule for TCP 22; create it if it is missing
if (-not (Get-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -ErrorAction SilentlyContinue)) {
New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' `
-Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
}
Get-Service sshd
netstat -ano | findstr ":22"
现在在你的电脑上执行 ssh [email protected] 进行连接(Windows 10/11、macOS 和 Linux 都自带 SSH 客户端),然后输入 Administrator 密码。默认登录后进入的是 cmd.exe 命令提示符。
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell `
-Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
之后新建的 SSH 会话会直接进入 PowerShell。如果安装了 PowerShell 7,请改为填写 pwsh.exe 的路径。
对于 Administrators 组中的账户,Windows 版 OpenSSH 从一个共用文件 C:\ProgramData\ssh\administrators_authorized_keys 读取公钥,而不是从用户自己的 .ssh 文件夹读取。该文件必须只允许 Administrators 和 SYSTEM 访问,否则 sshd 会忽略它。
# On your own computer: create a key pair if you do not have one yet
ssh-keygen -t ed25519
# On the server (PowerShell as Administrator): paste your PUBLIC key (id_ed25519.pub)
Add-Content -Path "C:\ProgramData\ssh\administrators_authorized_keys" -Value "ssh-ed25519 AAAA...your-key... you@laptop"
icacls.exe "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
请在新窗口中测试密钥登录。确认可以正常登录后,用记事本打开 C:\ProgramData\ssh\sshd_config,设置:
PubkeyAuthentication yes
PasswordAuthentication no
Restart-Service sshd
在确认新的密钥登录仍然可用之前,请保持当前会话不要断开。对于普通(非管理员)用户,公钥应放在 C:\Users\USERNAME\.ssh\authorized_keys 中。
# In C:\ProgramData\ssh\sshd_config set: Port 2222
New-NetFirewallRule -Name 'OpenSSH-2222' -DisplayName 'OpenSSH Server 2222' `
-Direction Inbound -Protocol TCP -LocalPort 2222 -Action Allow
Restart-Service sshd
Disable-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' # close 22 after testing 2222
如果只允许从你自己的地址访问 SSH,请在防火墙规则中加上 -RemoteAddress 198.51.100.20。更多 Windows 防火墙规则配置请参见 Windows 防火墙开放与关闭端口。
# Upload a file to the Administrator desktop (use forward slashes in Windows paths)
scp .\site-backup.zip [email protected]:C:/Users/Administrator/Desktop/
# Download a folder recursively
scp -r [email protected]:C:/inetpub/wwwroot ./wwwroot-copy
# Interactive SFTP session
sftp -P 2222 [email protected]
WinSCP、FileZilla 等图形化客户端同样可用:选择 SFTP 协议,填写服务器 IP、端口以及你的密钥或密码即可。传输大文件时,这比通过远程桌面剪贴板复制粘贴快得多,也稳定得多。
对于管理员账户,公钥必须写入 administrators_authorized_keys,并且权限要与步骤 4 完全一致。查看事件查看器 → 应用程序和服务日志 → OpenSSH → Operational(Event Viewer → Applications and Services Logs → OpenSSH → Operational)中的日志,通常会直接指出问题所在,例如文件所有者或权限不正确。
可能是 sshd 服务未运行、防火墙规则缺失,或连接的端口不对。在你的电脑上运行 Test-NetConnection 203.0.113.10 -Port 22,并在服务器上运行 Get-Service sshd 进行检查。
可以。SSH 和远程桌面可以同时使用。很多管理员用 SSH 执行脚本和传输文件,用远程桌面操作图形界面工具。
如果按以上步骤操作后问题仍未解决,请提交工单联系 IMIDC 7×24 技术支持,并附上服务器 IP、Windows Server 版本、执行的 SSH 命令和完整的报错输出(可运行 ssh -v 查看详细信息),方便工程师快速定位。