How to upload private SSH key to Vagrant box on "vagrant up"?

Janusz Slota - Mon 07 October 2013 - vagrant, howto

Sometimes it is handy to have your own private SSH key on your vagrant box, especially when you want to use git (or composer) inside your box, rather then in you host OS.

Assuming you're using OSX or Linux, you can use shell provisioner to upload local file to remote box:

Vagrant.configure("2") do |config|
    config.vm.provision :shell, :inline => "echo -e '#{File.read("#{Dir.home}/.ssh/id_rsa")}' > '/home/vagrant/.ssh/id_rsa'"
end

You may want to upload you git config too:

Vagrant.configure("2") do |config|
    config.vm.provision :shell, :inline => "echo -e '#{File.read("#{Dir.home}/.gitconfig")}' > '/home/vagrant/.gitconfig'"
end