Skip to content

Cannot connect to WSL:localhost(127.0.0.1) ports from Windows Host. But can connect to WSL:0.0.0.0 ports #6725

Closed
@brucejo75

Description

@brucejo75

Up until yesterday afternoon I had no problem connecting to my WSL2 listeners with my portproxy setup. Suddenly, it stopped working for me. I was working with the hosts file on Windows, but I believe that I put it back in good working order.

I found a lot of similar issues: #6638, #4851, #5018, #5298

Many of them allude to this issue, but do not isolate this specific problem.

Could I have a config issue in my Hosts files somewhere?

Environment

Microsoft Windows [Version 10.0.19042.870]
Ubuntu Release: 20.04
WSL2

Steps to Reproduce

  1. WSL: nc -l 127.0.0.1 9229
  2. WSL: sudo lsof -nP -iTCP -sTCP:LISTEN:
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nc      3729 brucejo    3u  IPv4  17148      0t0  TCP 127.0.0.1:9229 (LISTEN)

Clue?: Note the listener address: 127.0.0.1:9229
3. Windows Host: telnet localhost 9229 (I would have used ncat on the Windows side, but I could not find a Windows ncat that did not alarm Windows Defender. Any suggestions?)

Expected: Telnet to connect and stay connected
Actual behavior: Telnet session connects but then disconnects after a couple of seconds.

Workaround

Don't use localhost, use 0.0.0.0.

  1. WSL: nc -l 0.0.0.0 9229
  2. WSL: sudo lsof -nP -iTCP -sTCP:LISTEN:
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nc      3729 brucejo    3u  IPv4  17148      0t0  TCP *:9229 (LISTEN)

Clue?: Note the listener address: *:9229

  1. Windows Host: telnet localhost 9229
    Result: telnet session connects and stays connected.

Notes:

  1. I cannot reach any port listener on WSL:127.0.0.1.
  2. I can reach port listeners on WSL:0.0.0.0
  3. My Windows Hosts file has no address for localhost, comment says that DNS handles it.
  4. If I ping localhost from Windows Host, I get Pinging DEVPC [::1] with 32 bytes of data. It is apparently set to the ipv6 address? Could this be part of the problem?
  5. I am trying to understand why this suddenly stopped working, I want to undo whatever I did!

Thing I tried

  1. Modify WSL /etc/hosts file as per Local sites running in WSL2 not accessible in browser #5298 (comment)

Background data

Port forwards, Firewall
  1. WSL: Get {WSL IPADDR}: ip addr, ip address of eth0:.
  2. Windows Host: Make sure I have a portproxy for port 9229: netsh interface portproxy add v4tov4 listenport=9229 connectport=9229 connectaddress={WSL IPADDR}
netsh interface portproxy show all

Listen on ipv4:             Connect to ipv4:

Address         Port        Address         Port
--------------- ----------  --------------- ----------
*               5980        172.20.47.47    5980
*               4980        172.20.47.47    4980
*               5880        172.20.47.47    5880
*               4880        172.20.47.47    4880
*               4881        172.20.47.47    4881
*               5838        172.20.47.47    5838
*               6838        172.20.47.47    6838
*               9229        172.20.47.47    9229
  1. Windows Host: Make sure Hyper-V forwarding was enabled:
Set-NetIPInterface -InterfaceAlias "vEthernet (WSL)" -forwarding Enabled
Set-NetIPInterface -InterfaceAlias "vEthernet (Default Switch)" -forwarding Enabled
  1. Windows Host: Made sure firewall was open:
Get-NetFirewallRule -DisplayName 'WSL 2 Firewall Unlock' | Format-Table -Property DisplayName,
>> @{Name='Protocol';Expression={($PSItem | Get-NetFirewallPortFilter).Protocol}},
>> @{Name='LocalPort';Expression={($PSItem | Get-NetFirewallPortFilter).LocalPort}},
>> @{Name='RemotePort';Expression={($PSItem | Get-NetFirewallPortFilter).RemotePort}},
>> @{Name='RemoteAddress';Expression={($PSItem | Get-NetFirewallAddressFilter).RemoteAddress}},
>> Enabled,
>> Profile,
>> Direction,
>> Action

DisplayName           Protocol LocalPort                   RemotePort RemoteAddress Enabled Profile Direction Action
-----------           -------- ---------                   ---------- ------------- ------- ------- --------- ------
WSL 2 Firewall Unlock TCP      {5980, 4980, 5880, 4880...} Any        Any              True     Any  Outbound  Allow
WSL 2 Firewall Unlock TCP      {5980, 4980, 5880, 4880...} Any        Any              True     Any   Inbound  Allow
Windows C:\Windows\System32\drivers\etc\hosts
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#       127.0.0.1       localhost
#       ::1             localhost
192.168.1.99 MEDIA mpc
172.18.168.97 vm.lcl
192.168.1.112 mac
192.168.1.100 devpc.ip

# Dynamic section modified for VMs
172.21.142.138 mc.vm
172.29.181.6 mc4.vm
172.20.45.128 wsl
WSL /etc/hosts
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1       localhost
127.0.1.1       DEVPC.localdomain       DEVPC
��

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions