Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

3 Answers 3

up vote 7 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 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: http://docs.vagrantup.com/v2/vagrantfile/ssh_settings.html

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: superuser.com/a/570775/210384 –  rjocoleman Mar 26 '13 at 10:52

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 = File.read('/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.