Pages

Friday, 19 July 2013

Changing Private-Public Key pair of an Existing Amazon EC2 Instance.

Changing private-public key pair of an existing amazon EC2 instance has been a constrain for me. I done lot of brainstorming on this. But in most of the places I found that taking new instance is the way or rather better option. But after lot of research I found out a way. It may not be straight forward but will do the job. But as a software engineer it is pleasure to do some creepy interesting things that will make life easier.We can do it in three steps.

Step 1: Creating New Key Pair

  • Log in to AWS console
  • Go to Services -> EC2 -> From left navigator(Menu on left side) select Key Pairs
  • From there we can get the details of existing key pairs.
  • Click on the Option "Create New Key Pair" from the top menu
  • Give a name for New Key Pair and Click on create.The new key pair will be crated and downloaded as a pem file.

Step 2: Extracting Pubic Key From Key Pair

  • Here we need to use the public key with SSH client.so weed to get public key based on that
  • Use the following command for generating public key
    ssh-keygen -y -f private_key.pem > public_key.pub

Step 3: Replacing the Old Public Key with New in the Instance

  • Log in to the Instance using Old Key Pair using ssh client
  • Follow the below steps
    1. cd ~/.ssh 
    2. sudo vi authorized_keys
    3. Open the new public key file(public_key.pub) and copy paste the content to "authorized_keys".
    4. After a space add the Privet Key base name(For eg: Here private key name is "private_key.pem".So after public key put a space and add "private_key".)
    5. Delete the old public key from the file "authorized_keys" and save
Now try to log in with new key pair. Its done.

No comments:

Post a Comment