首頁 > 軟體

Vagrant初始登入失敗的一般性解決方案

2020-06-16 16:53:16

如果是下載的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

本文永久更新連結地址https://www.linuxidc.com/Linux/2018-04/151770.htm


IT145.com E-mail:sddin#qq.com