(Last Updated On: October 18, 2018)
Sometimes when you package VirtualBox VM to get a vagrant box, you may get the message “Warning: Authentication failure. Retrying… ” while trying to start the vagrant machine. Check

Step by step guide on using existing virtual machines with Vagrant

I tried to use vagrant ssh-config to generate a private key for Vagrant, then add it to Vagrantfile but it didn’t work out for me. The key on .vagrant/machines/default/virtualbox/private_key won’t authenticate and login successfully.
The error message is shown below:
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...

default: Adapter 1: nat
default: Adapter 2: bridged
==> 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: Connection timeout. Retrying...
default: Error: Connection timeout. Retrying...
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...
default: Error: Authentication failure. Retrying...

If you have encountered the same problem, this solution is for you:

Prerequisites:

  1. Installed vagrant: Install Latest Vagrant on Ubuntu 18.04 / Debian 9 & Kali Linux
  2. Vagrant Instance already added to VirtualBox, VMware or any other provider you use.
  3. SSH service on Vagrant instance set to start on boot up

Step 1: Generate SSH Keys

Generate ssh private and public keys using the ssh-keygen command.
My vagrant appliance working directory is ~/vagrant/centos-asterisk-server. This is where SSH keys will reside.

mkdir -p ~/vagrant/centos-asterisk-server/.ssh

Generate private/private RSA key pair with ssh-keygen

ssh-keygen

I will use the following settings:

Enter file in which to save the key : /home/$USER/vagrant/centos-asterisk-server/.ssh/id_rsa
Enter passphrase (empty for no passphrase): <Press Enter/Return key>
Enter same passphrase again: <Press Enter/Return key>

This will successfully generate RSA 2048 keys. You can set a passphrase or later Change or update SSH key Passphrase on Linux / Unix

Step 2: Configure Vagrant

Edit Vagrantfile and specify the location of generated private key

vim ~/vagrant/centos-asterisk-server/Vagrantfile

We’ll add it just below config.vm.box = "" line.We’ll also set “config.ssh.forward_agent” to True.

config.ssh.private_key_path = " /home/josepy/vagrant/centos-asterisk-server/.ssh/id_rsa"
config.ssh.forward_agent = true

My username and password that vagrant we’ll login in with are the default:

username: vagrant
password: vagrant

If you have different username and password, specify them on Vagrantfile

nano Vagrantfile

Add:

config.ssh.username = 'username'
config.ssh.password = 'password'

Save the changes and quit.

Copy RSA public key to the vagrant instance. We’ll start the Virtual Machine with VirtualBox directly.

Make sure ssh is running and port forwarding is configured. Follow steps below

  1. Open Oracle VirtualBox ( GUI)
  2. Right click on the name of Virtual Machine and go to settings > Network > Adapter 1 ( NAT) > Port Forwarding

Make sure SSH port forwarding is set, if not add it by clicking on the + sign
Name: ssh
Protocol: TCP
Host ip: 127.0.0.1
Host Port: 2200
Guest port: 22

 

  1. Copy rsa public key to Virtual Machine
ssh-copy-id -f -i /home/josepy/vagrant/centos-asterisk-server/.ssh/id_rsa.pub -p 2200 [email protected]

  1. Stop Virtual Machine ( power off) and start it with vagrant
cd /home/josepy/vagrant/centos-asterisk-server/
vagrant up

You can confirm that key was copied to guest os. Do

nano $HOME/.ssh/authorized_keys

Now start Vagrant instance using vagrant up command. It should start without any error,