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

Solution 1:

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