Google Cloud VM doesn't allow public ssh connections

I am running a Compute Engine VM in Google Cloud.

When I set up the instance, I pasted a public key into the Security section. Now that the instance is running, I can see that public key on its page in Google Cloud console (so it was saved and is associated)

However, I cannot ssh into the instance from my terminal. I opened Google's ssh client in the browser and saw that the ~/.ssh/authorized_keys file does not have my public key in it.

I manually edited ~/.ssh/authorized_keys to add a line with the same public key that is shown in the Google Cloud console on this instance, and found that I could connect from my terminal. But as soon as I terminate Google's in-browser ssh session, ~/.ssh/authorized_keys is reset and public ssh connections are blocked.

How do I allow public ssh connections to this instance?


Since you mention that ~/.ssh/authorized_keys file does not have your public key in it, this might be related to OS login activated at instance or project level.

Follow this guide to disable OS Login if enabled, as explained in the warning for this step:

Caution: Enabling OS Login on instances disables metadata-based SSH key configurations on those instances. Disabling OS Login restores SSH keys that you have configured in project or instance metadata.

If this is not the problem, you should collect and share your logs:

  1. Go to Compute Engine -> VM instances -> click on NAME_OF_YOUR_VM -> at the VM instance details find section Logs and click on Serial port 1 (console)

  2. Reboot your VM instance (if possible).

  3. Check full boot log for any errors or/and warnings.

  4. If you found errors/warning related to disk space you can try to resize it accordingly to the documentation Resizing a zonal persistent disk or follow this article Recovering an inaccessible instance or a full boot disk.

  5. If you require more help, share the full log by using pastebin.com.