Step 1: Create Additional Key Pairs
Each user that needs access to the EC2 instance should have a unique key pair. Create a key pair using the following command:
aws ec2 create-key-pair --key-name MySecondKeyPair --query 'KeyMaterial' --output text > MySecondKeyPair.pem
Remember to replace MySecondKeyPair with your desired key pair name. Repeat this step for as many key pairs as you need.
Step 2: Add Public Keys to the EC2 Instance
Next, add the public keys of the additional key pairs to the ~/.ssh/authorized_keys file on the EC2 instance.
First, extract the public key from the private key file:
ssh-keygen -y -f MySecondKeyPair.pem > MySecondKeyPair.pub
Then, copy the public key to the EC2 instance:
ssh -i MyKeyPair.pem ec2-user@<Your-EC2-Instance-IP> "echo 'cat MySecondKeyPair.pub >> .ssh/authorized_keys'"
Repeat this step for each additional key pair.
Test SSH Access
Finally, test SSH access with the new key pair:
ssh -i MySecondKeyPair.pem ec2-user@<Your-EC2-Instance-IP>
If everything is set up correctly, you should be able to log in to the EC2 instance with the new key pair.
Leave a comment