Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to move a an ssh key into vagrant and put them in ~/.ssh, what's the easiest way of doing that? I have the following in my Vagrant file:

config.vm.synced_folder "conf.d", "/svr/conf.d"
config.vm.provision :shell, 
:inline => "ls -l /svr/conf.d/.ssh"

total 4 -rw-r--r-- 1 vagrant vagrant 1670 Mar 26 08:19 id_rsa.mediapop

config.vm.provision :shell, 
:inline => "cp /svr/conf.d/.ssh/id_rsa.mediapop /home/ubuntu/.ssh/id_rsa"
config.vm.provision :shell, 
:inline => "ls -l /home/ubuntu/.ssh"

total 4 -rw------- 1 ubuntu ubuntu 0 Mar 22 08:56 authorized_keys -rw-r--r-- 1 root root 1670 Mar 26 08:59 id_rsa

but then when I do vagrant ssh -c "ls -l ~/.ssh" I get:

$ vagrant ssh -c "ls -l ~/.ssh"
total 4
-rw-r--r-- 1 vagrant vagrant 409 Mar 20 04:47 authorized_keys

So vagrant is overwriting my .ssh directory.

share|improve this question
up vote 10 down vote accepted

I put my SSH file in conf.d/.ssh/id_rsa.medipop and then did:

config.vm.synced_folder "conf.d", "/svr/conf.d"
config.vm.provision :shell, 
:inline => "cp /svr/conf.d/.ssh/id_rsa.mediapop /home/vagrant/.ssh/id_rsa"

Which worked splendidly once I realised the vagrant user is vagrant not ubuntu (which is why I was confused in my question as to why my ssh key seemed to disappear).

share|improve this answer
This is the way on windows, where agent forwarding doesn't work. – markus Nov 15 '13 at 21:01
works but get this error, ==> default: stdin: is not a tty also permissions change and had to be reset, will running with sudo off fix? – radtek Jan 20 '15 at 3:50

What about SSH Agent Forwarding?

Make sure your SSH key works locally first then add config.ssh.forward_agent = true to your Vagrantfile to pass through.

Vagrant details here:

share|improve this answer
I'm weary about doing that since I'm planning on distributing the vagrant box to our designers who would probably look at me funny if I started referring to keychains as computer things. :) Thanks though! – Kit Sunde Mar 26 '13 at 10:43
In that case I'd either build my own box with VeeWee and distribute it as a package including config.ssh.private_key_path or do something terrible like have the ssh key in the project folder with the Vagrantfile. It's got to be distributed somehow in the use case, so pick your evil! – rjocoleman Mar 26 '13 at 10:52
This is pretty much the same question: – rjocoleman Mar 26 '13 at 10:52

You can use Ruby's core File module, like so:

  config.vm.provision "shell" do |s|
    ssh_pub_key = File.readlines("#{Dir.home}/.ssh/").first.strip
    s.inline = <<-SHELL
      echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
      echo #{ssh_pub_key} >> /root/.ssh/authorized_keys

I'm really surprised that Vagrant doesn't provide this by default!

share|improve this answer
I would recommend to also add quotes around echo arguments: echo "#{ssh_pub_key}" >> /home/vagrant/.ssh/authorized_keys and echo "#{ssh_pub_key}" >> /root/.ssh/authorized_keys. This will also work if a public SSH key contains special characters (e.g. (). – tjanez Aug 17 '15 at 21:21

Take a look at the Vagrant Shell Provisioner, you'd add this to your Vagrantfile.

However, depending on what you're trying to achieve, it's probably better to use the supplied ssh key to access Vagrant.

To generate a quick config file to be added to your ~/.ssh/config, including an identity file line run $ vagrant ssh-config. You could then $ ssh you-vagrant-box rather than $ vagrant ssh.

share|improve this answer
Ah this is not for SSH'ing into my box, this is to give vagrant access to my git repos. So I need to move a file from my host into ~/.ssh/ – Kit Sunde Mar 26 '13 at 8:56
That is more clear! In your edit above are you sure that ~/ and /home/ubuntu are the same location? Vagrant's default homedir is /home/vagrant. – rjocoleman Mar 26 '13 at 9:21
Don't forget that your Vagrantfile is fully interpreted ruby, so you could do something like: git_ssh_key ='/svr/conf.d/.ssh/id_rsa.mediapop'); config.vm.provision :shell, :inline => "cat >>/home/vagrant/.ssh/id_rda.mediapop <<EOF #{git_ssh_key} EOF" You may run into permissions issues reading the ssh key though. – rjocoleman Mar 26 '13 at 9:24
I have managed to copy the file into ~/.ssh using a synced folder and cp, but it seems like vagrant overwrites it afterwards. – Kit Sunde Mar 26 '13 at 9:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.