How can I access a VM when the SSH daemon no longer works?

If you have a running virtual machine, but have lost the access via SSH to it, you are in general not able to access it anymore. The cloud images we use don't provision a user with a password, so you can't even fix things via the console. However, there is a workaround:

  1. Create a Snapshot of the VM
  2. Configure a new VM to launch from that Snapshot
  3. Setup specific Cloud-Init configuration to setup a new user in the VM
  4. Boot the copied VM
  5. Profit

Create a Snapshot of the VM

Go to the list of your running instances, and select the action Create Snapshot

Give the snapshot a name and wait for the snapshotting to finish. Your Snapshot will show up in the Images & Snapshots tab

Configure a new VM to launch from the Snapshot

When the snapshotting is done, you can Launch a new VM from that snapshot. Configure the new VM as you had the original VM. 

Setup specific Cloud-Init configuration to setup a new user in the VM

In the Post-Creation tab, enter a YAML file that looks like this:

#cloud-config
users:
- default
- name: joe
gecos: Joe Doe
primary-group: users
groups: sudo,adm
passwd: $1$XXXXXXXXXXXXXXX0
lock-passwd: false

The passwd is the hashed value of the password you want the user to have. It is easiest if you copy that value from an existing /etc/shadow file where that user with the password already exists.

 

Boot the copied VM

Now Launch the VM. You can now use the console through the GUI to access the machine and fix whatever needs to be fixed.

Profit

You can now terminate the first VM, as it is no longer needed (and will not be accessible in the future) and use the new VM instead.