9

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?

6

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
  • This worked for me! For those wondering, this makes Vagrant generate a new secure key, instead of using the old invalid one(if it even has one). – Nebojsac Feb 10 '15 at 18:00
5

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

1

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.

  • Enabling Virtualization from BIOS resolved my issue related "Vagrant SSH" – Firoz Sabaliya May 9 '16 at 10:05
0

A lot of the scripts I see use this to pull the public key:

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

The issue I've seen is that the github SSL cert is for www.github.com, not raw.github.com. Therefore, you end up getting a 400 error. You can verify this by looking at the contents of the /home/vagrant/.ssh/authorized_keys file.

Try using the -k option to ignore the SSL cert check:

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

Make sure that your VM has network access. In case you use Virtual Box, get in your machine setting, network tab and make sure your Cable Connected is checked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.