已删除的.vagrant目录阻止我使用SSH

时间:2017-05-05 08:51:57

标签: vagrant

我正在使用Vagrant运行4个VM。我的Vagrantfile看起来像这样:

Vagrant.configure(2) do |config|
  config.vm.box = "bento/centos-7.2"
  config.vm.synced_folder "data", "/vagrant"

  # Master1
  config.vm.define :master1 do |m1|
    m1.vm.hostname = "master1.mycluster"
    m1.vm.network :private_network, ip: "192.168.0.12"
    m1.vm.provider :virtualbox do |vb|
      vb.memory = "8192"
    end
    m1.vm.network "forwarded_port", guest: 50070, host: 50070
  end

  # Other machines are defined in the same way
end

一切都运行良好,直到我不小心从项目目录中删除了.vagrant文件夹。

首先,我尝试将我的VM ID放回.vagrant/machines/${name}/virtualbox/id。运行vagrant status确实看到我的虚拟机已启动并正在运行,但我无法通过SSH连接。

我找了一个解决方案,但找不到一个,所以我决定销毁并重新启动我的虚拟机。遗憾的是,vagrant up(或vagrant up --provision)命令试图通过SSH连接到 127.0.0.1:2222 。不应该尝试连接到 192.168.0.12:2222 吗?这是命令输出:

Bringing machine 'master1' up with 'virtualbox' provider...
==> master1: Importing base box 'bento/centos-7.2'...
==> master1: Matching MAC address for NAT networking...
==> master1: Checking if box 'bento/centos-7.2' is up to date...
==> master1: Setting the name of the VM: vagrant_master1_1493974030138_66977
==> master1: Clearing any previously set network interfaces...
==> master1: Preparing network interfaces based on configuration...
    master1: Adapter 1: nat
    master1: Adapter 2: hostonly
==> master1: Forwarding ports...
    master1: 50070 (guest) => 50070 (host) (adapter 1)
    master1: 61888 (guest) => 61888 (host) (adapter 1)
    master1: 18080 (guest) => 18080 (host) (adapter 1)
    master1: 9995 (guest) => 9959 (host) (adapter 1)
    master1: 8787 (guest) => 8787 (host) (adapter 1)
    master1: 8088 (guest) => 8088 (host) (adapter 1)
    master1: 22 (guest) => 2222 (host) (adapter 1)
==> master1: Running 'pre-boot' VM customizations...
==> master1: Booting VM...
==> master1: Waiting for machine to boot. This may take a few minutes...
    master1: SSH address: 127.0.0.1:2222
    master1: SSH username: vagrant
    master1: SSH auth method: private key
    master1: Warning: Remote connection disconnect. Retrying...
    master1: Warning: Authentication failure. Retrying...

但是,我可以通过ssh -p 2222 vagrant@127.0.0.1连接到我的VM,而SSH文件夹似乎没有权限问题。

[vagrant@localhost ~]$ ls -la /home/vagrant/.ssh/
total 4
drwx------. 2 vagrant root     28  1 déc.  15:56 .
drwx------. 3 vagrant vagrant  90  1 déc.  15:57 ..
-rw-------. 1 vagrant root    409  1 déc.  15:56 authorized_keys

如何使用Vagrant再次运行VM?

1 个答案:

答案 0 :(得分:0)

TL; DR:请勿修改~/.vagrant.d/insecure_private_key

在尝试修复第一个问题时(在.vagrant中放回VM ID后无法连接到VM),我在某个地方发现了一个帖子,告诉我这是一个文件流浪者不安全私钥的问题,位于{ {1}}。

我试图在那里放置一个自定义私钥,这就是为什么以后使用vagrant up不起作用的原因。

解决方案是在此处找到默认的Vagrant不安全私钥:https://github.com/mitchellh/vagrant/tree/master/keys,并将其放回~/.vagrant.d/insecure_private_key