Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaskanzig/3ef8c358efbae732968961e3e33acf9f to your computer and use it in GitHub Desktop.
Save thomaskanzig/3ef8c358efbae732968961e3e33acf9f to your computer and use it in GitHub Desktop.
How to add a new key pair to your exisitng AWS ec2 Instances

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.

1. Open Key Pairs page

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. 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“.

2. 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.
Attention: Please choice the format of Key file as .pem format.

3. 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

Now, copy and save the generated Publick Key from your newly created Private Key on some text editor.
This public key looks something like this:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICa03KFXF/SAo/Nbl70ZlVt2y7CHXWs3DVqZgWTKW9jh

4. Connect to your existing Ec2 instance and paste the Public key to AWS Ec2 Instance

Connect to your Ec2 instance via terminal or from your ec2 Dashboard. What we have to do now, 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 vi ~/.ssh/authorized_keys

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.

Good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment