Google Compute Engine && VS Code でのremote ssh環境構築

Google Compute Engineを用いたVMインスタンスに対して、remote ssh環境構築する際の、初期インストール・設定までを備忘録的に記します。

Google Compute EngineのVMインスタンスは以下立ち上がっている前提とします。

画像1

gcloudコマンドのインストール

ssh接続情報の更新において、 gcloud compute config-ssh が便利です(と、@_moricho_ さんに教えていただきました)。そのために gcloud を入れましょう。

MacOSの場合は、対話型インストールと言う方法が用いられます。

$ curl https://sdk.cloud.google.com | bash
$ exec -l $SHELL
$ gcloud init

gcloud init 実行時には、Google Cloud SDK 認証として、Googleアカウントログインが求められます。その後、defaultで指定するプロジェクトおよびリージョンを指定して終了(日本だと、 asia-northeast1-b  かしらね)

gcloud compute config-ssh

こちらの実行で、ローカル端末でのSSH鍵の作成・ssh configのupdateまでしてくれ、指定のalias名でsshできるようになります。

$ gcloud compute config-ssh

WARNING: The private SSH key file for gcloud does not exist.
WARNING: The public 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/hgsgtk/.ssh/google_compute_engine.
Your public key has been saved in /Users/hgsgtk/.ssh/google_compute_engine.pub.
The key fingerprint is:
SHA256:hogehoge hgsgtk@host
The key's randomart image is:
+---[RSA 2048]----+
hoge
+----[SHA256]-----+

Updating project ssh metadata...⠛Updated [https://www.googleapis.com/compute/v1/projects/your-project].
Updating project ssh metadata...done.
You should now be able to use ssh/scp with your instances.
For example, try running:

 $ ssh your-config

Visual Studio Codeでのremote ssh用意

使用するのは、Microsoft製の Remote - SSH

画像2

このPluginをインストールすると、サイドバーにRemote SSHのタブが現れ ssh config のリストを示してくれます。

画像3

ここから、実際に remote ssh でのファイル変更操作を VS Codeでできるようになる

Unable to watch for file changes in this large workspace.

対象のディレクトリが大きい場合は、 "Unable to watch for file changes in this large workspace." と言われる。その場合は以下の案内に従って操作する。支障がある場合は以下のURLを起点に対象のOSに沿って解決する。


この記事が気に入ったらサポートをしてみませんか?