I have instance in Google Cloud platform.

I can not connect to my instance because I accidentally removed the .ssh/authorized/keys file in my instance.

In addition to that, I connected using Google Serial connect but as a normal user so that user I has no rights to change ssh configuration folders or create authorized_keys again.

I don't know root or main user password because I was connecting to the server using private key. How can I connect to my server using ssh again?


You can connect to your VM using the 'SSH' button in the GUI Console next to the instance in the list of instances, which will open a browser window and a terminal session to the instance.

If you would like to connect via SSH client on the command-line, you can use gcloud tool.

'gcloud compute ssh example-instance'

Please see this link for more information.

It will prompt you to create SSH keys and then connect to the instance.

Another option is to manually add the SSH keys to the instance's metadata as described in this link which you can do via gcloud or manually via Google Cloud console.

The default ssh authentication for GCE instance is public key authentication and not password based. Moreover, for security reasons, most of the standard Google images do not provide the ability to connect directly as root. The instance creator and any users that were added using the metadata sshKeys value are automatically administrators to the account, with the ability to run sudo without requiring a password. Although it is not recommended, you need to create a startup script which will reset your root password which you can then use.

  1. Go to the VM instances page in Google Cloud Platform console.
  2. Click on the instance for which you want to add a startup script.
  3. Click the Edit button at the top of the page.
  4. Click on ‘Enable connecting to serial ports’
  5. Under Custom metadata, click Add item.
  6. Set 'Key' to 'startup-script' and set 'Value' to this script: #! /bin/bash echo 'root:PASSWORD' | chpasswd
  7. Click Save and then click RESET on the top of the page. You might need to wait for some time for the instance to reboot.
  8. Click on 'Connect to serial port' in the page.
  9. In the new window, you might need to wait a bit and press on Enter of your keyboard once; then, you should see the login prompt.
  10. Login using the root and PASSWORD you provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.