−25%

Windows 年繳方案適用,至 10 月 31 日。 檢視方案

EQVPS
開始使用

如何在 Windows Server 2022 安裝 OpenSSH Server(金鑰登入,PowerShell 作為殼層)

在 Windows Server 2022 VPS 上安裝內建的 OpenSSH Server,把 PowerShell 設為預設殼層,以 Administrator 身分用金鑰登入,並關閉密碼登入。附上兩個會讓金鑰登入失敗的 Windows 特有陷阱。

Windows Server 2022 把 OpenSSH Server 作為選用功能提供:不需要第三方安裝程式,透過 Windows Update 更新。啟用後,你可以透過 SSH 執行 PowerShell、用 scp 複製檔案,並像對待任何 Linux 電腦一樣把指令碼或 CI 指向這台伺服器。

以下所有操作都是在我們的 Windows Server 2022 映像檔上,透過 RDP 開啟的系統管理員 PowerShell 中完成(如何連線)。

先談網路。 在 NAT 的 Windows 方案上,唯一的輸入轉送指向 RDP,所以無法從網際網路連到 SSH。要從外部使用 SSH,需要有獨立 IPv4 的方案,例如 Windows Small-IP。連接埠可連線時,下面的步驟大約五分鐘。

1. 安裝 OpenSSH Server

Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

用戶端已經安裝好;伺服器端在你加入之前會顯示 NotPresent。在我們的伺服器上,安裝花了 28 秒。

2. 啟動服務並設為開機自動啟動

Start-Service sshd
Set-Service -Name sshd -StartupType Automatic
Get-NetFirewallRule -Name *OpenSSH*

安裝這個功能時會建立防火牆規則 OpenSSH-Server-In-TCP(連接埠 22,輸入,允許),所以不需要自己新增。

3. 把 PowerShell 設為預設殼層

預設情況下,SSH 工作階段會進入 cmd.exe。改成 PowerShell:

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force

新連線會立即使用 PowerShell 5.1,不需重新啟動。

4. 為 Administrator 加入金鑰:人人都會踩到的檔案

這是 Windows 的第一個陷阱。對於 Administrators 群組的帳戶,sshd 不會讀取 C:\Users\<name>\.ssh\authorized_keys。它讀取一個共用檔案 C:\ProgramData\ssh\administrators_authorized_keys,而且只有在除了 Administrators 和 SYSTEM 之外沒人能寫入時才會讀取:

$pub = 'ssh-ed25519 AAAA...your-public-key... you@laptop'
Set-Content -Path C:\ProgramData\ssh\administrators_authorized_keys -Value $pub -Encoding ascii
icacls.exe "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"

-Encoding ascii 也很重要。在我們的測試中,同一把金鑰用 Out-File(UTF-16,PowerShell 5.1 的預設編碼)儲存後,會被 Permission denied (publickey) 拒絕。在你自己的電腦上:

ssh Administrator@YOUR.SERVER.IP

應該不需密碼就出現 PowerShell 提示字元。對於非系統管理員的使用者,一般的 C:\Users\<name>\.ssh\authorized_keys 可以正常使用。

5. 關閉密碼登入:第二個陷阱

最直觀的修改 PasswordAuthentication no 在這個版本並不夠。OpenSSH 8.1 仍會提供 keyboard-interactive,詢問的是同一組 Windows 密碼。在我們的測試中,直到把它也關掉之前,不帶金鑰的登入仍會顯示 keyboard-interactive。此外,這些設定必須放在檔案結尾 Match Group administrators 區塊的前面,否則只對系統管理員生效。下面的指令把兩行都放到檔案開頭:

$cfg = 'C:\ProgramData\ssh\sshd_config'
$lines = Get-Content $cfg | Where-Object { $_ -notmatch '^#?\s*(PasswordAuthentication|KbdInteractiveAuthentication|ChallengeResponseAuthentication)\s' }
Set-Content $cfg -Value (@('PasswordAuthentication no', 'ChallengeResponseAuthentication no') + $lines) -Encoding ascii
Restart-Service sshd

保持目前的 SSH 工作階段不要關閉,從第二個終端機測試。只用密碼嘗試,現在應該會得到:

Permission denied (publickey).

在我們的測試中,Administrator 和一般使用者都是這個結果,而金鑰登入仍然可用。

可以用它做什麼

  • 從任何地方執行指令:ssh Administrator@IP 'Get-Service sshd'。
  • 複製檔案:scp .\app.zip Administrator@IP:C:/apps/。
  • 用與 RDP 相同的方法,把連接埠限制為你的位址:Set-NetFirewallRule -Name OpenSSH-Server-In-TCP -RemoteAddress 203.0.113.10。

選哪個方案

從外部使用 SSH 需要公用 IPv4:Windows Small-IP(4 vCPU、4 GB、50 GB)每月 $28,Windows Medium-IP(6 vCPU、6 GB)每月 $36。Windows 是使用你自有授權的 Server 2022(BYOL 說明)。四個方案都在 Windows VPS 頁面。想讓程式在中斷連線後繼續執行,請參考 在 Windows Server 上 24/7 執行指令碼。

常見問題

已經有 RDP 了,為什麼還要在 Windows 上用 SSH?

指令碼和工具都透過 SSH 溝通。伺服器上有了 OpenSSH,你就能從 Linux 電腦或 CI 工作執行 PowerShell 指令、用 scp 複製檔案,並讓自動化在沒有桌面工作階段的情況下管理伺服器。

我的金鑰對一般使用者有效,對 Administrator 卻無效,為什麼?

對於 Administrators 群組的成員,Windows 上的 OpenSSH 會忽略 ~/.ssh/authorized_keys,改為讀取 C:\ProgramData\ssh\administrators_authorized_keys。這個檔案只能由 Administrators 和 SYSTEM 寫入,否則 sshd 會默默略過它。

NAT 的 Windows 方案能用 SSH 連線嗎?

從外部不行。在 NAT 的 Windows 方案上,專屬外部連接埠轉送到 RDP(3389),這是唯一的輸入轉送。要從網際網路使用 SSH,請選擇有獨立 IPv4 的 Windows 方案。在伺服器內部,OpenSSH 仍可供本機工具使用。

Windows Server 2022 安裝的是哪個版本的 OpenSSH?

OpenSSH 8.1p1 for Windows。因此密碼相關設定使用 ChallengeResponseAuthentication,而不是較新的名稱 KbdInteractiveAuthentication。

評論

目前還沒有評論。歡迎成為第一位。

發表評論

評論在顯示前會經過審核。