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. So I would like to know whether it's possible or not to use multiple key pairs while creating an instance.
You can use Amazon EC2 to create your key pairs. You can also use a third-party tool to create your key pairs, and then import the public keys to Amazon EC2. Amazon EC2 supports ED25519 and 2048-bit SSH-2 RSA keys for Linux instances. You can have up to 5,000 key pairs per Region.
To create a key pairOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, under Network & Security, choose Key Pairs. On the Key Pairs page, choose Create Key Pair. For Key pair name, type a name that is easy for you to remember, and then choose Create.
Bottom line: You can use the same keypair on multiple instances and you can also use multiple keypairs on the same user on an instance. Show activity on this post. Yes, you can use one key pair for multiple EC2 instances. Click the "Launch" button and click "Choose an existing key pair."
EC2 key pairs and security groups don't cost a penny.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With