Vagrant安装Virtualbox CentOS :Warning: Authentication failure. Retrying...问题

  近日遇到Vagrant安装VirtualBox 的CentOS镜像初始化失败问题。

  环境:

  windows7 x64 ,vagrant 1.9.5,oracle vm virtualbox5.1.18

  从官方源添加的box:

vagrant box add generic/centos7 --provider=virtualbox

  过程:设置好Vagrantfile(是从之前用过的box配置文件复制过来的):

Vagrant.configure("2") do |config|
    config.vm.define "Docker" do |dk|
        dk.vm.box = "CentOS-7" #"generic/centos7"
        dk.ssh.username="root"

        dk.ssh.insert_key = 'true'
        dk.vm.box_check_update = false
        dk.vm.boot_timeout = 60
        dk.vm.network "private_network", ip:"192.168.33.12"
        #default by vagrant ,disabled
        dk.vm.synced_folder ".", "/vagrant", disabled: true
        dk.vm.synced_folder "e:/javawd/dockerwd", "/dockerwd"

        dk.vm.provider "virtualbox" do |vb|
          vb.gui = false
          vb.memory = "512"
          vb.name = "Docker"
          vb.cpus = 2
        end

        config.vm.provision "hello", type: "shell" do |s|
        s.inline = "echo hello Docker user"
        end
    end
end

  Vagrant up 启动,到SSH连接虚拟机阶段时出错,出错现象:

Docker: Waiting for machine to boot. This may take a few minutes...
Docker: SSH address: 127.0.0.1:2222                                
Docker: SSH username: root                                         
Docker: SSH auth method: private key                               
Docker: Warning: Connection aborted. Retrying...                   
Docker: Warning: Authentication failure. Retrying...               
Docker: Warning: Authentication failure. Retrying...               
Docker: Warning: Authentication failure. Retrying...               
Docker: Warning: Authentication failure. Retrying...               

其中的Docker是虚拟机的名称,最终以Timeout错误提示结束。

然后Vagrant destroy销毁虚拟机,修改Vagrantfile,添加ssh密码配置:

dk.ssh.username="root"
dk.ssh.password="vagrant"#vagrant box的默认值

再次执行Vagrant up:

Bringing machine 'Docker' up with 'virtualbox' provider...                 
==> Docker: Importing base box 'CentOS-7'...                               
==> Docker: Matching MAC address for NAT networking...                     
==> Docker: Setting the name of the VM: Docker                             
==> Docker: Clearing any previously set network interfaces...              
==> Docker: Preparing network interfaces based on configuration...         
    Docker: Adapter 1: nat                                                 
    Docker: Adapter 2: hostonly                                            
==> Docker: Forwarding ports...                                            
    Docker: 22 (guest) => 2222 (host) (adapter 1)                          
==> Docker: Running 'pre-boot' VM customizations...                        
==> Docker: Booting VM...                                                  
==> Docker: Waiting for machine to boot. This may take a few minutes...    
    Docker: SSH address: 127.0.0.1:2222                                    
    Docker: SSH username: root                                             
    Docker: SSH auth method: password                                      
    Docker: Warning: Connection reset. Retrying...                         
    Docker: Warning: Connection aborted. Retrying...                       
    Docker: Warning: Authentication failure. Retrying...                   
    Docker: Warning: Authentication failure. Retrying...                   
    Docker: Warning: Authentication failure. Retrying...                   
    Docker: Warning: Authentication failure. Retrying...                   
Timed out while waiting for the machine to boot. This means that           
Vagrant was unable to communicate with the guest machine within            
the configured ("config.vm.boot_timeout" value) time period.               
                                                                           
If you look above, you should be able to see the error(s) that             
Vagrant had when attempting to connect to the machine. These errors        
are usually good hints as to what may be wrong.                            
                                                                           
If you're using a custom box, make sure that networking is properly        
working and you're able to connect to the machine. It is a common          
problem that networking isn't setup properly in these boxes.               
Verify that authentication configurations are also setup properly,         
as well.                                                                   
                                                                           
