I recently bought a newer laptop. It had Windows 8 preinstalled which I wiped immediately in order to install Linux Mint 19.1 and started to migrate data from my old machine. I packaged my vagrant box and transferred it to the new machine. Once imported the box back and issued vagrant up command I was surprised by the following errors:
==> 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...
I tried to connect to the vagrant box using ssh command and default user vagrant/password vagrant and succeeded.
ssh vagrant@127.0.0.1 -p 2222
Something went wrong with the SSH keys while backing up and/or restoring the box. I had the described problem so many times, actually. Usually it happens after packaging and then restoring the Vagrant box. There is pretty simple solution to this issue that always saves me: you just need to adjust your Vagrantfile in order to instruct Vagrant to insert it’s actual public key into the box.
Vagrant.configure("2") do |config| --- config.ssh.username = "vagrant"; config.ssh.password = "vagrant"; config.ssh.insert_key = true; --- end
Once the key is inserted you can remove the above configuration. Next time you type vagrant ssh Vagrant will log you in using the SSH key instead of password authentication.
This approach always worked to me, but this time the above configuration did nothing. I don’t really know why, tried to debug and find more information (and hopefully solution) on the github page of the project without luck.
So, I thought I can insert the correct SSH public key into the vagrant box manually but I had only the private key. What I did is to generate public key from the private one and set it in ~/.ssh/authorized_keys inside the box.
First, run the following command to view where the private key is located:
vagrant ssh-config
The command returns output similar to this one:
Host default HostName 127.0.0.1 User vagrant Port 2222 --- IdentityFile /home/stz184/.vagrant.d/boxes/ofterize-VAGRANTSLASH-lemp72/0/virtualbox/vagrant_private_key ---
The line of interest begins with IdentityFile.
Next, generate a public key and upload it to the guest machine
<br data-mce-bogus="1"> ssh-keygen -y -f /home/stz184/.vagrant.d/boxes/ofterize-VAGRANTSLASH-lemp72/0/virtualbox/vagrant_private_key > authorized_keys scp -P $port authorized_keys vagrant@localhost:~/.ssh/ vagrant ssh -c "chmod 600 ~/.ssh/authorized_keys" rm authorized_keys
Enjoy!