How to Recover a Linux Cloud Instance with a Lost or Corrupted SSH Key
If you’ve ever tried to SSH into your Linux cloud instance only to be met with the dreaded Permission Denied (publickey)
error, you know how frustrating it can be. But don’t worry—if you’re seeing this error, you’re in the right place. I’m going to show you how to regain access to your instance, step by step. And for those who prefer a visual guide, check out the linked YouTube video where we demonstrate this on an Oracle Cloud Ubuntu instance.
Step 1: Setting Up a Temporary Instance
First, create a new temporary Linux instance in the same region and availability domain as your locked instance. This temporary instance will be your recovery tool, helping you access and fix the issue with your original instance.
Step 2: Detaching and Reattaching the Boot Volume
Once your temporary instance is ready, head to your cloud provider’s console. Locate the boot volume of your locked instance—the disk that contains all your system files. Detach this boot volume from the locked instance. Think of this like disconnecting a drive from a computer that you no longer have access to.
Now, attach the detached boot volume to your temporary instance, allowing you to access and modify its contents. Make sure to attach it in read/write mode, so you can make the necessary changes.
Step 3: Accessing the Disk
After attaching the boot volume, SSH into the temporary instance using a valid key. With access to the temporary instance, you’ll mount the disk, which allows you to browse its contents just like you would with a drive on your computer.
Navigate to the directory where the SSH keys are stored. This is the critical part where you’ll fix the key issue.
Step 4: Replacing the SSH Key
Once inside the right folder, you’ll find the file that holds the old, non-functional public key. Open this file and replace the old key with your new, working public key. Save your changes—this step is akin to updating a password to regain access.
Step 5: Reattaching the Boot Volume to the Original Instance
With the new key in place, unmount the disk from the temporary instance. Then, return to your cloud provider’s console, detach the boot volume from the temporary instance, and reattach it to your original locked instance. This is like reconnecting the drive to the computer that was previously inaccessible.
Step 6: Rebooting and Logging In
Finally, reboot your original instance. Now, with the updated key, you should be able to SSH into your instance without encountering the Permission Denied (publickey)
error.
Encountering the Permission Denied (publickey)
error doesn’t mean all is lost. By following these steps, you can quickly regain access to your Linux instance. For a detailed visual walkthrough, check out my YouTube video.