How to deliver private ssh keys for a virtualbox controlled by vagrant

ssh-keysvagrant

By default, Vagrant uses an insecure private key used when logging into the machine with vagrant ssh.

Vagrant supports specifying a different private key via private_key_path however I am not clear how I can best deliver this key with the box file. The tutorials and websites that I found usually assume that the users have the key installed in a common place.

However, I would like deliver just a box file without additional setup steps.

Can I only do this with vagrant's standard ssh key ?

Best Answer

The default private key ships with Vagrant, so there are no additional steps either. Just use the corresponding public key in the VM. Adding and shipping a custom key might only make sense when you control the distribution of the box, and don't want anyone else to be able to log in to running instances. Even in that case I would probable install and configure new key on a provision step instead.

Having said that, the Vagrant box file is a zip or (optionally gzipped) tar archive. You can include the private key for example to the top level directory there, and add the public key to the vagrant ssh user's authorized_keys in the VM itself.

Then to configure Vagrant to use the key you include a Vagrantfile to the box with content like:

Vagrant.configure("2") do |config|
  config.ssh.private_key_path = File.expand_path("../my_secret_key", __FILE__)
end