Skip to content

Instantly share code, notes, and snippets.

@plasticbrain
Created April 15, 2015 19:12
Show Gist options
  • Select an option

  • Save plasticbrain/6632b57bbf6a97e6a02b to your computer and use it in GitHub Desktop.

Select an option

Save plasticbrain/6632b57bbf6a97e6a02b to your computer and use it in GitHub Desktop.
AWS - EC2 - Change PEM Key
1. From the AWS EC2 Console go to "Key Pairs" and generate a new key pair (eg: NewKey.pem); Download the generated pem key, and chmod it to 0666
2. Use the new pem key to generate a public key
$ ssh-keygen -y
When prompted, enter the path to NewKey.pem. This will generate NewKey.pub
Tip: Edit NewKey.pub and append the key's name so you can identify it easier in the next steps.
Just append a single space, then a tag/name for the key
3. Add the new public key to your instance
$ cat NewKey.pub | ssh -i OriginalKey.pem user@amazon-instance "cat >> .ssh/authorized_keys"
4. Test the new key by logging in and removing the old/original key
$ ssh -i NewKey.pem user@amazon-instance
$ nano ~/.ssh/authorized_keys
Find the line containing the "original" key and remove it.
@jgonzalezd

Copy link
Copy Markdown

This saved my day! Thank you.

@rjdp

rjdp commented Jan 29, 2017

Copy link
Copy Markdown

tnx

@yc-codes

yc-codes commented Mar 6, 2020

Copy link
Copy Markdown

Thank you so much, saved my day.

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