nortmas
6/12/2017 - 11:00 AM

Copy public key form local computer to VM

Script to copy public key form local computer to VM. If you need to change or add custom public key, you can create Vagrantfile.local in a VM root folder and add following lines:

Vagrant.configure('2') do |config|
  config.vm.provision "file", source: "#{Dir.home}/.ssh/acquia_id_rsa/id_rsa", destination: "/home/vagrant/.ssh/id_rsa"
            config.vm.provision "shell",
                inline: "chmod 600 /home/vagrant/.ssh/id_rsa"
  
  config.vm.provision "shell" do |s|
      ssh_pub_key = File.readlines("#{Dir.home}/.ssh/acquia_id_rsa/id_rsa.pub").first.strip
      s.inline = <<-SHELL
        echo #{ssh_pub_key} >> /home/vagrant/.ssh/id_rsa.pub
      SHELL
  end
end
Vagrant.configure('2') do |config|
    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/id_rsa.pub
        SHELL
    end
end
Vagrant.configure('2') do |config|
    config.ssh.insert_key = true
    config.ssh.forward_agent = false
end

config.vm.provision "shell" do |s|
    ssh_pub_key = File.readlines("#{Dir.home}/.ssh/acquia_id_rsa/id_rsa.pub").first.strip
    ssh_priv_key = ""
    File.read("#{Dir.home}/.ssh/acquia_id_rsa/id_rsa").each_line do |line|
        ssh_priv_key += line
    end
    ssh_priv_key.strip
    s.inline = <<-SHELL
      echo #{ssh_pub_key} >> /home/vagrant/.ssh/id_rsa.pub
cat <<'EOF' > /home/vagrant/.ssh/id_rsa
#{ssh_priv_key}
EOF
    SHELL
end