Windows 10, Vagrant, and VirtualBox

Getting Vagrant working properly on Windows can be a pain. This post is meant to help out people who may be running into issues by providing a couple of potential solutions to some of the most common issues.

There’s a few cases where I mention specific paths. For simplicity, this whole post assumes that Vagrant was installed to ‘C:\’.

Scenario: I just got the Windows 10 Anniversary Update, and now running when I “vagrant up” my host machine blue-screens!

Likely this means you have a combination of 2 things going on: You are using VirtualBox as your provider AND you have Hyper-V enabled. Some people think that it broke due to the added support for nested virtualization with Hyper-V which did come with the anniversary update. Who knows, but disabling Hyper-V seems to fix it for most people.

If you are still having issues, then make sure you have a fairly recent version of VirtualBox installed as some of the older versions also seem to have issues with the anniversary update.

Solution Summary: Disable Hyper-v. You can have the Management Tools installed, just not the platform. Update VirtualBox if you still have issues.

To check if Hyper-V is enabled, either navigate to “Turn Windows features on or off” or open PowerShell as an administrator and run the following:

(Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V).State

To disable Hyper-V (just the platform), run the command:

Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V

If it was enabled, you will need to restart before VirtualBox will work properly.

Scenario: I just installed Vagrant 1.8.5 and now SSH isn’t working.

Yup — you see the VM booting and then you see “default: Warning: Authentication failure. Retrying…” repeat until it fails/times out.

This is a well known issue with version 1.8.5. It’s simply an issue with the wrong permissions being applied to the ~/.ssh/authorized_keys file. You can read up on the issue here.

It will be fixed in 1.8.6, or you can fix it yourself. You can either a) login to the VM manually by launching the GUI, and then doing a chmod on the necessary file (see the link I posted for the change that needs to be made), b) add the line “config.ssh.insert_key = false” in your Vagrantfile (if this is not set, it defaults to ‘true’), or c) edit the public key Ruby file as explained here.

Or there is option d) go back to Vagrant 1.8.4, which has its own issues.

Solution Summary: Any of the above a, b, or c work, but I would suggest ‘c’ as you are guaranteed to only need to make the change one time. This will get you by until 1.8.6 comes out. Here’s a quick PowerShell snippet to fix the issue using method ‘c’:

$pub_file = "C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.8.5\plugins\guests\linux\cap\public_key.rb"
$pattern = "mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys"
$add = "chmod 0600 ~/.ssh/authorized_keys"
(Get-Content $pub_file) |
Foreach-Object {
$_
if ($_ -match $pattern){$add}} | sc $pub_file

Scenario: I cannot get folder syncing to work with certain Vagrant Boxes and I see all kinds of rsync errors!

You may experience errors that look like this when trying to do ‘vagrant up’

mm_receive_fd: no message header
process_mux_new_session: failed to receive fd 0 from slave
mux_client_request_session: read from master failed: Connection reset by peer
Failed to connect to new control master
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.1]

This could be any number of things, but let me mention some things that I wish I knew when starting out.

Anything before 1.8.5 is broken. Windows hosts will have issues with SSH and Vagrant on version 1.8.4 and below due to the SSH options it tries to use. The issue is described here and the fix can be found here.

Solution: Upgrade to Vagrant 1.8.5 or update the Ruby helper file found as described in the issue above. This file is located at C:\HashiCorp\Vagrant\embedded\gems\gems\\plugins\synced_folders\rsync\helper.rb

Or, if you want to do it with PowerShell (are you picking up on a theme yet?):

$vagrant_version = (gp HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | ? {$_.DisplayName -eq 'Vagrant' }).DisplayVersion
$helper_path = "C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-$($vagrant_version)\plugins\synced_folders\rsync\helper.rb"
(cat $helper_path) | ? {$_ -notmatch '"-o ControlMaster=auto " +'} | sc $helper_path
(cat $helper_path) | ? {$_ -notmatch '"-o ControlPath=#{controlpath} " +'} | sc $helper_path
(cat $helper_path) | ? {$_ -notmatch '"-o ControlPersist=10m " +'} | sc $helper_path

Other Considerations

Some other gotchyas that can bite you while running Vagrant on Windows

Make sure VirtualBox Guest Additions are installed if you are using VirtualBox.

You can do this by opening PowerShell as an administrator and typing:

vagrant plugin install vagrant-vbguest

Make sure your SSH and rsync clients are configured properly and in your path.

For all of this to work, you need more than just ‘vagrant’ in your path. You at least need ‘rsync’ and ‘ssh’ in your path as well. But remember: you can have more than one SSH and rsync client installed and in your path and they will be prioritized on their order!

This means you need to make sure that the SSH client that is prioritized in your path is one that works well with Vagrant. For example, if you have Git installed as well, and you install Cygwin afterwards, Git will likely be higher priority in your path (with an SSH binary) and not play nice with Vagrant.

I strongly suggest using Cygwin 32-bit (there can be issues with 64-bit and rsync) and moving it above any other potential path entries that may contain an SSH executable.  You can do that by searching “Edit the system environmental variables” and going through the GUI, your you can (wait for it) open PowerShell and run the following commands (make sure to change the <your_cygwin_install_path> to your actual path!) :

$cygwin_executables ="<your_cygwin_install_path>\bin;"
$Reg = "Registry::HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
$OldPath = (Get-ItemProperty -Path "$Reg" -Name PATH).Path
if ($OldPath.Contains($cygwin_executables)) {
$OldPath = $OldPath.Replace("$($cygwin_executables)","")
}
$NewPath= $cygwin_executables + $OldPath
Set-ItemProperty -Path "$Reg" -Name PATH –Value $NewPath

I recently made a janky script to automatically do some of this work for fresh installs. The script installs can install Vagrant, Cygwin, modify the path, and fix the SSH permissions issue with version 1.8.5. If nothing else, it contains most of the PowerShell snippets I used for this post. It can be found here.

2 thoughts on “Windows 10, Vagrant, and VirtualBox

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s