Search code examples
oracle-cloud-infrastructure

How do you change the public key of a Oracle Cloud Instance?


I accidentally deleted my public and private key and had to generate new SSH keys due to not being able to restore the keys (and not having a backup anywhere). How do I change the public SSH key then of my Oracle Cloud instance?

Terminating the instance and remaking it isn't an option, and I've tried looking online but wasn't able to find much. Any help would be appreciated.

Thanks


Solution

  • Some background

    Found a solution! Just so people are aware, there are methods online that involve connecting to the machine via VNC, but for me personally it felt very trial-an-error, when pressing buttons at the wrong time, and it ended up not working properly (VNC didn't display recovery mode for me, just a blank screen after selecting it).

    Summary

    This guide involves: Creating another machine (as incl. in free tier anyway), detaching the boot volume drive from the machine and attaching it to the machine just created, to do editing to change the keys over, then attaching the drive back up.

    Create another VPS (Oracle have them incl. > free tier)

    I deleted one of my other VPS' in the Oracle panel (that was a free machine - as I didn't need it and wasn't using it) and created it again anew (I made sure to delete the old boot volumes before continuing).

    (This solution is assuming your using Ubuntu 20.04, but this will probably work for other OS's as well)

    Basically from there,

    1. I powered off the machine I wanted to change my SSH key of.
    2. After fully being powered off, just detach the boot volume from the VPS, and attached it as a block volume to the machine just created.

    Login to the machine via SSH, and run the connection commands by hitting the three dots (image below) and viewing the connection commands, to connect the drive up. Mounting the drive via Oracle Cloud Panel

    Editing files on the drive & mounting process

    Then by running blkid (or sudo fdisk -l for a more friendly view) you're able to see what drives are available for mounting. So then you just make a folder and simply type: sudo mount [drive path e.g. /dev/sdb] [folder path e.g. ./drive] Edit the file at /home/ubuntu/.ssh/authorized_keys, or however your machine is configured (Oracle by default disallows root, but if you've edited your configuration it's up to your end).

    Then, simply go to the relevant path to be able to unmount the drive, umount [folder path e.g. ./drive] Run the disconnect commands for the drive from the panel.

    Then, simply detach the drive from your other machine and reattach it back to the original machine. Wait till it's fully attached and then start the machine again.