本文說明如何設定應用程式,透過 SSH 和 OS 登入,以程式輔助方式在兩個虛擬機器 (VM) 執行個體之間建立連線。啟用應用程式使用 SSH 功能,有助於自動執行系統管理程序。
本指南中使用的所有程式碼範例,都託管在 GoogleCloudPlatform/python-docs-samples GitHub 頁面。
事前準備
- 為服務帳戶設定 SSH。
- 在專案或以服務帳戶身分執行的 VM 上設定 OS 登入。
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 Trusted Cloud by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.
gcloud
-
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。 登入後,執行下列指令初始化 Google Cloud CLI:
gcloud init
- Set a default region and zone.
設定 SSH 應用程式
設定應用程式,管理安全殼層金鑰並啟動與 Compute Engine VM 的安全殼層連線。整體來說,應用程式應執行下列操作:
- 匯入 Google OS Login 程式庫,建構用戶端程式庫,以便透過 OS Login API 進行驗證。
- 初始化 OS 登入用戶端物件,讓應用程式可以使用 OS 登入功能。
- 實作
create_ssh_key()
方法,為 VM 的服務帳戶產生 SSH 金鑰,並將公開金鑰新增至服務帳戶。 - 從 OS Login 程式庫呼叫
get_login_profile()
方法,以取得服務帳戶使用的 POSIX 使用者名稱。 - 實作
run_ssh()
方法,執行遠端安全殼層指令。 - 移除臨時的安全殼層 (SSH) 金鑰組檔案。
安全殼層應用程式範例
oslogin_service_account_ssh.py
範例應用程式示範了安全殼層應用程式的可能實作方式。在本範例中,應用程式使用run_ssh()
方法在遠端執行個體上執行指令,並傳回指令輸出內容。執行 SSH 應用程式
建立使用 SSH 的應用程式後,您可以按照類似下列範例的程序執行應用程式,範例會安裝並執行
oslogin_service_account_ssh.py
範例應用程式。您安裝的程式庫可能會因應用程式使用的程式設計語言而有所不同。或者,您也可以編寫應用程式,匯入
oslogin_service_account_ssh.py
並直接執行。連線至 VM,該 VM 會代管 SSH 應用程式。
在 VM 上安裝
pip
和 Python 3 用戶端程式庫:sudo apt update && sudo apt install python3-pip -y && pip install --upgrade google-cloud-os-login requests
選用步驟:如果您使用
oslogin_service_account_ssh.py
範例應用程式,請從 GoogleCloudPlatform/python-docs-samples 下載:curl -O https://raw.githubusercontent.com/GoogleCloudPlatform/python-docs-samples/master/compute/oslogin/oslogin_service_account_ssh.py
執行 SSH 應用程式。範例應用程式使用
argparse
接受指令列的變數。在本範例中,請指示應用程式在專案中的另一個 VM 上安裝並執行cowsay
。python3 service_account_ssh.py \ --cmd 'sudo apt install cowsay -y && cowsay "It works!"' \ --project=PROJECT_ID --instance=VM_NAME --zone=ZONE
更改下列內容:
PROJECT_ID
:應用程式連線的 VM 專案 ID。VM_NAME
:應用程式連線的 VM 名稱。ZONE
:應用程式連線的 VM 所在可用區。
輸出結果會與下列內容相似:
⋮ ___________ It works! ----------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
後續步驟
- 下載並檢視完整程式碼範例。完整範例內含同時使用所有方法的小範例。請依照自身需求下載、變更或執行範例。
- 進一步瞭解 Compute Engine 中的 SSH 連線運作方式, 包括 SSH 金鑰設定和儲存方式。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-08 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-08 (世界標準時間)。"],[[["This guide outlines the process of configuring applications to establish programmatic connections between two virtual machine (VM) instances using SSH and OS Login, which can enhance system management automation."],["The document details the steps to set up an SSH app, including importing the Google OS Login library, initializing the OS Login Client, generating SSH keys, obtaining the service account's POSIX username, implementing an SSH command execution method, and managing temporary SSH key files."],["A sample Python app, `oslogin_service_account_ssh.py`, is provided as an example, demonstrating how to use the OS Login API to apply public SSH keys for a service account and execute commands on a remote instance over SSH, with code hosted on GitHub."],["Instructions are given on how to prepare your environment and run the provided sample app, including connecting to the VM, installing necessary libraries, and executing a remote command via SSH, including examples."],["The guide references how to view the full code sample on Github, and to learn more about SSH connections in Compute Engine, including SSH key configuration and storage."]]],[]] -