Vagrant: Private network not getting set up running under WSL2 (same Vagrantfile fine under Windows Vagrant)

Created on 24 Jun 2020  路  10Comments  路  Source: hashicorp/vagrant

Vagrant version

Vagrant 2.2.9

Host operating system

Windows 10 (2004, May 2020)

Guest operating system

Ubuntu 16.04 (tried with others).

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "geerlingguy/ubuntu1604"
  config.vm.network :private_network, ip: "172.16.88.8"
  config.ssh.insert_key = false

  config.vm.synced_folder '.', '/vagrant', disabled: true

  config.vm.provider :virtualbox do |v|
    # For debugging.
    v.gui = true
    v.memory = 1024
  end
end

Debug output

https://gist.github.com/geerlingguy/2ccba46ce0c16d049d068cb2157d92dc

Expected behavior

The VirtualBox VM should be started, Vagrant should be able to connect to it (instead I get default: Warning: Connection refused. Retrying... over and over, and I can't connect via SSH), and the private network interface should be added (172.16.88.8).

Actual behavior

  • Vagrant inside WSL2 can't connect to the VM it just started.
  • Manually logging into the VM via VirtualBox GUI reveals only the NAT interface (10.0.2.15) and localhost:

85497683-fa307d80-b5a3-11ea-83c4-a6cdb12999cb

Note that when I run vagrant up under the _Windows_ environment via PowerShell, the correct interfaces are brought up, and Vagrant is able to connect to the VM:

Screen Shot 2020-06-24 at 8 47 02 AM

Steps to reproduce

  1. Install WSL2 on Windows 10 (2004), with Ubuntu bash
  2. Create a directory with the above Vagrantfile in it
  3. Download and install Vagrant for Linux in the WSL2 environment (regardless of it being installed in Windows)
  4. Run vagrant up

References

WSL hoswindows

Most helpful comment

I've the exact same issue with virtualbox provider. any help is greatly appreciated.

All 10 comments

I also tried:

  1. Run vagrant up in Powershell.
  2. Run vagrant ssh in WSL2.

But I get the error message:

$ vagrant ssh
==> default: This machine used to live in C:/Users/jgeerling/Downloads/ansible-101/episode-14/ansible-for-devops/drupal but it's now at /mnt/c/Users/jgeerling/Downloads/ansible-101/episode-14/ansible-for-devops/drupal.
==> default: Depending on your current provider you may need to change the name of
==> default: the machine to run it as a different machine.

So I also tried vagrant provision inside WSL2, but Ansible couldn't connect to the localhost IP:

fatal: [default]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 2222: Connection refused", "unreachable": true}

So then I tried using raw SSH:

$ ssh -p 2222 [email protected]
ssh: connect to host 127.0.0.1 port 2222: Connection refused

But if I started the VM via Powershell (which sets up the host-only private network correctly, unlike in WSL2), I _can_ connect via raw SSH inside WSL2 through that private network:

$ ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-142-generic x86_64)

vagrant@drupal:~$

So it looks like maybe a network boundary issue when vagrant is running inside WSL? It seems that 127.0.0.1 won't resolve through to Windows at all (maybe it only resolves to the Ubuntu environment), and additionally, Vagrant running inside WSL2 is unable to set up the private network on 172.16.88.8 in VirtualBox, which is running in the Windows environment?