If the box appears to be booting properly, you may want to increase        
the timeout ("config.vm.boot_timeout") value.                              

    仍然超时报错,但这次SSH auth method由private key变成password了 。。。

    自此便开始了漫长的网上搜索学习, 搜来搜去,数年间的中外网站上大量关于这个问题的搜索结果!!但无一能真正解决这个问题,有的可用,但不方便。因为估计vagrant是不会给用户这样的体验的吧?

    诸如:1.配置ssh.private_key_path=path/to/your/private_key(然后再从VirtualBox中打开虚拟机界面,进入虚拟机设置.ssh/authorized_keys,还要修改目录及文件的权限、属主等);

               2.用vagrant用户vagrant密码登录,修改/etc/ssh/sshd_config:PasswordAuthentication no改为yes;

               3.。。。

                太多了,不一一列举。

    综合百家言,自己反复分析测试,最后得出结论:

    一、官方肯定不会让用户有这样的体验,应该有很便捷的使用方式;

    二、官方文档不够详细,大家都没真正明白,没在官方文档中看到正式的说明;

    三、重事实:

           1.合格的box默认都有private key 及password两种SSH认证方式(auth method),首选前者;

            2.使用private_key时,不需要设置  ssh.usernamessh.password(或者只设置ssh.username="vagrant",多数不使用root用户)程序会自己搜索C:\Users\Administrator\.vagrant.d目录下的insecure_private_key(具体位置可能因个人电脑设置而不同,但这个文件是自动安装的,vagrant会搜索到),这种情况下,vagrant会在首次up时识别这个默认的文件,并重新生成新的,其实是生成密钥对,把公钥插入到虚拟机中相应位置(如:/home/vagrant/.ssh下的authorized_keys),把私钥保存在Vagrantfile所在目录下相应虚拟机数据文件夹中(例如我是:D:\Vagrant\tmp\.vagrant\machines\Docker\virtualbox,文件名为private_key,之后使用vagrant ssh 连接虚拟机也会自动搜索并使用这个文件进行验证);

            这些是从C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.9.5\plugins\communicators\ssh\communicator.rb中分析到的。这才应该是正常的,便捷的使用方式。这里会看到令人舒爽的过程:

vagrant reload
==> Docker: Attempting graceful shutdown of VM...                                   
    Docker: Checking whether SSH is ready...                                        
    Docker:                                                                         
    Docker: Vagrant insecure key detected. Vagrant will automatically replace       
    Docker: this with a newly generated keypair for better security.                
    Docker: Checking whether SSH is ready...                                        
    Docker: Checking whether SSH is ready...                                        
    Docker:                                                                         
    Docker: Inserting generated public key within guest...                          
    Docker: Checking whether SSH is ready...                                        
    Docker: Removing insecure key from the guest if it's present...                 
    Docker: Checking whether SSH is ready...                                        
    Docker: Key inserted! Disconnecting and reconnecting using new SSH key...       
    Docker: Checking whether SSH is ready...                                        
==> Docker: Clearing any previously set forwarded ports...                          
==> Docker: Clearing any previously set network interfaces...                       
==> Docker: Preparing network interfaces based on configuration...                  
    Docker: Adapter 1: nat                                                          
    Docker: Adapter 2: hostonly                                                     
==> Docker: Forwarding ports...                                                     
    Docker: 22 (guest) => 2222 (host) (adapter 1)                                   
==> Docker: Running 'pre-boot' VM customizations...                                 
==> Docker: Booting VM...                                                           
==> Docker: Waiting for machine to boot. This may take a few minutes...             
    Docker: SSH address: 127.0.0.1:2222                                             
    Docker: SSH username: vagrant                                                   
    Docker: SSH auth method: private key                                            
    Docker: Checking whether SSH is ready...                                        
==> Docker: Machine booted and ready!                                               
==> Docker: Checking for guest additions in VM...                                   

           3.如果box的作者设置默认用password验证,则在Vagrantfile中设置ssh.usernamessh.password,也会遇到用户名为root或者vagrant两种情况,但也比以其他方式先进入虚拟机,做一系列修改,再vagrant reload之类的方便。

            4.如果你要分发定制的box与朋友共享,可以使用自己的密钥,在虚拟机/home/vagrant/.ssh/authrized_keys中添加公钥内容,私钥给朋友,让他在Vagrantfile中设置ssh.private_key_path指向该文件。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值