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:

  1. A working Vagrant box
  2. 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.

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:

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: