このドキュメントでは、SSH と OS Login を使用して 2 つの仮想マシン(VM)インスタンスをプログラムで接続するアプリを構成する方法について説明します。アプリが SSH を使用できるようになると、システム管理プロセスの自動化に役立ちます。
このガイドで使用するすべてのコードサンプルは、GitHub の GoogleCloudPlatform/python-docs-samples ページに置かれています。
準備
- サービス アカウント用に SSH を設定します。
- プロジェクトか、サービス アカウントとして実行する VM で OS Login を設定します。
-
まだ設定していない場合は、認証を設定します。認証とは、 Trusted Cloud by S3NS サービスと API にアクセスするために ID を確認するプロセスです。ローカル開発環境からコードまたはサンプルを実行するには、次のいずれかのオプションを選択して 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 クライアント オブジェクトを初期化します。
- VM のサービス アカウントの SSH 認証鍵を生成し、サービス アカウントに公開鍵を追加する
create_ssh_key()
メソッドを実装します。 - サービス アカウントが使用する POSIX ユーザー名を取得するために、OS Login ライブラリから
get_login_profile()
メソッドを呼び出します。 - リモート SSH コマンドを実行する
run_ssh()
メソッドを実装します。 - 一時 SSH 認証鍵ファイルを削除します。
SSH アプリをホストする VM に接続します。
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 | || ||
- 完全なコードサンプルをダウンロードして確認する。完全なサンプルには、これらすべてのメソッドを同時に使用する一例が含まれています。ご自由にダウンロードして、ニーズに合わせて変更し、実行してください。
- SSH 認証鍵の構成やストレージなど、Compute Engine での SSH 接続の動作の詳細を確認する。
SSH アプリを設定する
SSH 認証鍵を管理し、Compute Engine VM への 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 Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-26 UTC。
[[["わかりやすい","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-07-26 UTC。"],[[["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."]]],[]] -