Connect to an Amazon EC2 instance without an original SSH key pair

If you need to connect to an Amazon EC2 instance without an original SSH key pair on a new device, you can follow these steps:

  1. Create a new key pair or use existing key.
    • If you create a private key in the Amazon EC2 console, retrieve the public key of the key pair.
  2. Open the Amazon EC2 console.
  3. Stop the instance.
  4. Choose Actions, Instance Settings > Edit User Data.
  5. Copy the following script into the Edit User Data dialog box:
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"

#cloud-config
cloud_final_modules:
- [users-groups, once]
users:
  - name: username
    ssh-authorized-keys:
    - ssh-rsa xxxxxxxxxxx (your public key)

Replace username with your username, such as ec2-user. You can enter the default username or a custom username (if previously set for the instance). For a list of default usernames, see Getting Instance-Related Information.

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

  1. Choose Save.
  2. Start your instance.
  3. After the cloud-init phase is complete, confirm that the public key has been replaced.

Important: Since the script contains a key pair, remove the script from the user data field.

  1. Stop the instance.
  2. Choose Actions, Instance Settings, Edit User Data.
  3. Delete all text in the Edit User Data dialog box, then choose Save.
  4. Start your instance.

Reference

Related Posts

AWS Lambda 從零到精通:完整實戰指南 2025

第一章:AWS Lambda 簡介 1. 什麼是 Serve…

發表迴響

%d 位部落客按了讚: