“You lost the SSH private key to an instance. How to access it again? method 02”.
Long version #
Losing an SSH key can be problematic and troubling. However, on OCI, it is simple to add another key to the opc user’s authorized_keys file. Follow the instructions below to regain access.
Jump to TLDR for instructions.
Be aware of the following:
- a temporary instance is required
- use an instance with a different Linux version. For example, create aux OL7, if the the no key instance is OL8. OL8 and OL9 uses LVM. The UUID of the Logical Volume is the same in most images and you would need to make adjustments.
- if the instance was created with no ssh key, more steps are required.
TLDR #
- Stop the instance with lost key (lostkey from now on)
- Detach nokey’s boot volume
- Go to the auxiliary instance (aux from now on)
- Under aux instance block volumes, attach nokey boot volume.
- SSH to aux instance
- Mount the root partition (lsblk -f will show the partitions and UUID)
- Add the key to the .authorized_keys
- Umount the volume from the nokey instance
- Re-attach the nokey volume to the nokey instance
- Start the nokey instance
You should now be able to SSH into the instance again