A SSH private key as generated by ssh-keygen
contains a public key part. How do I retrieve this public key from the private key? I've lost my public key and need to put the contents of this public key in the servers authorized_keys
file and do not want to create a new key pair.
Alternatively phrased: how do I create the id_rsa.pub
file from a id_rsa
file?
I've found the answer on Server Fault: Create a public SSH key from the private key?
The option
-y
outputs the public key:As a side note, the comment of the public key is lost. I've had a site which required the comment (Launchpad?), so you need to edit
~/.ssh/id_rsa.pub
and append a comment to the first line with a space between the comment and key data. An example public key is shown truncated below.For keys that were added to the SSH Agent (a program that runs in the background and avoids the need for re-entering the keyfile passphrase over and over again), you can use the
ssh-add -L
command to list the public keys for keys that were added to the agent (viassh-add -l
). This is useful when the SSH key is stored on a smart card (and access to the private key file is not possible).This is a solution is specifically for users using Windows to SSH into their remote machines, including cloud images on Amazon AWS and GCE.
(Disclaimer)
I recently used this solution to remote log in to new deployed VM images on GCE.
Tools used:
Steps to perform:
Description (how to do it):
Generate a key/pair or use an existing private key:
If you have a private key:
Open puttygen, press load button and select your private key (*.pem) file.
If you do not have a private key:
Create a new 'authorized_keys' file (with Notepad):
Copy your public key data from the "Public key for pasting into OpenSSH authorized_keys file" section of the PuTTY Key Generator, and paste the key data to the "authorized_keys" file.
Make sure there is only one line of text in this file.
Upload the key to a Linux server:
Upload authorized_keys file to the home directory at the remote machine.
Set proper permissions:
Make a
.ssh
directory (if it does not exist)Copy the
authorized_keys
file to the .ssh directory (this will replace any existingauthorized_keys
file; take note of this).If the file exists, simply add the contents of this file to the existing file.
Run commands to set permissions:
Now you will be able to ssh into a remote machine without entering credentials every time.
Further reading:
Generating and uploading SSH keys under Windows
Authentication without password using OpenSSH Key, certificates .pem and .pub