Sometimes when you package VirtualBox VM to get vagrant box, you get message “Warning: Authentication failure. Retrying… ” while trying to start vagrant machine. Check

Step by step guide on using existing virtual machines with Vagrant

I tried to use vagrant ssh-config to generate 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 same problem,this solution may help you.
Prerequisites:

  1. Installed vagrant
  2. Vagrant Instance already added to VirtualBox,Vmware or any other provider you use.
  3. SSH on Vagrant instance set to start on boot up

Step 1: Generate ssh private and public keys using ssh-keygen command.
My vagrant appliance working directory is : ~/vagrant/centos-asterisk-server. This is the same directory that i will put my private and public keys

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

Generate private/private rsa key pair with ssh-keygen

ssh-keygen

I will use following settings:
Enter file in which to save the key : /home/josepy/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>
It will successfully generate RSA 2048 keys. Remember i didn;t set passphrase ( just pressed enter key)

Step 2: Edit Vagrantfile and specify location of generated private key

vi ~/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.

Step 3: Copy rsa public key to 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 + 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 vagrant@127.0.0.1

  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,

  • Uaru

    This is very complicated solution. I need to provide people with a way to have a host with a couple of configured tools. Being forced to do so many steps beats the purpose of providing this VM. But there is simpler solution.

    1. Control-C when you see ‘default: Error: Connection timeout. Retrying…’
    2. vagrant ssh, password: vagrant
    3. chmod 644 .ssh/authorized_keys
    4. exit
    5. vagrant up
    6. vagrant provision

    From this moment on, we are in the place we wanted to be initially.
    Anyway, apparently there is no problem for Ubuntu hosts.

    • Thanks for the comment Uaru. It’s indeed simplified solution.

  • Rohit

    This helped. Thanks!!