Lets say you have a private/public key pair that you use to login to your server via SSH and you lose the public key, either it was deleted or corrupt and you don’t want to have to regenerate a new pair what options do you have? In this post I will demonstrate how to regenerate a public key from the corresponding private key that you still have.
It is a simple one liner command to generate a public key from a private key, so lets say our private key is named ‘user@myserver.key’ and we want to generate the public key and name it ‘authorized_keys’. Below is the command to do this.
Generating a Secure Shell (SSH) Public/Private Key Pair Several tools exist to generate SSH public/private key pairs. The following sections show how to generate an SSH key pair on UNIX, UNIX-like and Windows platforms. Open PuTTY Key Generator. Load your private key (.ppk file). Copy your public key data from the 'Public key for pasting into OpenSSH authorizedkeys file' section of the PuTTY Key Generator and paste the key data to the 'authorizedkeys' file (using notepad) if you want to use it. Snapshot showing portions of Puttygen to focus. Turns out the old version of ssh on this server doesn't generate the public key from the private key, it reads the idrsa.pub file to send the public key. Since this public key doesn't match what's in authorizedkeys, I was denied. The solution was to delete the public key. Alternatively, you can generate the public key with ssh-keygen. All Mac and Linux systems include a command called ssh-keygen that will generate a new key pair. If you're using Windows, you can generate the keys on your server. Just remember to copy your keys to your laptop and delete your private key from the server after you've generated it. To generate an SSH key pair, run the command ssh-keygen.
Windows 7 ultimate 32 bit activation key generator. ‘ssh-keygen -y’ : tells the prorgram ssh-keygen to output a public key
‘-f user@myserver.key’: references the key file user@myserver.key to generate the public key from
‘> authorized_keys’: instead of printing the output to the console redirect the output (public key) to a file named ‘authorized_keys’
Now we will need to take our authorized_keys file and store it on the server, if we can currently login via SSH to our server we can use rsync over ssh to securely copy the public key to the server, Although this key is displayed to anyone who attempts to login to your server it is smart to use SSH or another secure method to copy it up to the server to ensure that the key is not tampered with while it is being transmitted.
Below is an example using rsync to copy the authorized keys file to its destination on the server and removing it from your local computer.