The easiest way is to add the keys when you create the machine. But if you forgot, it just takes 1 line. Before that, check if there are any keys associated with the machine:
gcloud compute instances describe instance-1
If this is a new VM without keys, the metadata field should be empty.
Then, add your keys:
gcloud compute instances add-metadata instance-1 --metadata-from-file ssh-keys=./.ssh/id_ed25519.pub
Now you can connect to the external IP of the machine:
ssh 34.139.000.00
Here, I’m assuming you have the same username in your local machine and the VM, and that your SSH key is loaded in the SSH agent. If you have a different username, you should use the USERNAME:SSH_KEY
format as described in the GCP documentation.
You can double-check by running the describe
command again, the output should an ssh-keys
item in the metadata
field.