Ask
26
@loopback_lior ·

Reaching a service on Windows from inside WSL — sometimes localhost works, sometimes it does not, and I cannot see the pattern Networking

Running a database on Windows and an application inside the distro. Connecting to localhost from Linux works on my desktop and fails on my laptop, same setup as far as I can tell.

The other direction — a server inside Linux, browser on Windows at localhost — works reliably on both machines.

So the asymmetry is real and I would like the model rather than a workaround I do not understand:

  1. Why is one direction reliable and the other not?
  2. What address should Linux actually use to reach Windows?
  3. Why would two machines behave differently with the same versions?
3 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @hyperv_hakan · 3h ago

    For question 2, use the address the distro already knows, rather than localhost. Inside Linux:

    ip route show default | awk '{print $3}'
    

    That gives the gateway, which is the Windows host on the virtual network. It changes on every restart, which is exactly why hardcoding it into a config is a bad afternoon.

    So do not hardcode it. Options in order of how well they hold up:

    • Resolve $(hostname).local if your setup provides it
    • Read the gateway at startup in your app's config
    • Switch WSL to mirrored networking mode, in .wslconfig:
    [wsl2]
    networkingMode=mirrored
    

    Mirrored mode makes the distro share the host's network interfaces, so localhost genuinely means the same thing on both sides in both directions. It is the real fix for your problem rather than a workaround, and it also tends to survive VPNs, which the shim does not.

    24
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @vhdx_vera · 3h ago · 2 replies

    The asymmetry is the whole answer, and it exists because only one direction has special handling.

    In its default networking mode the distro is a virtual machine on its own network, with its own address. Two separate hosts.

    Linux → Windows gets no help. There is a forwarding shim that makes localhost work, but it is doing translation on the fly and it is the fragile half — VPN clients, a firewall profile flipping to Public, third-party network filters and some corporate agents all break it. That is your laptop.

    Windows → Linux is the direction that gets real support: when something inside the distro listens on a port, Windows publishes it on localhost automatically. That is why it never fails for you.

    So it is not that one is broken. It is that one is a convenience layer and the other is a supported feature.

    27
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @loopback_lior · 3h ago

      That explains why every fix I found was for my direction specifically. I had assumed the two were symmetrical and one of them had a bug.

      12
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report