Instruction from AWS EC2 support:
- Log into your AWS account
- go to your EC2 Console
- Under NETWORK & SECURITY, click on Key Pair Click on Create Key Pair
- Give your new key pair a name, save the .pem file. The name of the key pair will be used to connect to your instance
- Create SSH connection to your instance and keep it open
- in PuttyGen (https://www.puttygen.com/), click "Load" to load your .pem file
- Keep the SSH-2 RSA radio button checked. Click on "Save private key" You'll get pop-up window warning, click "Yes”
- click on "Save public key" as well, so to generate the public key. This is the public key that we're going to copy across to your current instance
- Save the public key with the new key pair name and with the extension .pub
- Open the public key content in a notepad
- copy the content below "Comment: "imported-openssh-key" and before "---- END SSH2 PUBLIC KEY ----
Note - you need to copy the content as one line - delete all new lines - on your connected instance, open your authorized_keys file using the tool vi. Run the following command: vi .ssh/authorized_keys you should see the original public key in the file also
- move your cursor on the file to the end of your first public key content :type "i" for insert
- on the new line, type "ssh-rsa" and add a space before you paste the content of the public key , space, and the name of the .pem file (without the .pem) Note - you should get a line with the same format as the previous line
- press the Esc key, and then type :wq!
this will save the updated authorized_keys file
now try open a new SSH session to your instance using your new key pai
When you've confirmed you're able to SSH into the instance using the new key pair, u can vi .ssh/authorized_key and delete the old key.