How to access service running on host from WSL2 (connection refused)

windows-subsystem-for-linux

I have selenium running on my host machine, and my app is inside a docker container (inside WSL2).

I am trying to get the app connect to the selenium, that is listening on port 4445.
It used to work a few months ago, I think something changed in WSL.

Host is listening on 4445:

PS> netstat -ano | findstr :4445
  TCP    0.0.0.0:4445           0.0.0.0:0              LISTENING       11604
  TCP    [::]:4445              [::]:0                 LISTENING       11604

I can access selenium from the windows host machine:

>curl -X POST http://DESKTOP-HED9HVG:4445/wd/hub
{"state":".....}

but not from WSL2:

$ curl -X POST http://172.22.241.214:4445/wd/hub
curl: (7) Failed to connect to 172.22.241.214 port 4445: Connection refused

I tried several options for the ip that I used in curl:

  • ip addr eth0 ip
  • $(hostname)
  • ip addresses from the results of ipconfig /all | findstr IPv4
  • ip address result of route -n | grep UG | head -n1 | awk '{print $2}'

I installed tcptraceroute on WSL and run it. This is the output:

$ tcptraceroute $(hostname) 4445
Selected device lo, address 127.0.0.1, port 53915 for outgoing packets
Tracing the path to DESKTOP-WXYZ1 (127.0.1.1) on TCP port 4445, 30 hops max
 1  DESKTOP-WXYZ1.localdomain (127.0.1.1) [closed]  0.075 ms  0.082 ms  0.074 ms

By the way, pinging from WSL to host does work:

$ ping $(hostname)
PING DESKTOP-WXYZ1.localdomain (127.0.1.1) 56(84) bytes of data.
64 bytes from DESKTOP-WXYZ1.localdomain (127.0.1.1): icmp_seq=1 ttl=64 time=0.053 ms

I tried to disable completely the windows firewall, but it doesn't help. I also added a rule in "Windows Defender Firewall" to enable port 4445 specifically. It still didn't help

Info about WSL:

>wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-20.04           Running         2
  docker-desktop         Running         2
  docker-desktop-data    Running         2

Any idea how to solve this?

Best Answer

See guidance here

https://docs.microsoft.com/en-us/windows/wsl/networking#accessing-windows-networking-apps-from-linux-host-ip

If you want to access a networking app running on Windows (for example an app running on a NodeJS or SQL server) from your Linux distribution (ie Ubuntu), then you need to use the IP address of your host machine. While this is not a common scenario, you can follow these steps to make it work.

Obtain the IP address of your host machine by running this command from your Linux distribution: cat /etc/resolv.conf Copy the IP address following the term: nameserver. Connect to any Windows server using the copied IP address. The picture below shows an example of this by connecting to a Node.js server running in Windows via curl.

You will also need to allow inbound connections to that port in the host. (Through a firewall rule).

Related Topic