我想将 ssh 密钥移入 vagrant 并将它们放入 中~/.ssh
,最简单的方法是什么?我的 Vagrant 文件中有以下内容:
config.vm.synced_folder "conf.d", "/svr/conf.d"
config.vm.provision :shell,
:inline => "ls -l /svr/conf.d/.ssh"
总计 4 -rw-r--r-- 1 vagrant vagrant 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 3 月 22 日 08:56 authorized_keys -rw-r--r-- 1 root root 1670 3 月 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
因此 vagrant 正在覆盖我的.ssh
目录。
答案1
我输入了我的 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"
当我意识到流浪者用户vagrant
不存在时,它就发挥了极好的作用ubuntu
(这就是为什么我对于我的 ssh 密钥似乎消失的原因感到困惑)。
答案2
关于什么SSH 代理转发?
首先确保您的 SSH 密钥在本地有效,然后添加config.ssh.forward_agent = true
到您的密钥中Vagrantfile
以便通过。
流浪者的详细信息在这里:http://docs.vagrantup.com/v2/vagrantfile/ssh_settings.html
答案3
您可以使用 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 没有默认提供这个功能!
答案4
要移动私钥和公钥,请按照以下步骤操作:
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