Skip to content

Instantly share code, notes, and snippets.

@rjdp
Created January 29, 2017 10:54
Show Gist options
  • Save rjdp/1c306f9e7453d18bc6c8927f6ec30f06 to your computer and use it in GitHub Desktop.
Save rjdp/1c306f9e7453d18bc6c8927f6ec30f06 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment