Generate New Key Pair For Ec2 Instance

Posted By admin On 16.12.20

Learn how to easily generate and add a new public and private key pair to an AWS EC2 instance. This is useful if you ever need to replace a compromised key or provide ssh access to multiple users without sharing private keys.

1. Generate a New Private Key

  1. Login to the AWS EC2 console and select Key Pairs in the left sidebar
  2. On the next page, click the Create Key Pair button
  3. Give the new key a name, then click the create button
  4. Download the new key, and then chmod it to 0666

How to Create and SSH Connect to AWS EC2 Instance. By Sajal Chakraborty Filed Under: AWS. We will create a new Key pair by choosing appropriate option from drop down and will have to provide a name of the key-pair file and then we need to download that file by clicking the Download Key Pair button in the page. A key is much more secure. Apr 13, 2018  Stop the EC2 instance for which you want to change the.pem file. Detach its /dev/xvda1 volume (let's call it volume A) - see here; Start new t1.micro EC2 instance, using my new key pair. Make sure you create it in the same subnet, otherwise you will have to terminate the instance and create it again. Mar 31, 2017  In this Recreate a lost Private Key process what I did have I created a new public-private key pair by generating one locally then uploading the key pair to AWS. In that way when I create a new EC2 instance I can assign the key pair I just created and access the boxes via SSH.

  • You can create a key pair using the console, AWS Command Line Interface (AWS CLI), or AWS Tools for Windows PowerShell. For more information, see Creating a Key Pair Using Amazon EC2. Note: To give the new key pair the same name as the lost key pair, you must first delete the lost key pair.
  • Login to EC2 Instance using New Key Pair. We can use the below command to login to the EC2 instance using the new key pair. Ssh -i newtest.pem ubuntu@18.234.223.77. In the above example, Make sure you are in the correct path of the.pem file. Change the username and IP Address based on your instance configuration.

For this tutorial we’ll call this new private key NewKey.pem


2. Generate a New Public Key

Next, use NewKey.pem that was created in step 1 to create a new public key. The new public key will be NewKey.pub.

1catNewKey.pub ssh-iOriginalKey.pemuser

Replace Key Pair Ec2

@amazon-instance'cat >> .ssh/authorized_keys'

4. Test the New Key

Use the new key to ssh into the server. If needed, the original key can be removed during this step

1nano~/.ssh/authorized_keys

Find the line containing the original/old key and remove it.

Related Articles

Adding an SSH Key to Amazon EC2

Create Ec2 Key PairHow to Add Additional Elastic IPs to AWS EC2 InstancesHow to Get an AWS Instance ID with PHP, wget, or Curl×

Need Some More Help? Let's Talk!

I'd love to work with you! Fill out the form below to schedule a free consultation to discuss your needs and how I can help. Random key generator in javascript.

Need More Help?