DEV Community

Cover image for Generating and Using a New PEM Key for EC2
DrSimple
DrSimple

Posted on

Generating and Using a New PEM Key for EC2

πŸ”‘ Generating and Using a New PEM Key for EC2

1. Create a New Key Pair

In the AWS Console:

  • Navigate to EC2 β†’ Key Pairs.
  • Create a new key pair and download the .pem file to your computer. (e.g., new_pemkey.pem)

2. Secure the Key

Run this command to set the correct permissions:

chmod 400 new_pemkey.pem
Enter fullscreen mode Exit fullscreen mode
  1. Generate the Public Key Use ssh-keygen to generate the public key from the .pem file:
ssh-keygen -y -f new_pemkey.pem
Enter fullscreen mode Exit fullscreen mode

Copy the output β€” this is your public SSH key.

  1. Add the Public Key in the EC2 Instance Open the EC2 instance in the AWS browser console.

Connect via the browser-based SSH client.

Inside the instance, open the authorized_keys file:

vi ~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

Paste the public key you generated in Step 3.

Save and exit.

  1. Connect Using the New Key Now you can connect to your EC2 instance from your local terminal:
ssh -i new_pemkey.pem ec2-user@<your-ec2-public-ip>
Enter fullscreen mode Exit fullscreen mode

βœ… You’ve successfully created, secured, and configured a new PEM key for your EC2 instance!

Top comments (0)