本文說明如何使用 SSH 連線至 Windows 虛擬機器 (VM) 執行個體。如要瞭解其他連線至 Windows VM 的方式,請參閱「使用 RDP 連線至 Windows VM」和「使用 PowerShell 連線至 Windows VM」。如要瞭解 SSH 在 Compute Engine 中的運作方式,請參閱「關於 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
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
-
支援的圖片
Windows Server 映像檔支援 Windows 版 SSH,前提是執行的來賓代理程式 (GCEGuestAgent
) 為 20220527.00
以上版本,且 OpenSSH 為 8.6
以上版本。
為 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:
請按照下列步驟開啟管理員命令提示字元工作階段:
開啟「開始」選單。
前往「命令提示字元」。
在「命令提示字元」上按一下滑鼠右鍵,然後選取「以系統管理員身分執行」。
如果系統提示您允許 Command Prompt 變更裝置,請選取「是」。
執行下列指令,下載並安裝
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.com
AD 的網域為example
。USERNAME
:您的 AD 使用者名稱。例如:cloudysanfrancisco
。VM_NAME
:您要連線的 Windows VM 名稱。
後續步驟
- 瞭解如何排解 SSH 連線問題。
- 瞭解如何將安全殼層金鑰新增至 VM
- 瞭解如何限制 VM 的安全殼層金鑰。