Reputation: 3799
While creating an EC2 instance, we provide a key pair name.
But generally, I associate multiple SSH public/private keys with any remote server. I know that it's not possible to attach a key pair once the EC2 server has been created, but I would like to know whether or not it's possible to use multiple key pairs while creating an instance.
Upvotes: 70
Views: 59376
Reputation: 2170
You can't... only way is to manually edit ~/.ssh/authorized_key
s and add the public keys of the extra users you would like to give access. The disadvantage if this approach is that you'll have to re-do this operation over again, when your EC2 get's terminated. Not really convenient in a developer/testing environment...
Upvotes: 11
Reputation: 1047
Unfortunately, it's also not possible to import a key having two entries. Only the first entry is imported into the new key pair.
What you can do is:
Don't use the EC2 key pairs but instead use the user_data
field to insert multiple SSH public keys in the /home/<user>/.ssh/authorized_keys
file, where <user>
is the standard user for your AMI (ubuntu, ec2_user etc.).
You can add user_data
to every launching EC2 instance. Consider the following example:
#!/bin/bash
echo "ssh-rsa AAAA…" > /home/ubuntu/.ssh/authorized_keys
echo "ssh-rsa AAAA…" >> /home/ubuntu/.ssh/authorized_keys
chown ubuntu: /home/ubuntu/.ssh/authorized_keys
chmod 0600 /home/ubuntu/.ssh/authorized_keys
User data scripts run as root
so you don't need to specify sudo
.
That way, you could create personalized SSH access keys via tools like Terraform before managing the instances with Ansible or similar.
Note that you don't know what keys are being used by a simple look, though. You'd need access to the machine to check it.
Upvotes: 77
Reputation: 16482
You can't associate multiple Key Pairs to an EC2 Instance.
With that said, you can create multiple users and provide them access to the instance via the SSH with Key Authentication rather than password.
The process goes this way
More Information - SSH with authentication key instead of password
Upvotes: 5