Change AWS EC2 Instance Key Pair

  1. Create a new Key Pair in Network & Security > Key Pairs, let’s name it “new_key_pair”:
  2. Use PuTTYgen to convert the PEM file to PPK file. The PPK file will be used in PuTTY to connect in your instance. Click “Load” and open the new PEM file created from AWS. Then click “Save private key” button. It will ask for a paraphrase so click “No” if you don’t want to add extra complexity in your key:
  3. Open PuTTY and connect to your instance using the PPK file.
  4. Once connected, type the command: cd ~/.ssh and press enter, Then type ls and press enter. You’ll see a file authorized_keys, this is the file where we are going to our new key pair.
  5. Let’s create a new file “new_key_pair.pem” by typing the command vi new_key_pair.pem. This will create an open the new file. Once in there, copy and paste the content of the new key pair we just created from EC2.
  6. Change the permission of the file by typing the command chmod 600 new_key_pair.pem.
  7. Next, type the command ssh-keygen -f new_key_pair.pem -y
  8. Copy the generated key and open the file authorized_keys by typing the command: vi authorized_keys
  9. Remove all the content and replace it with the generated code by ssh-keygen, make sure by the end of the content add the name “new_key_pair”
  10. Close session and connect using the new key pair, make sure to generate a new PPK for the new key pair PEM file.