本文說明如何使用 SSH 連線至 Windows 虛擬機器 (VM) 執行個體。如要瞭解連線至 Windows VM 的其他方式,請參閱「使用 RDP 連線至 Windows VM」和「使用 PowerShell 連線至 Windows VM」。如要瞭解 Compute Engine 中的 SSH 運作方式,請參閱「關於 SSH 連線」一文。
事前準備
-
如果尚未設定驗證,請先完成設定。
驗證可確認您的身分,以便存取 Google Cloud 服務和 API。如要從本機開發環境執行程式碼或範例,可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
安裝 Google Cloud CLI。 完成後,執行下列指令來初始化 Google Cloud CLI:
gcloud init若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
- Set a default region and zone.
支援的圖片
如果 Windows Server 映像檔執行的是客體代理程式 (
GCEGuestAgent)20220527.00以上版本和 OpenSSH8.6以上版本,則支援 Windows 的 SSH。為 Windows VM 啟用 SSH
如要啟用 Windows VM 的 SSH 連線,請安裝
google-compute-engine-ssh套件,並在專案或執行個體中繼資料中,將enable-windows-ssh鍵設為TRUE。在專案中繼資料中啟用 Windows 的 SSH,即可為專案中的所有 Windows VM 啟用 SSH。在執行個體中繼資料中啟用 Windows 的 SSH,可為單一 VM 啟用 SSH,並覆寫專案中繼資料中設定的值。在建立 VM 時啟用 Windows 的 SSH
使用 Google Cloud 控制台或 Google Cloud CLI 建立 VM 時,啟用 Windows 的 SSH。
控制台
從公開映像檔建立 VM,安裝
google-compute-engine-ssh套件並在啟動時啟用 SSH:指定下列「開機磁碟」屬性:
- 作業系統:Windows Server
- 版本:任何版本
在「Advanced options」(進階選項) 部分,展開「Management」(管理) 部分。
在「中繼資料」部分中,按一下「新增項目」。輸入下列中繼資料鍵/值組合,新增安裝
google-compute-engine-ssh套件的開機指令碼:- 鍵:
sysprep-specialize-script-cmd - 值:
googet -noconfirm=true install google-compute-engine-ssh
- 鍵:
在「中繼資料」部分中,按一下「新增項目」。輸入下列中繼資料鍵值組合,將
enable-windows-ssh設為TRUE:- 鍵:
enable-windows-ssh - 值:
TRUE
- 鍵:
按一下「建立」即可建立並啟動 VM。
gcloud
執行下列
gcloud compute instances create指令,建立 Windows Server VM,在啟動時安裝google-compute-engine-ssh套件並啟用 SSH:gcloud compute instances create VM_NAME \ --image-family=IMAGE_FAMILY \ --image-project=windows-cloud \ --metadata sysprep-specialize-script-cmd="googet -noconfirm=true install google-compute-engine-ssh",enable-windows-ssh=TRUE更改下列內容:
VM_NAME:新 VM 的名稱。IMAGE_FAMILY:Windows Server 映像檔系列。這會使用最新的非已淘汰 Windows Server 映像檔建立 VM。
在執行中的 VM 上為 Windows 啟用 SSH
如要在執行中的 Windows VM 上啟用 SSH,請視您是否能使用 RDP 存取 VM,或工作負載是否能容許重新啟動,採取下列其中一種做法:
- 需要遠端桌面協定存取權:使用遠端桌面協定連線至 VM,然後安裝 SSH 套件
必須重新啟動:使用開機指令碼安裝 SSH 套件
遠端桌面協定
如果可以透過 RDP 存取 VM,請按照下列步驟啟用 SSH:
開啟系統管理員命令提示字元工作階段,方法如下:
開啟「開始」功能表。
前往「命令提示字元」。
在「命令提示字元」上按一下滑鼠右鍵,然後選取「以系統管理員身分執行」。
如果系統提示您允許命令提示字元變更裝置,請選取「是」。
執行下列指令,下載並安裝
google-compute-engine-ssh套件:googet -noconfirm=true install google-compute-engine-windows && googet -noconfirm=true install google-compute-engine-ssh
關閉 RDP 工作階段。
將
enable-windows-ssh鍵設為TRUE,即可在中繼資料中啟用 Windows SSH。如要進一步瞭解如何設定中繼資料,請參閱設定自訂中繼資料。
開機指令碼
如果無法使用 RDP 存取 VM,請按照下列步驟啟用 SSH:
使用 SSH 連線至 VM
使用
gcloud compute ssh指令連線至 VM:gcloud compute ssh VM_NAME
將
VM_NAME替換為要連線的 Windows VM 名稱。連線至使用 AD 的 VM
如果要連線的 VM 使用 Active Directory (AD),請使用下列指令連線:
gcloud compute ssh DOMAIN\USERNAME@VM_NAME
更改下列內容:
DOMAIN:您的 AD 網域。舉例來說,ad.example.comAD 的網域為example。USERNAME:您的 AD 使用者名稱。例如:cloudysanfrancisco。VM_NAME:要連線的 Windows VM 名稱。
後續步驟
- 瞭解如何排解 SSH 連線問題。
- 瞭解如何將安全殼層金鑰新增至 VM
- 瞭解如何限制 VM 的安全殼層金鑰。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-12-04 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-12-04 (世界標準時間)。"],[],[]] -