Skip to content

WSL2 cannot connect to localhost when the service is running on Windows #5211

Closed

Description

Please fill out the below information:

  • Your Windows build number: (Type ver at a Windows Command Prompt)
    Microsoft Windows [Version 10.0.19041.264]

  • What you're doing and what's happening: (Copy&paste the full set of specific command-line steps necessary to reproduce the behavior, and their output. Include screenshots if that helps demonstrate the problem.)
    I am using an HTTP proxy (ShadowSocks) on windows which is listening on localhost port 1080. Then, I want the command in WSL2 to go through that proxy.
    image

  • What's wrong / what should be happening instead:
    curl: (7) Failed to connect to 127.0.0.1 port 1080: Connection refused

  • Strace of the failing command, if applicable: (If some_command is failing, then run strace -o some_command.strace -f some_command some_args, and link the contents of some_command.strace in a gist here).

  • For WSL launch issues, please collect detailed logs.

In WSL1, all things are fine. In WSL2 I could connect to the HTTP proxy through my Windows IP. I believe it is caused by WSL2 running in a separate VM.

Activity

  1. bingzhangdai commented on May 14, 2020

    @bingzhangdai
    Author

    To simplify the issue, I will show the telnet result, 172.21.80.1 is the Windows IP on WSL switch.
    image

  2. nunix commented on May 14, 2020

    @nunix

    hi,
    the issue here is that while Windows can see the localhost ports of WSL, the inverse in not true by default.
    The ports forwarding are from WSL to Windows. If you do a netstat -an | grep 1080 on WSL, it should return nothing.
    However, when you pass it trough the Gateway (WSL switch), it will reach it.

    Going forward, you can create a tunnel from Windows to WSL if you need/want to use localhost in your proxy (see: netsh interface portproxy command)

    Hope this helps.
    WSL Corsair

  3. bplasmeijer commented on May 14, 2020

    @bplasmeijer

    See also #5131
    Nuno do Carmo (@nunix) do you see this as default behavior with the portproxy on WSL?

  4. bingzhangdai commented on May 15, 2020

    @bingzhangdai
    Author

    Thanks for the reply. I notice that many related issues are all about the network between the two systems. I can create a tunnel or proxy for this. But may I know if the WSL team has a specific plan or eta for the network improvement? Thanks.

  5. nunix commented on May 15, 2020

    @nunix

    let's wait for Build (and here I really am like you, I'm waiting on news) and see if something will done.
    Bart Plasmeijer (@bplasmeijer) I remember the SSH, actually right now, netsh is what I use for many of my demos.

    still, I do agree with you that the "reverse forwarding" from Windows to WSL2 would be a great improvement

  6. bplasmeijer commented on May 15, 2020

    @bplasmeijer

    let's wait for Build (and here I really am like you, I'm waiting on news) and see if something will done.
    Bart Plasmeijer (@bplasmeijer) I remember the SSH, actually right now, netsh is what I use for many of my demos.

    still, I do agree with you that the "reverse forwarding" from Windows to WSL2 would be a great improvement

    cc: Craig Loewen (@craigloewen-msft) Ben Hillis (@benhillis) Hayden Barnes (@sirredbeard)

  7. gencer commented on May 18, 2020

    @gencer

    hi,
    the issue here is that while Windows can see the localhost ports of WSL, the inverse in not true by default.
    The ports forwarding are from WSL to Windows. If you do a netstat -an | grep 1080 on WSL, it should return nothing.
    However, when you pass it trough the Gateway (WSL switch), it will reach it.

    Going forward, you can create a tunnel from Windows to WSL if you need/want to use localhost in your proxy (see: netsh interface portproxy command)

    Hope this helps.
    WSL Corsair

    Nuno do Carmo (@nunix) can you give us an example? For example; I am trying to access 10134 port from WSL. 10134 port is on Windows assigned by Logitech Options application. I would like to access this port from inside WSL.

    Is it possible to do with the portproxy? Currently my WSL IP is: 172.27.80.1

  8. eromoe commented on May 19, 2020

    @eromoe

    same problem , we need to use proxy on windows localhost

  9. fatichar commented on May 27, 2020

    @fatichar

    In my case, I am unable to connect to a service running on windows on ANY IP. Always the response is:

    ~/tcc/network-info$ telnet  172.17.0.1 5432
    Trying 172.17.0.1...
    telnet: Unable to connect to remote host: Connection refused
    
  10. eromoe commented on May 28, 2020

    @eromoe

    Pravin Gajendra Chaudhary (@fatichar) turn off your firewall , it cost me serveral hours to get it out . This is difinitely another big problem

  11. bingzhangdai commented on May 28, 2020

    @bingzhangdai
    Author

    Actually, what I am looking for is something even more about proxy between WSL2 and Windows. I am expecting similar experiences like WSL1, where WSL and Windows are using the same address (at least users should not be bothered if services on WSL and Windows are listening on different addresses) and follow the same firewall rules.

    If the network is not unified, why not use VM directly and using Samba to share the filesystem? WSL1 provide grate convenience compared with VM. 🤔

    Am I too greedy? 😂

  12. nunix commented on May 28, 2020

    @nunix

    hi,
    the issue here is that while Windows can see the localhost ports of WSL, the inverse in not true by default.
    The ports forwarding are from WSL to Windows. If you do a netstat -an | grep 1080 on WSL, it should return nothing.
    However, when you pass it trough the Gateway (WSL switch), it will reach it.
    Going forward, you can create a tunnel from Windows to WSL if you need/want to use localhost in your proxy (see: netsh interface portproxy command)
    Hope this helps.
    WSL Corsair

    Nuno do Carmo (@nunix) can you give us an example? For example; I am trying to access 10134 port from WSL. 10134 port is on Windows assigned by Logitech Options application. I would like to access this port from inside WSL.

    Is it possible to do with the portproxy? Currently my WSL IP is: 172.27.80.1

    hi Gencer W. Genç (@gencer) I would suppose it's possible indeed, but on powershell if you do a netstat -an, on which interface is port 10134 mapped?
    if it's localhost, then WSL2 cannot reach it indeed, if it's bound to 0.0.0.0 then from WSL2 distro, try connecting to the :10134

    Let me know the output of netstat and I will provide you the command

  13. gencer commented on May 28, 2020

    @gencer

    Nuno do Carmo (@nunix), Unfortunately it binds on localhost as follow:

      TCP    127.0.0.1:10134        0.0.0.0:0              LISTENING
      TCP    127.0.0.1:10134        127.0.0.1:50456        ESTABLISHED
      TCP    127.0.0.1:10134        127.0.0.1:50833        ESTABLISHED
      TCP    127.0.0.1:10134        127.0.0.1:56123        ESTABLISHED
      ... and vice-versa
      TCP    127.0.0.1:56123        127.0.0.1:10134        ESTABLISHED
      TCP    127.0.0.1:50456        127.0.0.1:10134        ESTABLISHED
    

    However, we make it work on SSH/WSL by making vscode extension runing on UI side. This makes us to access localhost port and data.

  14. djfos commented on Oct 11, 2020

    @djfos

    you can access Windows port from WSL2 using your local IP like 192.168.31.66 instead of localhost, though is not elegant

  15. toryano0820 commented on Nov 15, 2020

    @toryano0820

    I also have experienced this issue when accessing my Windows' Redis instance from my project running in WSL2. End up mapping Nameserver to localhost in /etc/hosts.

    You can find Nameserver from /etc/resolv.conf, it looks like: nameserver 172.18.144.1
    Then you can add line in /etc/hosts like: 172.18.144.1 localhost

    Created a shell script to make life simpler: https://gist.github.com/toryano0820/6ee3bff2474cdf13e70d972da710996a

    Not sure yet if there's a downside with this method. But it works!

  16. 59 remaining items

  17. pwang2 commented on Aug 9, 2023

    @pwang2

    This is a frustrating topic. Exposing all the interfaces from windows via 0.0.0.0 is INSECURE. it would be great and makes most sense to just keep it behave the same as a loopback interface in WSL..

  18. ludobegins commented on Oct 27, 2023

    @ludobegins

    That moment when you scroll the whole thread since 2020 hoping at this point someone would have a good fix but nope 😂😕

  19. MarcLongoria commented on Oct 27, 2023

    @MarcLongoria

    That moment when you scroll the whole thread since 2020 hoping at this point someone would have a good fix but nope 😂😕

    No joke. I've been following this thread since it was opened, hoping someone far more intelligent than me would have solved this by now. Yet here we are lol

  20. obedm503 commented on Oct 27, 2023

    @obedm503

    This was fixed in #4619

  21. dons20 commented on Oct 28, 2023

    @dons20

    Update since Major Version 2.0.9

    For step 2, you can just do wsl --update only, no need to do the pre-release anymore.


    Original

    Obed (@obedm503) Thanks for pointing it out. Would have never known it was actually being addressed all this time.

    Had to take a few steps to get it working on my local environment. Just thought I'd share. The changes are on a pre-release of WSL so it won't be available through regular update paths.

    1. Update Windows to at least the October 10th build. See more info here: [WSL 2] WSL 2 cannot access windows service via localhost:port #4619 (comment)
    2. Update your WSL to the latest pre-release by running this in CMD or Powershell wsl --update; wsl --update --pre-release. You can view the changes of the pre-release versions here: https://github.com/microsoft/WSL/releases
    3. Update (or create) your .wslconfig file which is located at %USERPROFILE% in explorer. Add the following:
      [wsl2]
      networkingMode=mirrored # add this line if file already exists
      
    4. (Optional) Shutdown and restart your WSL (after a few seconds) so that the config changes apply.

    Since getting it enabled, I haven't had a single issue with my localhost being inaccessible. I could safely enable IPV6 again too.

    Side note, pre-release v2.0.5 https://github.com/microsoft/WSL/releases/tag/2.0.5 moved the networkingMode setting from [experimental] to [wsl2].

  22. seri0zha commented on Oct 31, 2023

    @seri0zha

    Keno C (@dons20) thank you so much, it works fine for me. I have spent a lot of time trying to resolve this problem

  23. MarcLongoria commented on Oct 31, 2023

    @MarcLongoria

    Awesome to hear! The release mentions it's a window 11 fix. Anyone on Windows 10 to confirm it works for them as well?

  24. Jacksonmills commented on Nov 15, 2023

    @Jacksonmills

    Obed (@obedm503) Thanks for pointing it out. Would have never known it was actually being addressed all this time.

    Had to take a few steps to get it working on my local environment. Just thought I'd share. The changes are on a pre-release of WSL so it won't be available through regular update paths.

    1. Update Windows to at least the October 10th build. See more info here: [[WSL 2] WSL 2 cannot access windows service via localhost:port #4619 (comment)](https://github.com/microsoft/WSL/issues/4619#issuecomment-1778054020)
    
    2. Update your WSL to the latest pre-release by running this in CMD or Powershell `wsl --update; wsl --update --pre-release`. You can view the changes of the pre-release versions here: https://github.com/microsoft/WSL/releases
    
    3. Update (or create) your `.wslconfig` file which is located at `%USERPROFILE%` in explorer. Add the following:
       ```
       [wsl2]
       networkingMode=mirrored # add this line if file already exists
       ```
    
    4. (Optional) Shutdown and restart your WSL (after a few seconds) so that the config changes apply.
    

    Since getting it enabled, I haven't had a single issue with my localhost being inaccessible. I could safely enable IPV6 again too.

    Side note, pre-release v2.0.5 https://github.com/microsoft/WSL/releases/tag/2.0.5 moved the networkingMode setting from [experimental] to [wsl2].

    This worked for me!

  25. teapot2 commented on Nov 16, 2023

    @teapot2

    Keno C (@dons20)

    This worked for me too on Windows 11. Thank you!

  26. derharry commented on Dec 27, 2023

    @derharry

    Keno C (@dons20) Thanks! Works for me on W11.

    This issue, WSL cannot connect to remote localhost, on my side had to do when the connected WiFi or LAN was set to "public network". On "private network" everything was fine.
    With this fix WSL also connects to localhost while in public network. :-)

  27. Vividious commented on Jan 20, 2024

    @Vividious

    Keno C (@dons20) cheers mate!
    For real, over a year of struggle

  28. mamlzy commented on May 11, 2024

    @mamlzy

    Update since Major Version 2.0.9

    For step 2, you can just do wsl --update only, no need to do the pre-release anymore.

    Original

    Obed (@obedm503) Thanks for pointing it out. Would have never known it was actually being addressed all this time.

    Had to take a few steps to get it working on my local environment. Just thought I'd share. The changes are on a pre-release of WSL so it won't be available through regular update paths.

    1. Update Windows to at least the October 10th build. See more info here: [WSL 2] WSL 2 cannot access windows service via localhost:port #4619 (comment)
    2. Update your WSL to the latest pre-release by running this in CMD or Powershell wsl --update; wsl --update --pre-release. You can view the changes of the pre-release versions here: https://github.com/microsoft/WSL/releases
    3. Update (or create) your .wslconfig file which is located at %USERPROFILE% in explorer. Add the following:
      [wsl2]
      networkingMode=mirrored # add this line if file already exists
      
    4. (Optional) Shutdown and restart your WSL (after a few seconds) so that the config changes apply.

    Since getting it enabled, I haven't had a single issue with my localhost being inaccessible. I could safely enable IPV6 again too.

    Side note, pre-release v2.0.5 https://github.com/microsoft/WSL/releases/tag/2.0.5 moved the networkingMode setting from [experimental] to [wsl2].

    Works! thank you so much!

  29. ghost closed this as completedon May 28, 2024
  30. misternian commented on Mar 18, 2025

    @misternian

    Keno C (@dons20) save my time. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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