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.
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“.
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.
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
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!
This is working. Thank you!