Ubuntu – Cannot ssh to ubuntu gcp instance

google-cloud-platformgoogle-compute-enginesshssh-keysUbuntu

I migrated the vm from libvirt to Google Cloud Platform using Cloudendure. The initial sync is complete and is in Data Replication stage from over a week. Once the VM is launched using test mode and try to putty using ssh it throws
Connection Refused exited with error code 255.

The firewall rule in GCP and the machine allows port 22 for incoming connections.
SSH key is also updated properly in metadata section and also in authorized_keys in the machine.

Steps tried:

  1. Followed this article to install Linux Guest OS.
  2. Generated SSH key using ssh keygen -t rsa -C "" in gcloud shell.

I cannot ssh into the linux environment. Appreciate the help

Operating System: Ubuntu 18.04 LTS x64

Best Answer

This is a workaround that will help you to fix your issue and you have to follow these steps:

  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 useradd -G sudo USERNAME echo 'USERNAME:PASSWORD' | chpasswd

  1. 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.
  2. Click on 'Connect to serial port' in the page.
  3. 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 USERNAME and PASSWORD you provided.

Then inside the instance you need to fetch which is not working by Validate the Guest Environment :

First: look in your serial console if these line below are listed :

Started Google Compute Engine Accounts Daemon
Started Google Compute Engine IP Forwarding Daemon
Started Google Compute Engine Clock Skew Daemon
Started Google Compute Engine Instance Setup
Started Google Compute Engine Startup Scripts
Started Google Compute Engine Shutdown Scripts
Started Google Compute Engine Network Setup

Second: Verify if the package for the guest Environment is installed run the command in your serial output

apt list --installed | grep google-compute

It should list the below line :

google-compute-engine
google-compute-engine-oslogin
python-google-compute-engine
python3-google-compute-engine

Third:you need to verify if all the services for the guest environment are running by running this command :

sudo systemctl list-unit-files | grep google | grep enabled

It should list the below line :

google-accounts-daemon.service      enabled
google-ip-forwarding-daemon.service enabled
google-clock-skew-daemon.service    enabled
google-instance-setup.service       enabled
google-shutdown-scripts.service     enabled
google-startup-scripts.service      enabled
google-network-setup.service        enabled

If some package on the Guest Environment is missing or not running tthen you should install it following the documentation.