Vagrant初始登录失败的一般性解决方案

如果是下载的box文件,vagrant box add和init之后启动,可能出现长时间无法通过vagrant ssh登陆的问题

==> localvm2: Importing base box 'bigdatavm'...
==> localvm2: Matching MAC address for NAT networking...
==> localvm2: Setting the name of the VM: localvm2
==> localvm2: Fixed port collision for 22 => 2222. Now on port 2200.
==> localvm2: Clearing any previously set network interfaces...
==> localvm2: Preparing network interfaces based on configuration...
    localvm2: Adapter 1: nat
    localvm2: Adapter 2: hostonly
==> localvm2: Forwarding ports...
    localvm2: 22 (guest) => 2200 (host) (adapter 1)
==> localvm2: Running 'pre-boot' VM customizations...
==> localvm2: Booting VM...
==> localvm2: Waiting for machine to boot. This may take a few minutes...
    localvm2: SSH address: 127.0.0.1:2200
    localvm2: SSH username: vagrant
    localvm2: SSH auth method: private key
    localvm2: Warning: Remote connection disconnect. Retrying...
    localvm2: Warning: Remote connection disconnect. Retrying...
    localvm2: Warning: Authentication failure. Retrying...
    localvm2: Warning: Authentication failure. Retrying...
    localvm2: Warning: Authentication failure. Retrying...
    localvm2: Warning: Authentication failure. Retrying...
    localvm2: Warning: Authentication failure. Retrying...
    localvm2: Warning: Authentication failure. Retrying...
    localvm2: Warning: Authentication failure. Retrying...
    localvm2: Warning: Authentication failure. Retrying...

这时有两种可能

一是虚拟机确实启动失败,由于vagrant默认不显示虚机启动界面,所以不太好判断。因此需要在Vagrantfile配置中增加vb.gui = true选项,就可以查看虚机的启动过程。常见问题是没有开启PC的vt-x支持,进BIOS修改配置即可。

二是如果使用拷贝过来的Vagrantfile进行up启动

可能会由于ssh认证机制导致失败。vagrant默认采用key登录,但所用的KeyPair可能没有正常配置。

使用vagrant ssh-config查看

D:\bigdata>vagrant ssh-config
Host localvm1
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/bigdata/.vagrant/machines/localvm1/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

The provider for this Vagrant-managed machine is reporting that it
is not yet ready for SSH. Depending on your provider this can carry
different meanings. Make sure your machine is created and running and
try again. Additionally, check the output of `vagrant status` to verify
that the machine is in the state that you expect. If you continue to
get this error message, please view the documentation for the provider
you're using.

D:\bigdata>

私钥的地址为D:/bigdata/.vagrant/machines/localvm1/virtualbox/private_key,但实际上本机没有这个文件。

修改方式1:拷贝本机生成的私钥到上述路径;用用户名密码(一般约定为vagrant/vagrant)通过shell登陆虚机,修改~/.ssh下的公钥文件为自己本机生成的公钥。

下次vagrant up就可以登陆成功了。

修改方式2:更改ssh配置为初始密码登陆(增加password和insert_key配置)

 config.vm.define :localvm3 do |localvm3_config|
    localvm3_config.vm.hostname = "localvm3.vagrant.internal"
    localvm3_config.vm.network :private_network, ip: "192.168.66.33"
    localvm3_config.ssh.password = "vagrant"
    localvm3_config.ssh.insert_key = false
    localvm3_config.vm.provider "virtualbox" do |vb|
        vb.gui = true
        vb.name = "localvm3"
        vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
        vb.customize ["modifyvm", :id, "--memory", "2048"]
    end
end

相关推荐