Just as a note, I am able to get vagrant provision to run with an Ansible provisioner inside WSL2 on a machine that I started using vagrant up over in Windows, but I had to create a custom Ansible inventory file and set that in the provisioner config, then I also had to copy the insecure_private_key into a path on the Linux filesystem so I could modify its permissions (otherwise it's always 0777 and SSH fails since the key is open to everyone), and then I set the key file path in Ansible's inventory config, so in the end, I can hack around things to get Ansible-based provisioning working through Vagrant on WSL2 with a VM that was built using Vagrant in Powershell, but this setup is not very portable and requires a lot of hand-wringing.

See full details of the workaround in this comment: https://github.com/geerlingguy/ansible-for-devops/issues/291#issuecomment-648847520

I too am facing the same problem on 2.2.9. However I am unable to run vagrant up in Powershell as I just get the error below.

         1: from C:/HashiCorp/Vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/plugins/hosts/suse/host.rb:20:in `open'
C:/HashiCorp/Vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/plugins/hosts/suse/host.rb:20:in `initialize': Cannot translate name. @ rb_sysopen - /etc/os-release (Errno::ELOOP)

Otherwise experiencing same "missing network" on VM + WSL + Host as experienced in OP. Same Windows verison, same Vagrant verison, etc.

it's looks like vagrant cannot access network file in wsl 2, when I move vagrant file to windows directory, its work properly.

wsl directory from powershell

PS Microsoft.PowerShell.Core\FileSystem::\\wsl$\Ubuntu\home\faisal\workspace\src\github.com\faisalburhanudin\vagrant-sample> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
Traceback (most recent call last):
        20: from C:/HashiCorp/Vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/lib/vagrant/batch_action.rb:86:in `block (2 levels) in run'
        19: from C:/HashiCorp/Vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/lib/vagrant/machine.rb:198:in `action'
        18: from C:/HashiCorp/Vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/lib/vagrant/machine.rb:198:in `call'
        17: from C:/HashiCorp/Vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/lib/vagrant/environment.rb:613:in `lock'
        16: from C:/HashiCorp/Vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/lib/vagrant/machine.rb:212:in `block in action'
        15: from C:/HashiCorp/Vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/lib/vagrant/machine.rb:240:in `action_raw'
        14: from C:/HashiCorp/Vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/lib/vagrant/action/runner.rb:34:in `run'

windows directory

PS C:\Users\faisal\Documents\test> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'generic/ubuntu2004' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'generic/ubuntu2004'
    default: URL: https://vagrantcloud.com/generic/ubuntu2004
==> default: Adding box 'generic/ubuntu2004' (v3.0.20) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/generic/boxes/ubuntu2004/versions/3.0.20/providers/virtualbox.box
Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com

Hey, I was wondering if anyone happened to get around this? It seems like a vagrant/wsl2 limitation but here's to hoping 馃

I have the same problem in I get Warning Connection refused. Retrying. I can ssh log into the created machine with Putty.

Bringing machine 'web01' up with 'virtualbox' provider...
==> web01: Clearing any previously set forwarded ports...
==> web01: Clearing any previously set network interfaces...
==> web01: Preparing network interfaces based on configuration...
web01: Adapter 1: nat
==> web01: Forwarding ports...
web01: 80 (guest) => 8080 (host) (adapter 1)
web01: 22 (guest) => 2222 (host) (adapter 1)
==> web01: Running 'pre-boot' VM customizations...
==> web01: Booting VM...
==> web01: Waiting for machine to boot. This may take a few minutes...
web01: SSH address: 127.0.0.1:2222
web01: SSH username: vagrant
web01: SSH auth method: private key
web01: Warning: Connection refused. Retrying...

Vagrantfile Vagrant.configure("2") do |config|
config.vm.define "web01" do |web01|
web01.vm.box = "v0rtex//xenial64"
web01.vm.network "forwarded_port", guest: 80, host: 8080
web01.vm.hostname = "web01"
web01.ssh.forward_agent = true
web01.ssh.port = 2222
web01.vm.synced_folder ".", "/vagrant", disabled: true
web01.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
end
web01.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end

Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal

Is there any update on this? I've spent two days trying to configure WSL2 with Vagrant and am constantly having the same issue for Ansible's connection to localhost being refused. I've tried multiple fixes suggested and none of made any progress. I can see the box is running and can connect it to it using username and password in the virtualbox interface, but this always hangs on this step and refuses the connection and throws the following:

System info:
Ansible 2.8.0; Vagrant 2.2.14; Linux

Trellis version (per changelog): "Validate that letsencrypt_contact_emails is a list"

Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port
2200: Connection refused
fatal: [default]: UNREACHABLE! => {"changed": false, "unreachable": true}

Constant problems like this with workarounds that don't actually work are making Vagrant very frustrating to use with WSL2.

I've just encountered the same issue

I've the exact same issue with virtualbox provider. any help is greatly appreciated.

Same thing here. ssh from wsl2 times out on first vagrant up, even disabling key authentication and fallback to password.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hesco picture hesco  路  3Comments

RobertSwirsky picture RobertSwirsky  路  3Comments

barkingfoodog picture barkingfoodog  路  3Comments

DreadPirateShawn picture DreadPirateShawn  路  3Comments

Cbeck527 picture Cbeck527  路  3Comments