How do you add an existing key to a live EC2 instance that has no key pair?
I have tried using Session Manager to vi /.ssh/authorized_keys
and add in the public key of the pair, but I get this error:
"~/.ssh/authorized_keys"
"~/.ssh/authorized_keys" E212: Can't open file for writing
Is this something that has to be done using the console?
No associated key pair
This instance is not associated with a key pair. Without a key pair, you can't connect to the instance through SSH.
You can connect using EC2 Instance Connect with just a valid username. You can connect using Session Manager if you have been granted the necessary permissions.
Since you’ve got Systems Manager configured you’re half-way there. The only thing standing between you and success is the fact that Systems Manager Session opens the session as
ssm-user
and you’ll need to dosudo -u ec2-user -i
first and once you're logged in asec2-user
do the subsequent steps.Then you may have to create the
~ec2-user/.ssh
directory if it doesn’t exist and chmod it to 700.And finally you can add the public key to
~ec2-user/.ssh/authorized_keys
and chmod that file to 600.That should do the trick :)