[英]Can't ssh to vagrant VMs using the insecure private key (vagrant 1.7.2)
我有3个VM的集群。 这是Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
hosts = {
"host0" => "192.168.33.10",
"host1" => "192.168.33.11",
"host2" => "192.168.33.12"
}
Vagrant.configure("2") do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.ssh.private_key_path = File.expand_path('~/.vagrant.d/insecure_private_key')
hosts.each do |name, ip|
config.vm.define name do |machine|
machine.vm.hostname = "%s.example.org" % name
machine.vm.network :private_network, ip: ip
machine.vm.provider "virtualbox" do |v|
v.name = name
# #v.customize ["modifyvm", :id, "--memory", 200]
end
end
end
end
在我最近升级之前,它一直有效:
ssh -i ~/.vagrant.d/insecure_private_key vagrant@192.168.33.10
而是,vagrant要求输入密码。
似乎最新版本的vagrant(我在1.7.2上)为每台计算机创建了一个安全私钥。 我通过运行发现了它
vagrant ssh-config
输出显示每个主机的不同键。 我通过比较验证了密钥是否不同。
我试图通过在Vagrantfile中设置config.ssh.private_key_path来强制使用不安全的密钥,但是它不起作用。
我想对所有机器使用不安全密钥的原因是我想使用ansible从外部配置它们。 我不想使用Ansible设置程序,但是将VM视为远程服务器。 因此,Vagrantfile仅用于指定集群中的计算机,然后将在外部进行配置。
该文档仍然说,默认情况下,计算机将使用不安全的私钥。
如何使我的VM使用不安全的私钥?
Vagrant在1.6和1.7版本之间更改了行为,现在将插入自动生成的不安全密钥,而不是默认密钥。
您可以通过在Vagrantfile中设置config.ssh.insert_key = false
来取消此行为。
如果您像指定的那样指定private_key_path
那么Vagrant不应替换不安全密钥,但是内部逻辑会检查private_key_path
指向默认的insecure_private_key
,如果这样做,则Vagrant会替换它。
更多信息可以在这里找到。
当Vagrant创建新的ssh密钥时,它将以默认配置保存在Vagrantfile目录下的.vagrant / machines / default / virtualbox / private_key之下 。
使用自动生成的密钥,您可以从与Vagrantfile相同的目录中使用该密钥登录,如下所示:
ssh -i .vagrant/machines/default/virtualbox/private_key -p 2222 vagrant@localhost
要了解有关vagrant框的实际ssh配置的所有详细信息,请使用vagrant ssh-config命令。
# vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/babo/src/centos/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
将config.ssh.insert_key = false
添加到Vagrantfile并删除新的vm私钥.vagrant/machines/default/virtualbox/private_key
vagrant会使用正确的私钥~/.vagrant.d/insecure_private_key
自动更新vagrant ssh-config
。 我要做的最后一件事是ssh进入虚拟机并更新虚拟机上的授权密钥文件。 curl https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub > ~/.ssh/authorized_keys
tldr;
ssh vagrant@127.0.0.1 -p2222 -i/~/www/vw/vw-environment/.vagrant/machines/default/virtualbox/private_key
我无法使它正常工作,所以最后我在ssh.rb ruby脚本中添加了以下内容( /opt/vagrant/embedded/gems/gems/vagrant-1.7.1//lib/vagrant/util/ssh.rb
)
print(*command_options)
就在执行ssh调用的这一行之前
SafeExec.exec("ssh", *command_options)
这样就可以打印出传递给ssh调用的所有命令选项,从那里您可以根据流浪者计算出的正确ssh参数得出适合您的东西。
如果您专门使用Ansible(而不是Vagrant Ansible供应商),则可能要考虑使用Ansible仓库中的vagrant动态库存脚本:
或者,您可以手工制作自己的脚本并动态构建自己的无业游民清单文件:
SYSTEMS=$(vagrant status | grep running | cut -d ' ' -f1)
echo '[vagrant_systems]' > vagrant.ini
for SYSTEM in ${SYSTEMS}; do
SSHCONFIG=$(vagrant ssh-config ${SYSTEM})
IDENTITY_FILE=$(echo "${SSHCONFIG}" | grep -o "\/.*${SYSTEM}.*")
PORT=$(echo "${SSHCONFIG}" | grep -oE '[0-9]{4,5}')
echo "${SYSTEM} ansible_ssh_host=127.0.0.1 ansible_ssh_port=${PORT} ansible_ssh_private_key_file=${IDENTITY_FILE}" >> vagrant.ini
done
然后使用ansible-playbook -i=vagrant.ini
如果尝试使用~/.ssh/config
,则必须动态创建或编辑现有条目,因为ssh端口可以更改(由于Vagrant中的冲突检测)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.