Problem Description
When trying to connect to an Oracle Cloud compute instance, the SSH key in use was corrupted or lost, making it impossible to connect to the instance.
The error message is as follows:
Permission denied (publickey).
Solution
Detach the boot volume from the instance, attach it to a new instance, add a new SSH key, and then reattach the boot volume to the original instance.
Note: The screenshots in this article are no longer available (the original image hosting service cdn.staticaly.com has been discontinued), but the written steps are complete and usable. The Oracle Cloud console interface may have changed, so please refer to the actual interface.
Detailed Steps
1. Stop the inaccessible instance
From the console, go to the instance details page and click the Stop button.
2. Detach the boot volume
At the bottom of the instance details page, locate the boot volume, find the corresponding boot volume, click the three dots, and select Detach boot volume.
3. Attach the boot volume to an accessible instance
Note: The instance to which the boot volume is attached must be accessible (connectable), as you will connect to this instance later to mount the boot volume.
In the console, find the accessible instance, locate the attached block storage volumes, click Attach block storage volume, select the boot volume you just detached, and click Attach.
4. Use iSCSI commands to mount the boot volume to the running instance
SSH into the accessible instance, then use iSCSI commands to mount the boot volume to the running instance.
The iSCSI commands can be found on the attached block storage volume page: locate the boot volume you just attached, click the three dots, and you will see the iSCSI commands.
Expand the Linux tab, copy the Linux iSCSI connection commands, and execute them on the already connected instance.
5. Modify the authorized_keys file
sudo su -
fdisk -l
mount /dev/sdb1 /mnt
sudo vi /home/opc/.ssh/authorized_keys
sudo vi /home/ubuntu/.ssh/authorized_keys
umount /mnt
6. Use iSCSI commands to unmount and detach the boot volume
Copy the Linux iSCSI disconnection commands and execute them on the already connected instance.
Use the fdisk -l command to verify that the boot volume has been unmounted. If it has, you can detach the boot volume.
7. Attach the boot volume back to the original instance
In the console, find the original instance, locate the attached block storage volumes, click Attach block storage volume, select the boot volume you just detached, and click Attach.
Wait for the boot volume to finish attaching, then click the Start button on the instance to start it. You can now log in to the instance using the new SSH key.
