我怎样才能将我的部署密钥移到stream浪汉中?

我想将一个ssh密钥移到vagrant中,并放入~/.ssh ,那么最简单的方法是什么? 我在我的stream浪文件中有以下内容:

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

总4 – rw – r – r – 1stream浪stream浪1670年3月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" 

总共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

但是当我做vagrant ssh -c "ls -l ~/.ssh"我得到:

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

所以stream浪者正在覆盖我的.ssh目录。

那么SSH代理转发呢?

确保您的SSH密钥在本地工作,然后将config.ssh.forward_agent = true添加到您的Vagrantfile以通过。

Vagrant详细信息在这里: http : //docs.vagrantup.com/v2/vagrantfile/ssh_settings.html

我把我的SSH文件在conf.d/.ssh/id_rsa.medipop然后做:

 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" 

一旦我意识到stream浪的用户是vagrant而不是ubuntu (这就是为什么我对我的问题感到困惑,为什么我的SSH密钥似乎消失了)。

您可以使用Ruby的核心文件模块,如下所示:

  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 

我真的很惊讶,Vagrant不提供这个默认!

看一下Vagrant Shell Provisioner ,你可以把它添加到你的Vagrant文​​件中。

然而,根据你想要达到的目的,最好使用提供的ssh密钥访问Vagrant。

要生成一个快速configuration文件,将其添加到~/.ssh/config ,包括身份文件行,运行$ vagrant ssh-config 。 那么你可以使用$ ssh you-vagrant-box而不是$ vagrant ssh

要移动私钥和公钥,以下内容将起作用:

 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