Authentication failure. Retrying - 彻底解决vagrant up时警告

举报
lxw1844912514 发表于 2022/03/27 01:47:41 2022/03/27
【摘要】 碰到的问题 使用vagrant启动虚拟机时,出现如下警告: vagrant up default: Warning: Authentication failure. Retrying... 原因分析 授权失败主要原因: 虚拟机获取不到物理机的公钥(有疑问的小伙伴,建议先了解一下S...

碰到的问题

使用vagrant启动虚拟机时,出现如下警告:

vagrant up default: Warning: Authentication failure. Retrying...
  

原因分析

授权失败主要原因:

  • 虚拟机获取不到物理机的公钥(有疑问的小伙伴,建议先了解一下SSH)

解决方案

  • 公钥复制到虚拟机vagrant用户家目录下的authorized_keys文件中

  • Vagrantfile中指定物理机当前用户的私钥路径

步骤一、确认物理机中已经生成了公钥和私钥

以windows系统为例,查看当前登录用户的文件夹下是否包含.ssh文件夹,以及.ssh文件夹下是否包含id_rsa(私钥)、id_rsa.pub(公钥)两个文件

 

.ssh文件夹文件如下:

 

注意:必须打开 【显示隐藏的文件、文件夹或驱动器】才能看到.ssh文件夹

 

如果已经包含id_rsa(私钥)、id_rsa.pub(公钥)两个文件则可跳过步骤一。如果没有两个文件则继续往下看

生成公钥和私钥有多种方法,我们使用最常用的办法。开发者一般都会安装git。直接使用git bash生成一下就好了

进入git安装目录

 

运行git-bash.vbs

 

执行ssh-keygen

一路回车,完成后,记住下面的地址

 

进入该路径,发现有两个文件:

 

这两个文件的含是:首先,他们是1对的,两者缺1不可,id_rsa 是私钥,id_rsa.pub是公钥

步骤二、将公钥复制到authorized_keys文件中

小伙伴可能奇怪,vagrant都报错了,怎么还能进入虚拟机?没错!其实此时虚拟机已经启动完毕了,只不过此时不能接受vagrant的命令,也无法设置共享目录。

你只需要用客户端工具(例如:Xshell)登录虚拟机

一般来说默认的用户名是vagrant,密码也是vagrant。

 

进入.ssh目录

查看是否有authorized_keys文件


    
  1. [vagrant@localhost .ssh]$ pwd
  2. /home/vagrant/.ssh
  3. [vagrant@localhost .ssh]$ ls -al
  4. total 8
  5. drwx------ 2 vagrant root 28 Jul 26 2016 .
  6. drwx------. 8 vagrant vagrant 4096 Nov 6 11:02 ..
  7. -rw------- 1 vagrant vagrant 786 Jul 26 2016 authorized_keys
  8. [vagrant@localhost .ssh]$

如果authorized_keys文件不存在,则自己手动创建一下。
注意authorized_keys 文件的权限是600,所有者是vagrant,所属组也是vagrant


    
  1. [vagrant@localhost .ssh]$ touch authorized_keys
  2. [vagrant@localhost .ssh]$ chmod 600 authorized_keys

复制公钥

如果已经存在authorized_keys文件,复制物理机公钥文件id_rsa.pub的内容,粘贴到authorized_keys文件中。每个公钥只占一行
注意:公钥内容只占一行
例如我的authorized_keys文件中有两个公钥


    
  1. [vagrant@localhost .ssh]$ vim authorized_keys
  2. 1 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDlo4N35+9OM2UCAC82E4RtiqROZU/jI6bgg76QAA56cGdLwk4CNZzbn309nNRtO7tyBtWCyFx2AOn3Hd8hIFWiokMgxlf3eSjowT9dZqmbhGrYzAkPq r63rpHUX7M4FVjMLtoREqrGbBQZ7uZItViKeXXXl7bsGOUserLchzi+p3PJgjmw5j6ea+Kj2P7EThvcevoEPLcwGyckCTEiYo8nJ21K5bkmKCi2F8kaaJ9zbIeJ/2woayUkoZeufNo3A/gZx2bvHYAiFT 4RYLDwjrspq7pQS5Cs83YUGvolPKQfCrJRH3N+sNaeHx1NzEULMvQNxgEsFIVpi5k7OBIf4BY/ vagrant
  3. 2 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCov9Z/3qVWXkxLS6koRWxNu9lEt+e0+/6M+XCtDx7qWiCCZovNSCbKAHO3gwCV3myIyoiP/9bv2d0Sw18d/5BMYHWT4l85IZdF87no0Euu8Yt1w4BEU rCbL0jrDXHlVBhMyCeETr7BKDlM56meiNMo/PvNuN3qcp6tukLUXgrFRQ24hgg1mMvqQ0Km5UHYnHr+Vygc3udEVEEG5Px+04y6ap8gRZg7tKVgckdXZ7+1rNJtTXqR81uXXXbyown4eoccqsUTOK3iUs 2GdFwH/t3unbCSLu13UKDcLGG6hKG/x4aA1itIl3NdbzODgbte8UGXlifomayG+PTaf1tvb+n/ dc@GZ-Design003
步骤三、在物理机的Vagrantfile中添加以下内容

    
  1. config.ssh.private_key_path = "C:/Users/dc/.ssh/id_rsa"
  2. config.ssh.forward_agent = true

Vagrantfile全部配置内容如下:


    
  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. # All Vagrant configuration is done below. The "2" in Vagrant.configure
  4. # configures the configuration version (we support older styles for
  5. # backwards compatibility). Please don't change it unless you know what
  6. # you're doing.
  7. Vagrant.configure(2) do |config|
  8. config.vm.box = "web"
  9. config.ssh.private_key_path = "C:/Users/dc/.ssh/id_rsa"
  10. config.ssh.forward_agent = true
  11. # config.winnfsd.logging="on"
  12. # config.winnfsd.uid=1
  13. # config.winnfsd.gid=1
  14. # config.vm.synced_folder "./","/vagrant",type:"nfs"
  15. config.vm.define :web do |web|
  16. web.vm.provider "virtualbox" do |v|
  17. v.customize ["modifyvm", :id, "--name", "web", "--memory", "2048", "--cpus","2"]
  18. end
  19. web.vm.box = "web"
  20. web.vm.hostname = "web"
  21. web.vm.synced_folder "./","/vagrant"
  22. # web.vm.network:private_network, ip: "192.168.33.11"
  23. web.vm.network "public_network"
  24. end
  25. config.vm.define :php do |php|
  26. php.vm.provider "virtualbox" do |v|
  27. v.customize ["modifyvm", :id, "--name", "php", "--memory", "512"]
  28. end
  29. php.vm.box = "php"
  30. php.vm.network:private_network, ip: "192.168.33.10"
  31. # php.vm.network "public_network", ip: "192.168.33.10"
  32. php.vm.synced_folder "./","/vagrant",type:"nfs"
  33. php.winnfsd.logging="on"
  34. php.winnfsd.uid=1
  35. php.winnfsd.gid=1
  36. php.vm.synced_folder "./","/vagrant"
  37. end
  38. config.vm.define :swoole do |swoole|
  39. swoole.vm.provider "virtualbox" do |v|
  40. v.customize ["modifyvm", :id, "--name", "swoole", "--memory", "512"]
  41. end
  42. swoole.vm.box = "swoole"
  43. # swoole.vm.network:private_network, ip: "192.168.33.12"
  44. swoole.vm.network "public_network", ip: "192.168.33.12"
  45. # swoole.vm.synced_folder "./","/vagrant",type:"nfs"
  46. swoole.vm.synced_folder "./","/vagrant"
  47. end
  48. config.vm.define :master do |master|
  49. master.vm.provider "virtualbox" do |v|
  50. v.customize ["modifyvm", :id, "--name", "master", "--memory", "512"]
  51. end
  52. master.vm.box = "master"
  53. master.vm.network "public_network"
  54. master.vm.synced_folder "./","/vagrant"
  55. end
  56. config.vm.define :slave do |slave|
  57. slave.vm.provider "virtualbox" do |v|
  58. v.customize ["modifyvm", :id, "--name", "slave", "--memory", "512"]
  59. end
  60. slave.vm.box = "slave"
  61. slave.vm.network "public_network"
  62. slave.vm.synced_folder "./","/vagrant"
  63. end
  64. end
  65. # The most common configuration options are documented and commented below.
  66. # For a complete reference, please see the online documentation at
  67. # https://docs.vagrantup.com.
  68. # Every Vagrant development environment requires a box. You can search for
  69. # boxes at https://atlas.hashicorp.com/search.
  70. # Disable automatic box update checking. If you disable this, then
  71. # boxes will only be checked for updates when the user runs
  72. # `vagrant box outdated`. This is not recommended.
  73. # config.vm.box_check_update = false
  74. # Create a forwarded port mapping which allows access to a specific port
  75. # within the machine from a port on the host machine. In the example below,
  76. # accessing "localhost:8080" will access port 80 on the guest machine.
  77. # config.vm.network "forwarded_port", guest: 80, host: 8080
  78. # Create a private network, which allows host-only access to the machine
  79. # using a specific IP.
  80. # config.vm.network "private_network", ip: "192.168.33.10"
  81. # Create a public network, which generally matched to bridged network.
  82. # Bridged networks make the machine appear as another physical device on
  83. # your network.
  84. # config.vm.network "public_network"
  85. # Share an additional folder to the guest VM. The first argument is
  86. # the path on the host to the actual folder. The second argument is
  87. # the path on the guest to mount the folder. And the optional third
  88. # argument is a set of non-required options.
  89. # config.vm.synced_folder "../data", "/vagrant_data"
  90. # Provider-specific configuration so you can fine-tune various
  91. # backing providers for Vagrant. These expose provider-specific options.
  92. # Example for VirtualBox:
  93. #
  94. # config.vm.provider "virtualbox" do |vb|
  95. # # Display the VirtualBox GUI when booting the machine
  96. # vb.gui = true
  97. #
  98. # # Customize the amount of memory on the VM:
  99. # vb.memory = "1024"
  100. # end
  101. #
  102. # View the documentation for the provider you are using for more
  103. # information on available options.
  104. # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  105. # such as FTP and Heroku are also available. See the documentation at
  106. # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  107. # config.push.define "atlas" do |push|
  108. # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  109. # end
  110. # Enable provisioning with a shell script. Additional provisioners such as
  111. # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  112. # documentation for more information about their specific syntax and use.
  113. # config.vm.provision "shell", inline: <<-SHELL
  114. # sudo apt-get update
  115. # sudo apt-get install -y apache2
  116. # SHELL

步骤四、重启虚拟机

在物理机的命令行重启虚拟机

vagrant reload XXXX


转载:https://segmentfault.com/a/1190000011925921

文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/lxw1844912514/article/details/100029597

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。