How to Change Authentication Key Pair in EC2 Instance(Linux Server)

Ankit Gupta
Nerd For Tech
Published in
6 min readApr 27, 2023

in very easy and detailed Steps.

Photo by regularguy.eth on Unsplash

We currently have a running Linux server that has a Key pair enabled. In the subsequent steps, I will demonstrate the change authentication Key pair to server/instance and also how to generate a PPM key and convert it to a PPK key. If you already possess a PPK key, you may use it directly without the need for additional generation or conversion steps.

To whitelist your IP and allow access to your server’s Security Group options, please follow these steps:

a. Select the server that you wish to configure.

Server details with Shown Key Name By Ankit Gupta
Server Name

b. Navigate to the Security Group options and click on the Inbound rule.

Security Group options By Ankit Gupta
Security Group options

c. Add your IP address to the whitelist and save the rule. In inbound rules use the Source column dropdown as “My IP” and write your Name in the Description.

whitelisting IP in inbound rules
whitelist IP inbound rules

d. To complete this process, go to the Security tab of your selected server and follow the above steps. This will ensure that your IP is allowed access to the server’s inbound rule.

To access the server, please utilize Putty or Mobaxterm for logging in, and take note of the Public IP address of the server. In the event that you possess a private server, kindly log in through the Bastion host and ensure to whitelist your IP in the Bastion Host, if necessary. Please note that for demonstration purposes, we will be using a public server.

Search on your Computer and open Putty if you have already downloaded Putty.

Putty by Ankit Gupta
Putty

Paste or Write the IP address of the server in HostName or (IP address).

server in HostName
https://medium.com/@ankitgupta_974
Putty Configuration
https://medium.com/@ankitgupta_974
Putty Configuration

On the left side, the panel Clicks on SSH then Auth, and then Credentials.

Putty Configuration
Putty Configuration

In the right-side panel click on Browse and load/choose the PPK you have generated. Click Open.

Login as ec2-user or ubuntu or root. In my case ec2-user.

Write these commands :

1. sudo su -

2. cd /home/ec2-user/.ssh

Take the backup of the Authentication Key File.

3. cp authorized_keys authorized_keys.bkp

4. cat authorized_keys

Note: Please note that it is important to keep the Terminal window open. In case the Terminal becomes inactive, you can easily restart it by simply right-clicking on the Terminal window.

Go to the left side and click on the Key Pairs and create a new Key pair.

Key Pairs

Click on Create Key Pair and create a New Key Pair.

Name your New Key Pair and I have chosen the .pem key file format. Give a Tag so it can easy to find out. Click on Create KeyPair.

https://medium.com/@ankitgupta_974
Create Key Pair

Go to “Puttygen”, Load the New Private Key File (Pem), and then click ok.

PuttyKey Gen

To ensure a successful login to your EC2 server after changing the authentication Key, please follow these steps carefully. Firstly, refer to the image provided and copy the text before “imported-openssh-key” from the first line as shown in the image below. Next, paste this text onto Notepad, as it will be required for the following steps. It is crucial that you avoid making any errors while copying, as this may prevent you from logging into your EC2 server. If you have any questions or concerns, please do not hesitate to ask for assistance.

Note: While copying doesn’t copy the space of last or initial.

Using the Save private key button Save the private key file in .ppk Format.

Go to the Terminal again in the 4th step: vi authorized_keys.

Enter “ i ” to insert and remove previous authentication lines and paste the newly copied authentication Lines.

After the above, Click the Esc button and then write “ :wq! ” to save the changes and exit the file. Check the shown below Image.

Use the below command to verify.

cat authorized_keys

Without Closing, Right Click on the terminal and create New Session (Necessary Step), and Login with the same Public Ip but with New Key.

“This following step is to use only check the New authentication Key is working,while preserving/affecting the integrity of any existing Terminal sessions.”

https://medium.com/@ankitgupta_974

After Login through Putty to the server through New Key, in my case, my key pair name is “Temporary_New_Key_Pair”, we are able to log in.

https://medium.com/@ankitgupta_974

You can check and log in through the old Key pair you are also not able to Log in.

https://medium.com/@ankitgupta_974

Suggestions, improvements, and queries are welcome.

I hope this blog helps and saves your precious time so that you can spend it with your loved ones. Keep Smiling and show some love !!! Thank you for reading, if you enjoyed it, please hit the clap button and share it also.

If you like the above Blog on Cloud Computing Please Support Me through “Buymeacoffee ”.

Follow us for more content.

More content at Nerd For Tech.

--

--