如何使用Vagrant up对VM进行身份验证?


9

当vagrant ssh和ssh vagrant @ localhost -p2222有效时,Vagrant Up期间身份验证失败

我想在启动时使用Vagrant执行Shell脚本。在使用vagrant up以下方法启动虚拟机时,Vagrant无法进行身份验证:

c:\temp\helloworld>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'helloworld'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: helloworld_default_1398419922203_60603
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Error: Connection timeout. Retrying...
    default: Error: Authentication failure. Retrying...
    default: Error: Authentication failure. Retrying...
    default: Error: Authentication failure. Retrying...
    default: Error: Authentication failure. Retrying...
    ...

执行后CTRL + C,可以使用vagrant ssh和向VM进行身份验证ssh vagrant@localhost -p2222

流浪文件

我使用默认的Vagrantfile,并且只更改了主机名:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "helloworld"
  ...

流浪版本

c:\temp\helloworld>vagrant --version
Vagrant 1.5.1

如何使用对VM进行身份验证vagrant up

Answers:


6

引起此问题的原因是,“ Vagrant”框上没有公钥。以下两个选项之一可以解决此问题。

第一种选择是使用Packer创建一个新的Vagrant盒子。将以下代码段添加到json文件中,并构建Vagrant框。

"provisioners": [{
    "type": "shell",
    "scripts": [
      "scripts/vagrant.sh"
    ]
}]

游标脚本的内容如下:

#!/bin/bash
yum install wget -y

mkdir /home/vagrant/.ssh
wget --no-check-certificate \
    'https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub' \
    -O /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh
chmod -R go-rwsx /home/vagrant/.ssh

第二个选项是在运行了此处vagrant package指定的以下命令后,重新打包()Vagrant框:

mkdir -p /home/vagrant/.ssh
wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
chmod 0700 /home/vagrant/.ssh
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh

这对我有用!对于那些想知道的人,这使Vagrant生成了一个新的安全密钥,而不是使用旧的无效密钥(如果它甚至有一个密钥)。
Nebojsac 2015年

5

首先,尝试:查看您的计算机配置中有哪些无用的私钥

$ vagrant ssh-config

例:

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/Users/konst/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

http://docs.vagrantup.com/v2/cli/ssh_config.html

其次,做:用您自己的系统私钥的内容 更改文件insecure_private_key的内容


1

我也无法超越:

默认值:SSH身份验证方法:私钥

当我使用VirtualBox GUI时,它告诉我操作系统处理器不匹配。

为了使无所事事的事情进一步发展,在BIOS设置中,我不得不凭直觉来反驳:

禁用:虚拟化

启用:VT-X

尝试在BIOS中切换这些设置。


从BIOS启用虚拟化解决了我有关“流浪SSH”的问题
Firoz Sabaliya

0

我看到的许多脚本都使用此脚本来提取公钥:

curl -L https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -o /home/vagrant/.ssh/authorized_keys

我看到的问题是github SSL证书用于www.github.com,而不是raw.github.com。因此,您最终会得到一个400错误。您可以通过查看/home/vagrant/.ssh/authorized_keys文件的内容来验证这一点。

尝试使用该-k选项忽略SSL证书检查:

curl -L -k https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -o /home/vagrant/.ssh/authorized_keys

0

确保您的VM具有网络访问权限。如果您使用Virtual Box,请进入机器设置,“网络”选项卡,并确保已选中“已连接电缆”。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.