Connection Refused in Vagrant using WSL 2

9.3k views Asked by At

I am trying WSL 2, also trying Vagrant on it. I used it in Ubuntu 20.04 and it is working properly, but now i am facing problem here in WSL 2 in Windows 10. My Problem might be about SSH problem on WSL, seems 127.0.0.1:222 cannot connect into the image in Virtual Box. please correct me if my understanding is wrong

Technologies Version Involved

  • Windows 10 build 19042.630
  • Vagrant version 2.2.14 both inside WSL and Windows 10
  • WSL 2 - Ubuntu 20.04
  • Virtual Box 6.1 only in Windows 10 host

Vagrant File

Vagrant.configure("2") do |config|
    config.vm.box = "base"
    config.ssh.username = "vagrant"
    config.ssh.password = "vagrant"
    config.vm.define "laravel_envoy" do |laravel_envoy|
      laravel_envoy.vm.hostname = "192.168.12.12"
      laravel_envoy.vm.box = "bento/ubuntu-18.04"
      laravel_envoy.vm.network :private_network, ip: "192.168.12.12"
      laravel_envoy.vm.provider "virtualbox" do |bionicv|
        bionicv.memory = 4096
        bionicv.cpus = 4
        bionicv.customize ["modifyvm", :id, "--uartmode1", "disconnected"]
      end
      laravel_envoy.vm.synced_folder "/mnt/c/Users/jjmigz-win10/Code/gcp-home","/home/vagrant/Code/gcp-home"
    end

    config.vm.network "forwarded_port", guest: 8992, host: 8991, host_ip: "127.0.0.1"

  end

Error Raw Text

jmigzwsl@jmigzw10:/mnt/c/Users/jjmigz-win10/Code/laravel_gitlab_ci_cd_with_envoy$ vagrant up

Bringing machine 'laravel_envoy' up with 'virtualbox' provider...
==> laravel_envoy: Clearing any previously set forwarded ports...
==> laravel_envoy: Clearing any previously set network interfaces...
==> laravel_envoy: Preparing network interfaces based on configuration...
    laravel_envoy: Adapter 1: nat
    laravel_envoy: Adapter 2: hostonly
==> laravel_envoy: Forwarding ports...
    laravel_envoy: 8992 (guest) => 8991 (host) (adapter 1)
    laravel_envoy: 22 (guest) => 2222 (host) (adapter 1)
==> laravel_envoy: Running 'pre-boot' VM customizations...
==> laravel_envoy: Booting VM...
==> laravel_envoy: Waiting for machine to boot. This may take a few minutes...
    laravel_envoy: SSH address: 127.0.0.1:2222
    laravel_envoy: SSH username: vagrant
    laravel_envoy: SSH auth method: password
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
    laravel_envoy: Warning: Connection refused. Retrying...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

~/.bashrc config

export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
export PATH="$PATH:/mnt/c/Program Files/Oracle/VirtualBox"
export VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH="/mnt/c/Users/jjmigz-win10/Code/laravel_gitlab_ci_cd_with_envoy"

What I tried

  • i tried to change my host_ip: "127.0.0.1" into my Vagrantfile but still i got error, also I cannot find real solution like this problem using WSL 2 in the internet.

  • i tried to add this config.vm.boot_timeout in the Vagrantfile but still got error

  • i tried to add this bionicv.gui = true in my Vagrantfile but still got error

Related Question - outdated and not using WSL 2

2

There are 2 answers

0
Karandash8 On BEST ANSWER

A bit of monkey patching and here you go. Just install the plugin vagrant plugin install virtualbox_WSL2 and vagrant ssh works from WSL2.

Source code: https://github.com/Karandash8/virtualbox_WSL2

0
Jose Mantilla On

After looking into the matter and debugging all vagrant messages, try one more time on wsl2 by using these steps:

  1. vagrant plugin install virtualbox_WSL2
  2. Add to your Vagrantfile the following line: config.vm.network :forwarded_port, guest: 22, host: 2522, auto_correct: false, id: "ssh"

Note: Don't set any user.

My example

Vagrant.configure("2") do |config|
    # General configuration
    config.vm.box = "generic/rhel9"
    config.vm.hostname = "kiosk.localdomain"

    config.vm.network "private_network", ip: "192.168.56.10"
 config.vm.network :forwarded_port, guest: 22, host: 2522, auto_correct: false, id: "ssh"
      auto_config= false
 config.vm.synced_folder '.', '/vagrant', disabled: true
end

I'm using virtual box on win 11 + wsl2 and that was the solution, enjoy it!