Google Cloudで作成したVMインスタンスを操作するにあたり、ブラウザからSSHで接続できますが、
レスポンスに難有りだったりするのでMacのターミナルを使ってアクセスしたいと思います。
その手順のメモ。
既にGCPが使える状態で、VMインスタンスも作成済み、更にPythonもインストールされている前提です。
1. Google Cloud SDKのインストール
Google Cloud CLIをインストールする
こちらのページから自身の環境に合わせてgcloud CLIのパッケージをダウンロードします。
ダウンロードしたら必要に応じて展開したフォルダを移動させてあげましょう。
適当な場所にフォルダを移動させたら、そのフォルダに移動したのち、以下を実行します。
% ./google-cloud-sdk/install.sh
インストール直後はPATHが通っていないはずなので、PATHを通してあげましょう。
% vi ~/.zshrc
記載する内容は
export PATH="/(google-cloud-sdkフォルダをおいた場所)/google-cloud-sdk/bin:$PATH"
です。入力して保存したら
% source ~/.zshrc
で反映させます(ターミナルの再起動でもOK)
PATHを通したら使えるか確認してみます。
% gcloud -v
Google Cloud SDK 458.0.1
bq 2.0.101
core 2023.12.20
gcloud-crc32c 1.0.0
gsutil 5.27
このような感じで表示されればインストール完了です。
2. gcloudの初期化
gcloud CLIのインストールが完了したら、gcloudの初期化を行います。
初期化のコマンドは以下の通り
% gcloud init
※私の環境の場合、以前同端末で使用した別のGCP設定が残っていたのでinit開始直後の表示が若干違います。
とはいえそれ以外は基本的に同じです
% gcloud init
Welcome! This command will take you through the configuration of gcloud.
Settings from your current configuration [default] are:
compute:
region: us-west1
zone: us-west1-a
core:
account: xxxxx@xyz.com
disable_usage_reporting: 'True'
project: ⚪︎⚪︎⚪︎⚪︎⚪︎-xxxxx
Pick configuration to use:
[1] Re-initialize this configuration [default] with new settings
[2] Create a new configuration
Please enter your numeric choice: 1
Your current configuration has been set to: [default]
You can skip diagnostics next time by using the following flag:
gcloud init --skip-diagnostics
Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).
Choose the account you would like to use to perform operations for this
configuration:
[1] xxxxx@xyz.com
[2] Log in with a new account
Please enter your numeric choice: 2
Your browser has been opened to visit:
こちらは既に設定されている・プロジェクトをそのまま使うか否かの確認です。
今回は別アカウント・プロジェクトのGCPを使用するので以上の通りで進めていきます。
上記まで進むとブラウザが起動してGoogleアカウントの認証になります。
使用するアカウントでログインして認証しましょう。
Updates are available for some Google Cloud CLI components. To install them,
please run:
$ gcloud components update
You are logged in as: [nakayama.yosuke@gmail.com].
Pick cloud project to use:
[1] myprofile-ssss
[2] mysampleprojectXXX
[3] ⚪︎⚪︎⚪︎⚪︎⚪︎-xxxx
[4] ◻︎◻︎◻︎◻︎◻︎-yyyyy
[5] △△△△△△-zzzz
[6] ▽▽▽▽▽▽-vvvv
[7] Enter a project ID
[8] Create a new project
Please enter numeric choice or text value (must exactly match list item): 3
Your current project has been set to: [◻︎◻︎◻︎◻︎◻︎-yyyyy].
既にGCPでプロジェクトが作られていれば複数のプロジェクトが表示されるので使うプロジェクトの番号を選択、もしくは新しくプロジェクトを作ります。
今回は既存のものを使います。
Do you want to configure a default Compute Region and Zone? (Y/n)? n
Your Google Cloud SDK is configured and ready to use!
* Commands that require authentication will use aaa.bbb@gmail.com by default
* Commands will reference project `◻︎◻︎◻︎◻︎◻︎-yyyyy` by default
Run `gcloud help config` to learn how to change individual settings
This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.
Some things to try next:
* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting
* Run `gcloud cheat-sheet` to see a roster of go-to `gcloud` commands.
リージョンの設定確認をされますが、してもしなくても問題はなさそう。
今回はCompute EngineのVMインスタンスで既にリージョン設定してあるので飛ばします。
設定する場合はYをクリックすると、50個(!)くらいのリージョンが表示されるようなので、そこから任意のものを選択すれば良いようです。
認証情報は以下で確認できます。
% gcloud auth list
Credentialed Accounts
ACTIVE ACCOUNT
* aaa.bbb@gmail.com
xxxxx@xyz.com
To set the active account, run:
$ gcloud config set account `ACCOUNT`
元のアカウントは上書きされるわけではなく、今回のアカウントが追加されたとうことで2つアカウントが表示されていますが、問題なく認証されているのがわかります。
これで初期化は完了です。
3. SSH公開鍵の作成と登録
ターミナルからSSHで接続するためにはSSH公開鍵をVMインスタンスに登録する必要があります。
VMインスタンスを作成した時点で、ブラウザからSSH接続するための公開鍵が登録されているので、それに追加で登録します。
まずはターミナルで公開鍵を作成します。
% ssh-keygen -t rsa -b 4096 -C "(sshでログインしたいユーザ名)"
Generating public/private rsa key pair.
Enter file in which to save the key (/User/xxx/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /User/xxx/.ssh/id_rsa.
Your public key has been saved in /User/xxx/.ssh/id_rsa.pub
sshでログインしたいユーザ名はGCPのアカウントと合わせるのが一般的ですが、別途していしても問題ありません。
Enter file in which to save the key (/User/xxx/.ssh/id_rsa):
ここは認証鍵ファイルの書き出し先を指定します。デフォルトのまま(/User/xxx/.ssh/id_rsa)で問題ありません。
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
この部分はSSHでのアクセス時のパスワードを入力します(同じもの2回。任意でOK)
2回目のパスワードを入力してエンターキーを押せぱ指定した場所に2つのファイル(id_rsa、id_rsa.pub)が作成されます。
VMインスタンスに登録する公開鍵はid_rsa.pubの方なので、こちらのデータの中身をコピーします。
とりあえずcatコマンドなどで中身を確認しましょう。
% cat ~/.ssh/id_rsa.pub
ssh-rsaで始まりssh-keygenコマンド実行時に入力したユーザ名で終わる文字の羅列が表示されるので、これをすべてコピーします。
続いてGCPでSSHでアクセスしたいVMインスタンスを編集します。
編集画面からSSH認証鍵の項目の欄で、「項目追加」をクリックしてコピーしたデータを貼り付けます。
画像は認証鍵を追加した後の状態です。
おそらくSSH認証鍵3が追加されると思うので、これで編集を完了します。
4. SSHでの接続
認証鍵を追加したらターミナルから接続してみましょう。
ssh ユーザ名@インスタンス外部IPアドレス -i (/User/xxx/.ssh/id_rsa)
インスタンス外部IPアドレスはGCPのCompute EngineのVMインスタンス一覧から確認できます。
上記コマンドを実行すると先ほど決めたパスワードを求められます
Enter passphrase for key '.ssh/id_rsa':
パスワードを入力すればVMインスタンスにログインできると思います。
Linux webserver 5.10.0-26-cloud-amd64 #1 SMP Debian 5.10.197-1 (2023-09-29) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Jan 10 11:46:11 2024 from 106.73.26.225
xxx@webserver:~$
おまけ
ログインするのにユーザー名、アドレス、ファイルパス(設定によってはポート番号)を入力していくのは面倒大変なので、ログインを簡略化します。
.ssh以下にconfigファイルを作成します。
% vi ~/.ssh/config
configの内容を以下の通りに入力し、保存します。
HOST (任意の名前)
HostName インスタンス外部IPアドレス
User ユーザ名
Port ポート番号(デフォルトなら22)
IdentityFile (id_rsaファイルの場所)
このファイルを作成すると以下のコマンドでログインできるようになります。
% ssh (ファイルに入力した任意の名前)
これを実行するとパスワードが聞かれるので入力するとログインできます。
参考
こちらを大変参考にさせていただきました。
Google Cloud PlatformにMacからsshする方法