• notice
  • Congratulations on the launch of the Sought Tech site

How do I connect to an Amazon EC2 instance if I lose my SSH key pair after the initial boot?

I want to connect to my Amazon Elastic Compute Cloud (Amazon EC2) instance, but I've lost my SSH key pair. How to connect to the instance?


important hint:

Methods 1, 2, and 3 require stopping and starting the instance. However, the following points should be noted:

  • If your instance is backed by instance store or has instance store volumes that contain data, the data will be lost when the instance is stopped. For more information, see Determining the Root Device Type of an Instance . Be sure to back up any data you want to keep on the instance store volume .

  • Stopping and restarting an instance changes the instance's public IP address. When routing external traffic to your instance, it's a best practice to use  Elastic IP addresses instead of public IP addresses.

Method 1: Enter User Data

1. Create a new key pair .

2. If you created the private key in the Amazon EC2 console, retrieve the key pair from the public key .

3. Open the  Amazon EC2 console .

4.     Stop your instance .

5. Select Actions, Instance settings, Edit user data.

6. Copy the following script into the Edit User Data dialog:

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

- [users-groups, once]
 - name: username
   - PublicKeypair

Replace username with your username, such as ec2-user. You can enter a default username, or enter a custom username if you have previously set up a custom username for the instance. For a list of default usernames, see General prerequisites for connecting to an instance .

Replace PublicKeypair with the public key retrieved in step 2. Make sure to enter the full public key starting with ssh-rsa.

7. Select Save.

8.     Launch your instance .

9. After the cloud-init phase is complete, verify that the public key has been replaced .

Important: Since the script contains a key pair, delete the script in the user data field.

10.     Stop your instance .

11. Select Actions, Instance Settings, Edit user data.

12. Delete all text in the Edit user data dialog box, and then choose Save.

13.     Launch your instance .




Technical otaku

Sought technology together

Related Topic


Leave a Reply