Skip to content

Instantly share code, notes, and snippets.

@gipsh
Last active May 15, 2017 04:55
Show Gist options
  • Save gipsh/d129d1e925d9c3e9cbe204e63eaf735c to your computer and use it in GitHub Desktop.
Save gipsh/d129d1e925d9c3e9cbe204e63eaf735c to your computer and use it in GitHub Desktop.
Replace or change a key on ec2 instance

AWS doesnt let you change your ec2 instance key from the admin console.

If you need to replace a key for an ec2 instance:

  1. if you already have the key/pub generated, jump to #3.

  2. Create a new key from command line:

ssh-keygen -t rsa -C "my-new-key"

Chose a name and add .pem as extention.

This will create two files, a .pub and a .pem

  1. Log into you instance as the user you want to change key, or simly root

  2. Edit the ~/.ssh/authorized_keys copy the content of the pub file and save it.

Will be somethign like this: ssh-rsa 2e+Mk2Y3mfswFpno9....long bla bla...3442f2A pepe

  1. Now try to login, the id file is your new pem file as usual

ssh -i "pepe.pub.pem" root@ec2-7-10-54-11.compute-1.amazonaws.com

  1. Delete other old keys from authorized_keys if you want.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment