9

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.

5 Answers 5

13

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).

2
  • This is the way on windows, where agent forwarding doesn't work.
    – markus
    Nov 15, 2013 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, 2015 at 3:50
12

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

3
  • 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, 2013 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, 2013 at 10:52
  • 1
    This is pretty much the same question: superuser.com/a/570775/210384
    – rjocoleman
    Mar 26, 2013 at 10:52
5

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

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

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

1
  • 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, 2015 at 21:21
1

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.

4
  • 1
    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, 2013 at 8:56
  • 1
    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, 2013 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, 2013 at 9:24
  • 1
    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, 2013 at 9:32
0

To move both private and public keys the following will work:

config.vm.provision "shell" do |s|
  ssh_prv_key = File.read("#{Dir.home}/.ssh/id_rsa")
  ssh_pub_key = File.readlines("#{Dir.home}/.ssh/id_rsa.pub").first.strip
  s.inline = <<-SHELL
    echo Provisioning public ssh key...
    [ -e /home/vagrant/.ssh/id_rsa.pub ] && rm /home/vagrant/.ssh/id_rsa.pub
    touch /home/vagrant/.ssh/id_rsa.pub
    echo "#{ssh_pub_key}" >> /home/vagrant/.ssh/id_rsa.pub

    echo Provisioning private ssh key...
    [ -e /home/vagrant/.ssh/id_rsa ] && rm /home/vagrant/.ssh/id_rsa
    touch /home/vagrant/.ssh/id_rsa
    echo "#{ssh_prv_key}" >> /home/vagrant/.ssh/id_rsa

    echo Provisioning of ssh keys completed [Success].
  SHELL
end

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .