How can I move my deploy key into vagrant?
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.
Solution 1:
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).
Solution 2:
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
Solution 3:
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!
Solution 4:
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
.