Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Compute instance SSH key from metadata not added

I have been successfully adding SSH keys to newly spawned instances for a while, but all of a sudden it just won't work. They key isn't added to the target account (and the account isn't created if it doesn't exist). It still works fine for one of my projects, but it doesn't on the other which contains an adapted version of the code from the first. The steps both codes go through are as follows:

  1. Create a disk from a snapshot.
  2. Generate an RSA key pair.
  3. Set the ssh-keys property in the metadata to the public key generated in the previous step in the format understood by Google Cloud (<username>:ssh-rsa <key> <username>).
  4. Create the instance with the disk created in the first step and the metadata created in the third.

When I check out the instance data on the Google Cloud Console, it has the correct key assigned to the correct user in the SSH Keys section. However, when I SSH into the instance, the key hasn't been added to the authorized_keys file for that user. Just in case, I tried recreating the snapshot without that user so that the user can be created on the fly (which should work), but in that case the user isn't created.

In the end, Google Cloud just acts as if the SSH key isn't in the metadata, yet it is. I came accross this answer but can't seem to understand this part:

As a direct consequence of that, the GCE platform takes care of placing/removing your ssh keys in the instance (...) ONLY when you do not specify your own keys (at instance creation or later). If you do, the GCE platform will consider the ssh key management as manual, and nothing will be kept in sync with the metadata store.

I don't see anything I'm doing that could be triggering it into manual mode, yet there obviously is an issue somewhere.


I just tried running the same process from an older snapshot and it worked flawlessly. I'll investigate further to see what causes this.

like image 411
PLPeeters Avatar asked Aug 15 '26 12:08

PLPeeters


1 Answers

You need to have the latest version of the Guest Tools. Here is an example to install the latest version on Ubuntu:

$ sudo apt-add-repository universe

$ sudo apt install -y python-google-compute-engine python3-google-compute-engine google-compute-engine-oslogin gce-compute-image-packages

You can read here more details and other OS.

like image 108
Pol Arroyo Avatar answered Aug 18 '26 03:08

Pol Arroyo