フューチャー技術ブログ

GCP連載#8 gcloud compute sshじゃなくてsshコマンドを使いたいあなたへ

はじめに

GCP連載企画の8日目です。

いきなりですが、 gcloud compute sshコマンドは便利ですよね。

ssh先のGCEインスタンスとクライアント間で、キーペア(秘密鍵/公開鍵)の自動生成 -> GCEインスタンスへの鍵配置を自動で行ってくれて、ユーザーは鍵の存在を意識することなくsshできるコマンドですが、gcloud compute sshコマンドじゃなくて、素のsshコマンドでGCEインスタンスにsshしたい、という方もいらっしゃるのではないでしょうか。

例えば、以下のケース(あれ、少ない…)

  • 使い慣れたsshクライアント(TeraTerm等)を使ってsshしたい
  • VScodeのsshプラグイン(便利ですよね)を使ってリモートサーバー上のファイルを編集したい

そんな方は、きっと ~/.ssh 配下に生成された秘密鍵( google_compute_engine) とホスト情報を ~/.ssh/config に手動で追加されていることでしょう。

ただ、対象のホストが多くなると都度configを更新するのは大変ですよね。実はこのconfigへの追加、 gcloud compute config-sshコマンドを使えば 自動でやってくれますよ! (本題)

やってみる

まずは、 gcloud compute sshコマンドを使って test-instanceにsshしてみましょう。
自動でキーペアを生成してくれていることがわかります。

❯ gcloud compute ssh test-instance  --project test-project --zone asia-northeast1-a
WARNING: The public SSH key file for gcloud does not exist.
WARNING: The private SSH key file for gcloud does not exist.
WARNING: You do not have an SSH key for gcloud.
WARNING: SSH keygen will be executed to generate a key.
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/hoge/.ssh/google_compute_engine.
Your public key has been saved in /Users/hoge/.ssh/google_compute_engine.pub.
The key fingerprint is:
SHA256:PQXtls0bpoISsn+/u2vDpqMKBORO5VEFgEGqyJ4klcM hoge@MAC00011
The key's randomart image is:
+---[RSA 2048]----+
|.+o+ooo. .. |
|+oo.. .. |
|ooE. ..+ |
|*o .. . . .+ = |
|++. o .S.o. o o |
|+.. . . . ... . |
| o. . . . . |
| . . o = |
| ...o.*** |
+----[SHA256]-----+
Updating project ssh metadata...⠹Updated [https://www.googleapis.com/compute/v1/projects/test-project].
Updating project ssh metadata...done.
Waiting for SSH key to propagate.
Warning: Permanently added 'compute.3504985360589690116' (ECDSA) to the list of known hosts.
Linux test-instance 4.9.0-12-amd64 #1 SMP Debian 4.9.210-1 (2020-01-20) 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.
hoge@test-instance:~$

ローカルの ~/.ssh配下にキーペア( google_compute_engine, google_compute_engine.pub)が生成されていますね。

ls ~/.ssh
config google_compute_engine google_compute_engine.pub google_compute_known_hosts

ここで本題の gcloud compute config-ssh の出番です。 実行してみましょう。


❯ gcloud compute config-ssh --project test-project
You should now be able to use ssh/scp with your instances.
For example, try running:

$ ssh test-instance.asia-northeast1-a.test-project

~/.ssh/configtest-instanceが追加されていますね!

cat ~/.ssh/config

...

# Google Compute Engine Section
#
# The following has been auto-generated by "gcloud compute config-ssh"
# to make accessing your Google Compute Engine virtual machines easier.
#
# To remove this blob, run:
#
# gcloud compute config-ssh --remove
#
# You can also manually remove this blob by deleting everything from
# here until the comment that contains the string "End of Google Compute
# Engine Section".
#
# You should not hand-edit this section, unless you are deleting it.
#
Host test-instance.asia-northeast1-a.test-project
HostName 35.200.9.109
IdentityFile /Users/hoge/.ssh/google_compute_engine
UserKnownHostsFile=/Users/hoge/.ssh/google_compute_known_hosts
HostKeyAlias=compute.3504985360589690116
IdentitiesOnly=yes
CheckHostIP=no

# End of Google Compute Engine Section

あとは、使い慣れた sshコマンドでアクセスできますよ。

❯ ssh test-instance.asia-northeast1-a.test-project
Last login: Sun Feb 16 11:58:05 2020 from 126.228.201.100
hoge@test-instance:~$

これで快適なsshライフ??が送れますね

GCP連載企画の8日目でした。次は村瀬さんの初めてのGCP 画像AI(Vision API)をさわってみたです。