How to authenticate to a VM using Vagrant up?

Authentication failure during Vagrant Up, while vagrant ssh and ssh vagrant@localhost -p2222 works

I would like to execute a shell script using Vagrant at boot. Vagrant is unable to Authenticate, while the VM has been started using vagrant up:

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...
    ...

After executing CTRL + C it is possible to authenticate to the VM using vagrant ssh and ssh vagrant@localhost -p2222

Vagrant file

I use the default Vagrantfile and I only changed the hostname:

# -*- 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"
  ...

Vagrant version

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

Question

How to authenticate to VM using vagrant up?


Solution 1:

The issue was caused because no Public key reside on the Vagrant box. One of the following two options solves the issue.

The first option is to create a new Vagrant box using Packer. Add the following snippet to the json file and build the Vagrant box.

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

The content of this vagrant script is as follows:

#!/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

The second option is to repackage (vagrant package) the Vagrant box once the following commands specified here have been run:

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

Solution 2:

First, try: to see what vagrant private key in you machine config

$ vagrant ssh-config

Example:

$ 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

Second, do: Change content of file insecure_private_key with content of your own system private key

Solution 3:

I also could not get beyond:

default: SSH auth method: private key

When I used the VirtualBox GUI, it told me there was an OS processor mismatch.

To get vagrant up progressing further, in the BIOS settings I had to counter-intuitively:

Disable: Virtualisation

Enable: VT-X

Try toggling these setting in your BIOS.