How to create ssh public key from private key - Vagrant & Linux

How to create SSH public Key from SSH private key is a question made me write this post. Though we would be able to achieve the SSH key-based authentication by producing the private key. Sometimes it is necessary that we must have the SSH public key.

Especially when we are trying to do ssh-copy-id to enable ssh key-based authentication between hosts

Now let us see how to create the SSH public key from the SSH private key file we already have and copy the SSH public key over using ssh-copy-id

Server provisioning tools like vagrant do come with only Private keys, Vagrant login to the managed Virtual Machines using just the private_key file.  to be more precise vagrant ssh somevm1  uses the ~/.vagrant.d/insecure_private_key file

I am going to use Vagrant's SSH private key file for this post and create SSH public key from it. Generally vagrant creates the private_key under home directory of the user in .vagrant.d sub directory with the file name insecure_private_key

Execute the following commands in order as they are given.

ssh-keygen -y -f ~/.vagrant.d/insecure_private_key
ssh-keygen -y -f ~/.vagrant.d/insecure_private_key > ~/.vagrant.d/vagrant.pub
ssh-copy-id -f -i ~/.vagrant.d/vagrant.pub [email protected]

Execution Result

[email protected]:~/VirtualBox VMs/vagrantVM$ ssh-keygen -y -f ~/.vagrant.d/insecure_private_key
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ==

[email protected]:~/VirtualBox VMs/vagrantVM$ ssh-keygen -y -f ~/.vagrant.d/insecure_private_key > ~/.vagrant.d/vagrant.pub</strong>

[email protected]:~/VirtualBox VMs/vagrantVM$ ssh-copy-id -f -i ~/.vagrant.d/vagrant.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/aksarav/.vagrant.d/vagrant.pub"

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

As we have copied the public key to the VM, We would now be able to log in without any username and password.

[email protected]:~/VirtualBox VMs/vagrantVM$ ssh [email protected]
[vagrant@mwiapp01 ~]$ 

 

Cheers

AK SARAV