Windows 伺服器

Windows Server 開啟 SSH 遠端登入:安裝 OpenSSH Server、配置金鑰登入與 SCP/SFTP 傳輸檔案

6 個步驟 9 分鐘閱讀 17 次閱讀 0
本文目錄

Windows Server 內建了 Microsoft 移植的 OpenSSH Server,因此你可以像管理 Linux 一樣管理 Windows 伺服器:通過 SSH 執行 PowerShell 命令、用指令碼自動化運維,並使用 scp 或 WinSCP、FileZilla 等 SFTP 客戶端安全地傳輸檔案,無需開啟遠端桌面會話。本文介紹如何在 Windows Server 2019、2022 和 2025 上安裝並啟動 SSH 伺服器、將 PowerShell 設為預設 Shell、為管理員配置金鑰登入、修改埠以及傳輸檔案。

和 Linux 一樣,開放在 22 埠的 SSH 服務會招來大量猜解密碼的機器人。請使用金鑰認證並停用密碼登入(步驟 4),或至少在防火牆中將 22 埠限製為僅允許你自己的 IP 訪問。

步驟 1:安裝 OpenSSH Server

以管理員身份開啟 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 安裝包進行安裝。

步驟 2:啟動服務並檢查防火牆

# 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 命令提示符。

步驟 3:將 PowerShell 設為預設 Shell

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 的路徑。

步驟 4:使用 SSH 金鑰登入並停用密碼登入

對於 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 中。

步驟 5:修改 SSH 埠(可選)

# 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 防火牆開放與關閉埠。

步驟 6:使用 scp 和 SFTP 傳輸檔案

# 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)中的日誌,通常會直接指出問題所在,例如檔案所有者或許可權不正確。

SSH 連線超時?

可能是 sshd 服務未執行、防火牆規則缺失,或連線的埠不對。在你的電腦上執行 Test-NetConnection 203.0.113.10 -Port 22,並在伺服器上執行 Get-Service sshd 進行檢查。

開啟 SSH 後還能使用遠端桌面嗎?

可以。SSH 和遠端桌面可以同時使用。很多管理員用 SSH 執行指令碼和傳輸檔案,用遠端桌面操作圖形介面工具。

如果按以上步驟操作後問題仍未解決,請提交工單聯絡 IMIDC 7×24 技術支援,並附上伺服器 IP、Windows Server 版本、執行的 SSH 命令和完整的報錯輸出(可執行 ssh -v 檢視詳細資訊),方便工程師快速定位。

這篇文章有幫助嗎?

相關教程