I have a running amazon ec2 linux instance associated with a keypair (p1) and I have downloaded the private key to my home desktop. Now at work, I created a keypair (p2) on my work desktop and imported the public key to Amazon via the AWS console.
At home, I want to add the public key of keypair p2 to be added to authorized_keys
of my AMI instance (which I can currently access only from home).
However, I forgot to bring the public key of p2 with me, so is it possible to somehow export this public key from Amazon.
The correct ssh-keygen command is however:
Nice puzzle, thanks! Here's one answer:
Start a new, temporary EBS boot t1.micro instance A, specifying keypair p2. Specify an availability zone where you have another instance B already running and to which you have access. (Start a temporary one if needed).
Stop (not terminate) the instance A after it has been in the running state for a few minutes, so it has a chance to save the public key to its authorized_keys file.
Detach the root EBS volume from the stopped instance A. Attach and mount it to your running instance B.
Copy the public key from the mounted file system.
Detach and delete the EBS volume. Terminate the temporary instance A.
I already provided one answer which uses EBS volumes to get at the ssh public key, but here's another way you can get at it by starting a temporary EC2 instance with a user-data script that sends the public key to the console output. Here are the steps:
Save the following code to a file named
output-ssh-key.userdata
on your local computer. DO NOT RUN THESE COMMANDS LOCALLY!Run a stock Ubuntu 10.04 LTS instance with the above file as a user-data script. Specify the keypair for which you want to retrieve the public ssh key:
Keep requesting the console output from the instance until it shows your public ssh key. Specify the instance id returned from the run-instances command:
Within 2-10 minutes you will get output like this:
The temporary instance will automatically terminate itself in under an hour, but you can terminate it yourself if you'd like to make sure that you aren't charged more than the two cents this will cost to run.
If you have the private SSH key you can re-generate the public key component simply by running the following ssh-keygen command:
That much is the simple part... The AWS console and API does not support pushing 2 keypairs when starting an EC2 instance. This is an exercise left for the system administrator to do through other means.
If you have access to the identity key already authorized you could simply perform the following ssh-copy-id command:
This will copy the given public key to the server and into the
~user/.ssh/authorized_keys
file automatically for you and ensure proper permissions on the file.The more elegant way would be to include the additional identity keys in your configuration management processes. In my case this entails adding the additional keys to the Puppet configuration for the node.
As a side note, personal preference but would would utilize a better SSH key management method than simply having to include separate keys for work and home location. As I mentioned in a previous question I maintain my keys on a USB drive that I keep with me rather than on any computer I utilize.
Another option would be to add a short script in user_data that just adds another ssh key to root:
Then you can log into the machine as root with
ssh -l root -i <KEYFILE> URL
and just read out the key from authorized_keys of the user ec2_user, ubuntu or however it is called.Only thing - you need to make the machine publicly reachable, and make sure access to port 22 is possible from outside.