Vagrant ssh fails with VirtualBox

39

13

vagrant up fails when it gets to the ssh part:

myterminal$ vagrant up
[default] VM already created. Booting if its not already running...
[default] Running any VM customizations...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- ssh: 22 => 2222 (adapter 1)
[default] -- db2: 30003 => 30003 (adapter 1)
[default] Cleaning previously set shared folders...
[default] Creating shared folders metadata...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] Failed to connect to VM!
Failed to connect to VM via SSH. Please verify the VM successfully booted
by looking at the VirtualBox GUI.

Then when I subsequently try and connect using vagrant ssh or vagrant reload or similar, I get this:

myterminal$ vagrant reload
[default] Attempting graceful shutdown of linux...
SSH connection was refused! This usually happens if the VM failed to
boot properly. Some steps to try to fix this: First, try reloading your
VM with `vagrant reload`, since a simple restart sometimes fixes things.
If that doesn't work, destroy your VM and recreate it with a `vagrant destroy`
followed by a `vagrant up`. If that doesn't work, contact a Vagrant
maintainer (support channels listed on the website) for more assistance.

Please help! I'm really stumped.

Kind regards,

Luke

lukewm

Posted 2011-01-13T14:24:31.190

Reputation:

Answers

10

I saw this problem with my setup and eventually narrowed it down to the fact that I'd added a name onto the local hosts file. So I had:

127.0.0.1 localhost myname myalias

Removed the myalias which I'd added and it booted okay. Thanks to @saphirlazuli for the clue that it was networking related.

Mark Cheverton

Posted 2011-01-13T14:24:31.190

Reputation: 146

What is the workaround if I want to preserve myalias? – Mickey Cheong – 2011-09-10T12:10:01.503

If you want to use an alias, the Debian/Ubuntu way is to keep the first line as just 127.0.0.1 localhost, but add a second line with your aliases, like so: 127.0.1.1 foo bar baz – CodeGnome – 2012-04-05T07:43:10.527

26

I have had the same problem with the box given in the "Getting started" of Vagrant site. My solution below is for this tutorial's box.

I find a solution here : https://github.com/mitchellh/vagrant/issues/391

  1. in the VagrantFile, add the gui mode : config.vm.boot_mode = :gui
  2. run vagrant up
  3. in the gui, log with the user "vagrant" (password = "vagrant")
  4. int the gui, run sudo /etc/init.d/networking restart, in /etc/network/interfaces file, you must now have a section with #VAGRANT-BEGIN [...]#VAGRANT-END
  5. run vagrant reload on the physical machine

saphirlazuli

Posted 2011-01-13T14:24:31.190

Reputation:

1I get The following settings shouldn't exist: boot_mode – Calin – 2016-03-24T14:50:34.827

1Got in the latest version you have to put v.gui = true for the provider config – Calin – 2016-03-24T14:52:45.783

I also fixed this problem by temporarily disabling bridged mode. – Andy – 2016-08-01T16:39:14.703

3This worked for me, thanks. I had Vagrant running before, then it stopped working with a new box. Any idea what causes it? – Andrew Vit – 2011-07-02T04:57:42.823

sudo /etc/init.d/networking restart is worked for me, thanks! – Anatoly – 2011-09-12T07:40:04.047

2I did restart, but in /etc/network/interfaces I don't have section with #VAGRANT-BEGIN [...]#VAGRANT-END, any clue why? Can I add it manually? – sparrovv – 2011-10-07T08:02:52.920

sudo dhclient works too – Ngo Minh Nam – 2011-11-14T16:30:42.007

6

This is an intermittent problem for me, and definitely seems to be related to networking. I found some more helpful information at Vagrant GitHub issues #391 and #455. I tried deleting my unused host-only network in VirtualBox settings (as recommended as a potential fix in one of the GitHub issues) which didn't seem to help.

Starting in gui mode (as described in a previous answer) is a great way to debug/fix problems. I found that it's because for whatever reason, the VM cannot obtain an IP address on startup. Starting in GUI mode then logging in (vagrant/vagrant) and running sudo dhclient in the guest OS would retrieve the IP address and the Vagrant setup process would complete successfully.

Still looking for a permanent fix though.

Hope this helps!

[EDITED TO ADD]: A helpful suggestion was added to GitHub Issue #455 which involves adding the following config to your Vagrantfile:

config.ssh.max_tries = 150

This is currently working for me, and seems to make sense since the problem seems to be related to timeout - increasing the number of tries should increase the probability of the SSH process succeeding in time. It's an intermittent issue in the first place so I'll reserve judgement for a while, but fingers crossed it'll work! Thanks to karel1980 for suggesting the fix.

Dawngerpony

Posted 2011-01-13T14:24:31.190

Reputation: 363

Deleting my unused host-only network in VirtualBox settings worked for me! Thank you! – Duke – 2013-09-21T16:32:39.077

1

It seems that this setting was depricated as of 1.3.X - https://github.com/berkshelf/berkshelf/pull/856

– Jeremy Hajek – 2014-02-19T21:58:50.413

5

If vagrant up fails, I recommend the following:

  1. Boot with gui mode: this helps to see if the image is broken (or e.g. what it does before the provisioning is started), set config.vm.boot_mode = :gui in your Vagrantfile
  2. Debug logging: run the provisioning with debug, chef.log_level = :debug
  3. Repackage the box: sometimes you need to install something before everything else runs - to bootstrap the bootstrapper. That's not really possible, so the easiest is to boot a vm without any provisioning, install whatever you need and re-package the box (vagrant box repackage foo, then vagrant import foo and then add foo in your Vagrantfile)

Note: gui mode can have other issues, e.g. for me no provisioning works with vagrant 0.7.5 when gui is enabled. It's still great to debug the actual box though.

