How to add a new key pair to your exisitng AWS ec2 Instances

To connect Amazon Ec2 Instances we need a Private Key generated while creating a particular Instance. In case, anyhow you deleted that key pair from your account or for security reasons you want to attach a new key to your Instance, then here is the step-by-step tutorial to perform the same.

Replace a Lost Key Pair for Amazon ec2 Linux Instance with a new one

Let’s learn the steps to recover our AWS ec2 Instance to access it remotely by replacing our lost Key with a new Private key pair.

1. Go to ec2 Dashboard

Once you log in to your AWS account you will see the AWS Management Console. On the left top side, click on the Service drop Menu to select EC2.

Login to Ec2 AWS account

 

2. Open Key Pairs page

As you are on the ec2 Dashboard, scroll down and from the left side select “Key Pairs” and then click on the “Create Key Pair“.

Create Key Pairs

 

4. Create a new Private SSH ec2 key pair

Now, you will see a page to select some options such as giving a name to your key, pair type, and format of Key file. Choose as per your requirement and then click on the “Create key pair” button.

A pop-up will appear, Save the key on your local system, somewhere, securely.

Create SSH ec2 Key Pair

 

5. Extract the Public key from the private key

So, you have a newly created private key on your system, let’s use it to retrieve the Public key from it. Open a Command prompt on Windows or Terminal on Linux systems.

Use the below-given syntax:

ssh-keygen -y -f /path_to_downloaded_key-pair.pem

For example:  

ssh-keygen -y -f "C:\Users\h2s\Desktop\AWS EC2.pem"

Now, copy and save the generated Publick Key from your newly created Private Key on some text editor.

Generate Public key from Private key

 

5. Connect to your existing Ec2 instance

Connect to the instance that private key you lost and want to replace with a new one. To do that, from your Ec2 Dashboard, click on the Instances option given on the left side panel and then select that Instance that private key you want to change.

Select Created Instance to connect

Now, Again from the “Ec2 Instance Connect Tab”  click on the “Connect” button.

Connect Instance using web console

 

6. Paste Public key to AWS Ec2 Instance

You will see the command line interface of your Ec2 Instance that you have connected. What we have to do, is to register our new Private key to the existing Instance, for that just paste the Public generated from it into the Instance Authorization key file. In simple words, just follow the below-given commands on the Web terminal:

sudo nano .ssh/authorized_keys

Replace a Lost Key Pair for Amazon ec2 Instance

 

7. Replace Lost Key’s string with a new one

Paste the Extracted Public key here in the opened file:

Just copy the Public key we have extracted earlier and paste it just below the existing line. You can use Ctrl+V to paste. After that save the file by pressing Ctrl+O and exit the same using the Ctrl+X shortcut key on your keyboard.

Note: If you don’t want to use the previous key pair that you lost or due to any reason then simply delete the existing public key string available in the file (the first line).

Paste Public Key to Ec2 Instance

 

8. Login using via SSH remotely using the key

Reboot Instance, if possible. Finally, you can connect to your instance using Putty, Terminal, Command prompt, or any other SSH client using the Instance Public Ip-address and the newly Private key that you have created. To know more about it you can see: How to SSH AWS ec2 Linux Instances remotely

 

 

2 thoughts on “How to add a new key pair to your exisitng AWS ec2 Instances”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.