Sometimes for a Vagrant box you want to use your own public SSH key instead of the public one provided by Vagrant by default. Either you want to securely share the box or you are paranoid about security. Whatever the reason may be, you can setup a custom generated public key.
What you need to get started:
This tutorial assumes that you have the following:
- A working Vagrant box
- A public and private key combination
Copy the public key:
In order to use your custom generated public key we will need to copy it to the vagrant user’s authorized_keys file.
[joseph@home ~]$ cat $HOME/.ssh/id_rsa.pub | ssh firstname.lastname@example.org -p 2222 'cat >> $HOME/.ssh/authorized_keys'
Assuming that you have your vagrant box running on localhost and port 2222. Then your public key has now been copied over to the vagrant user.
Modify the Vagrantfile
We need to tell Vagrant that we are using our own public key instead of the usual supplied one. To do this we need to modify the Vagrantfile. Open the Vagrantfile for your Vagrant box in your favorite editor and add the following config parameter:
config.ssh.private_key_path = "/home/joseph/.ssh/id_rsa"
The above config parameter is telling Vagrant to use your private key instead of the default one provided by Vagrant. You will want to give it the location of the private key you want to use.
Test the changes
You need to reboot your Vagrant box for the settings to take effect. Once the box is up try and SSH into the box. If you followed all the steps you can now try to SSH into your Vagrant box:
[joseph@home centos7-vagrant]$ ssh email@example.com -p 2222
Last login: Thu Sep 4 23:29:14 2014 from 10.0.2.2