# Recovering Lost EC2 Key Pair: A Step-by-Step Guide to Creating a New Key Pair

# Introduction:

Amazon Elastic Compute Cloud (EC2) is a powerful and flexible cloud computing service that allows users to run virtual servers in the cloud. EC2 instances are secured using key pairs, which consist of a public key that is stored on the instance and a private key that the user securely keeps. Losing access to the private key can be challenging, but fear not – Amazon Web Services (AWS) provides a straightforward process to recover from this predicament. In this blog post, we will guide you through recovering a lost EC2 key pair by creating a new one.

**Step1: Launch a New Temporary Instance:**

* In your AWS Management Console, create a new temporary instance.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708874962111/bc9666ac-71f0-4d71-9653-8fa75cb17f57.png align="center")
    
    **Step2: Create a New Key Pair:**
    
    * Generate a new key pair and give it a name during the creation process.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875011658/30e7bd3b-c81e-47ae-a9e1-d004417f66a8.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875022868/48057771-c629-4aa2-b644-4b40b37eca87.png align="center")
        
    
    **Step3: Stop the Old Instance:**
    
    * Navigate to your old instance ([blog.anupkafle.com.np](http://blog.anupkafle.com.np)) and stop it.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875113275/dab3f0be-8524-4058-aebd-613e78a44bc4.png align="center")
        
    
    **Step4: Go to your Volume:**
    
    * After the instance is stopped, go to storage, and click on the volume ID.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875418054/dc87458f-b887-4cc0-ba5e-2eeb1f1344f3.png align="center")
        
    
    **Step5: Rename the volume Volume (optional):**
    
    * Rename your volume so that we can recognize it easily.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875456796/d3720f6a-9417-429a-8988-bbffc6f4b6d8.png align="center")
        
    
    **Step6: Detach Old Volume:**
    
    * In the Volume, go to Actions, and detach it.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875519396/408ca674-4454-41c1-a603-7aae558b6d62.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875545998/d5d6731f-9ed0-400d-9800-ed7cf59d6724.png align="center")
        
    
    **Step7: Attach Volume to Temporary Instance:**
    
    * Attach the detached volume to your new temporary instance.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875555329/b11f3bb3-92ea-4ed6-98c1-ebf8452fe51f.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875669812/305d506d-ce97-4f33-afda-152640519d79.png align="center")
        
    
    **Step8: Connect to Temporary Instance:**
    
    * Utilize AWS Instance Connect or SSH from your terminal to connect to the temporary instance.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875583390/977cb1bd-d3f2-4d59-bd2e-9ea48685abec.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875593709/47f1f448-a616-4d39-86c9-26e3761740be.png align="center")
        
    
    **Step9: Prepare for Disk Operations:**
    
    * Create a directory:
        
        `mkdir -p /var/anupblog-disk`
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875631234/47069ef4-ba21-47fc-886a-ef2a5d58aa9a.png align="center")
        
    
    **Step10: Mount Old Disk:**
    
    * Mount the old disk to the temporary instance.
        
        ```bash
        mount -o nouuid /dev/xvdf1 /var/anupblog-disk
        ```
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875768710/27aaf3a0-d1a2-4880-b4a9-de4e3476d99d.png align="center")
        
    * Use `lsblk` to confirm the volume attachment.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875778046/83b72e63-4f8c-4b90-b531-b3be8f315807.png align="center")
        
    
    **Step11: Copy New Public Key to Mounted Disk:**
    
    * Copy the new public key:
        
        ```bash
        cat /home/ec2-user/.ssh/authorized_keys >> /var/anupblog-disk/home/ec2-user/.ssh/authorized_keys
        ```
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875813638/52a2661e-b3dd-4159-8c74-63e54cdacc15.png align="center")
    
    **Step12: Unmount the Disk:**
    
    * Safely unmount the disk:
        
        ```bash
        umount /var/anupblog-disk
        ```
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875841601/3b001027-36c1-49b0-bbf8-d99d3f1f011e.png align="center")
    
    **Step13: Detach Volume from Temporary Instance:**
    
    * In the AWS Console, detach the volume from the temporary instance.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875954051/70eedba7-97b1-4e95-b669-e99cb431d9fe.png align="center")
        
    
    **Step14: Attach Volume to Old Instance:**
    
    * Attach the volume to the old instance, ensuring the device name remains the same.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875968698/057732e0-3e8e-4e48-88ca-033d64164154.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708875975012/dc9cb76c-29fa-4916-9d50-435d39efc2bd.png align="center")
        
    
    **Step15: Start the Old Instance:**
    
    * Start your old instance.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708876015556/d86970e4-2f58-4934-bf8c-84abf3a53a5d.png align="center")
        
    
    **Step16: SSH into the Instance:**
    
    * Using the key created for the temporary instance, SSH into your old instance.
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708876028033/90e8a76f-6e00-4fa5-acbb-07ef305e3bda.png align="center")

# Conclusion:

Losing access to an EC2 instance due to a lost key pair can be a nerve-wracking experience, but AWS provides a clear and effective process for recovery. By following the step-by-step guide outlined in this blog post, you can create a new key pair, associate it with your EC2 instance, and regain control of your virtual server. Remember to maintain secure practices for managing and storing your key pairs to prevent future issues.