Till

Posted 2011-01-13T14:24:31.190

Reputation: 387

2

My network card type was originally set to 82540EM. I changed it to Am79C973 and now Vagrant up works every time. I've started multiple VMs, even rebooted the host and it still works.

The command is:

VBoxManage modifyvm yourmachinename --nictype1 Am79C973

Remember to repackage and readd your box to Vagrant.

I'm running Debian Squeeze 64-bit as host and guests, using VirtualBox 4.1.16 on a Core i7-2600 machine.

Simon Celen

Posted 2011-01-13T14:24:31.190

Reputation: 11

1

If the kernel module is already installed, try starting the VM directly with VirtualBox. This will show you any startup errors encountered as the VM boots. Problems like filesystem errors will stop the SSH server daemon from operating correctly.

Sasha

Posted 2011-01-13T14:24:31.190

Reputation:

1

I had the same problem on osx (VirtualBox 4.1.0 and Vagrant 0.8.6), and for me the only help was to:

  1. login with :gui by login/pass: vagrant/vagrant
  2. modify the “/etc/rc.local” file to include the line “sh /etc/init.d/networking restart” just before “exit 0″.
  3. disable :gui
  4. vagrant reload

I've found this at: https://github.com/mitchellh/vagrant/issues/391 thx mikhailov.

sparrovv

Posted 2011-01-13T14:24:31.190

Reputation: 111

1

I had this same problem. In my case 64-bit emulation wasn't enabled. Enabling this feature in my bios fixed the issue.

diedthreetimes

Posted 2011-01-13T14:24:31.190

Reputation: 101

1

My guest box would not boot properly due to a disk issue.

I forced the VM to close via

VBoxManage controlvm poweroff

Then I modified my Vagrantfile to include this line:

config.vm.boot_mode = :gui

Next, I re-ran

vagrant up

In the GUI that popped up, I had to run the 'disk fixer' utility.

After running the disk utility, my VM was successfully booted, and then I could run vagrant ssh.

Kevin Meredith

Posted 2011-01-13T14:24:31.190

Reputation: 255

1

I uncommented the part in the Vagrantfile like this:

  config.vm.provider :virtualbox do |vb|
    # Don't boot with headless mode
    vb.gui = true

    # Use VBoxManage to customize the VM. For example to change memory:
    # vb.customize ["modifyvm", :id, "--memory", "1024"]
  end

I saved it and then I did

vagrant reload

and then things worked fine. I'm not sure why that fixed it though.

Pavan Katepalli

Posted 2011-01-13T14:24:31.190

Reputation: 135

0

As with others, I used the GUI (I always have this on to prevent problems like this)

config.vm.boot_mode = :gui

I then used the VirtualBox menu, as the commands here didn't work. Machine > Reset in the menu, then ssh was then possible.

mahemoff

Posted 2011-01-13T14:24:31.190

Reputation: 803

0

Type sudo /etc/init.d/vboxdrv setup . It should now work.

Daniel

Posted 2011-01-13T14:24:31.190

Reputation: 45

0

My problem was that in

/etc/hosts

I had entry

127.0.1.1 lucid32

where as my box name was lucidtest, I changed the above line to

127.0.1.1 lucidtest

and it worked.

Owais Lone

Posted 2011-01-13T14:24:31.190

Reputation: 303

0

This message suggests ssh server is not running on the VM. make sure it is installed

sudo apt-get install openssh-server

and running

ssh localhost

should not give an error.

To exclude errors within vagrant, you should try ssh into the vm directly by:

ssh vagrant@localhost -p2222

(assuming default user and port forwarding )

Viktor Trón

Posted 2011-01-13T14:24:31.190

Reputation: 131

0

On windows 7 run

vagrant ssh

if you get something like this...

`vagrant ssh` isn't available on the Windows platform. You are still able
to SSH into the virtual machine if you get a Windows SSH client (such as
PuTTY). The authentication information is shown below:

Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: U:/.vagrant.d/insecure_private_key

Then you should use something like this...

ssh vagrant@127.0.0.1 -p 2222 -i /cygdrive/u/.vagrant.d/insecure_private_key

Notice I am using cygwin on my windows box rather than Putty.

Artistan

Posted 2011-01-13T14:24:31.190

Reputation: 291

OP is not using Windows 7. – Tanner Faulkner – 2012-10-29T20:25:01.850

Thank you r.tanner.f -- No OS was specified, this helped me, hopefully helps others. – Artistan – 2012-10-30T13:26:33.457

Don't know why you were downvoted, this seems like it could help other people – Mark – 2013-07-17T15:45:12.347

0

I shut down the running vm and created a new basebox:

  1. Shut down VM I am trying to export
  2. vagrant package --base "salt-master basebox" --vagrantfile ./Vagrantfile

Zippy Zeppoli

Posted 2011-01-13T14:24:31.190

Reputation: 101

0

Here is another possible reason for the error:

[default] Waiting for VM to boot. This can take a few minutes.
[default] Failed to connect to VM!
Failed to connect to VM via SSH. Please verify the VM successfully booted
by looking at the VirtualBox GUI.

A username can be specified in the config file like this:

config.ssh.username = "badusername"

If that username does not exist or cannot be logged into for some reason, neither you nor Vagrant will be able to SSH into the VM. Thus Vagrant will "fail to connect", even if the VM is in fact running and working properly.

SunSparc

Posted 2011-01-13T14:24:31.190

Reputation: 193

0

My issue was that vagrant ssh wouldn't do anything at all.

I removed ~/.ssh/config and the problem was solved. Copy the file somewhere or rename it if you want to keep it.

zMan

Posted 2011-01-13T14:24:31.190

Reputation: 101

on local or remote system? – Jeremy Hajek – 2018-08-19T02:39:50.197