本文档介绍了如何将应用配置为使用 SSH 和 OS Login 在两个虚拟机 (VM) 实例之间以编程方式进行连接。允许应用使用 SSH 对自动执行系统管理流程非常有用。
本指南中使用的所有代码示例都托管在 GoogleCloudPlatform/python-docs-samples GitHub 页面上。
准备工作
- 为服务账号设置 SSH。
- 在您的项目或以服务账号身份运行的虚拟机上设置 OS Login。
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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
-
After installing the Google Cloud CLI, sign in to the gcloud CLI with your federated identity and then initialize it by running the following command:
gcloud init
- Set a default region and zone.
- 导入 Google OS Login 库来构建客户端库,以便您可以使用 OS Login API 进行身份验证。
- 初始化 OS Login 客户端对象,以便应用可以使用 OS Login。
- 实现
create_ssh_key()
方法,以便为虚拟机的服务账号生成 SSH 密钥并将公钥添加到服务账号。 - 从 OS Login 库调用
get_login_profile()
方法以获取服务账号使用的 POSIX 用户名。 - 实现
run_ssh()
方法以执行远程 SSH 命令。 - 移除临时 SSH 密钥文件。
在虚拟机上,安装
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
从命令行接受变量。在此示例中,指示应用在项目中的另一个虚拟机上安装并运行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
:应用连接到的虚拟机的项目 ID。VM_NAME
:应用要连接的虚拟机的名称。ZONE
:应用连接到的虚拟机所在的可用区。
输出类似于以下内容:
⋮ ___________ It works! ----------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
- 下载并查看完整的代码示例。 完整的示例中包含了一个同时使用所有这些方法的小例子。您可以根据自己的需求随意下载、更改并运行该示例。
- 详细了解 SSH 连接在 Compute Engine 中的工作原理,包括 SSH 密钥配置和存储。
设置 SSH 应用
将您的应用设置为管理 SSH 密钥并启动与 Compute Engine 虚拟机的 SSH 连接。大体而言,应用应执行以下操作:
示例 SSH 应用
oslogin_service_account_ssh.py
示例应用演示了 SSH 应用可能的实现。在此示例中,该应用使用run_ssh()
方法在远程实例上执行命令并返回命令输出。运行 SSH 应用
在创建使用 SSH 的应用后,您可以按照与以下示例类似的过程运行该应用,该示例会安装并运行
oslogin_service_account_ssh.py
示例应用。您安装的库可能会不同,具体取决于应用使用的编程语言。或者,您可以编写一个导入并直接运行
oslogin_service_account_ssh.py
的应用。后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-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"]],["最后更新时间 (UTC):2025-07-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."]]],[]] -