28

I'm running Ubuntu 20.04 on WSL2 / Hyper-V / Windows 10.

When I do curl http://127.0.0.1 inside Ubuntu I get this back:

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.19.3</center>
</body>
</html>

When I do it on the host - on Windows 10 itself (with Git Bash) I get this:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused

How can I make port 80 accessible outside of Ubuntu? Do I need to update the firewall rules in Windows or something?

2
  • 1
    What are the results of ifconfig within WSL2 and ipconfig within Windows? As far as I know WSL is essentially a very lightweight virtual machine so a similar access method through the virtual network card should apply. Essentially you need the actual machine address as per the answer from Mark, and to ensure that firewalls are suitably permissive.
    – Mokubai
    Oct 15, 2020 at 7:54
  • 1
    If none of the suggestions work, check if C:\program files\wsl\wslrelay.exe is allowed to access the network. I was using a third-party firewall, and it blocked wslrelay.exe. Just like you, I was able to access the port inside WSL2 but not from the outside.
    – nharrer
    Dec 14, 2023 at 16:08

9 Answers 9

10

That's because each host has a localhost 127.0.0.1. The windows host thinks it's going to itself. The easiest solution is for you to IP with a real IP. example 192.168.x.x

I found the following blurb

In the early stages of WSL 2, we can’t use localhost. We need to use an IP since Linux is inside a VM.

To access your application currently, we will use an IP.

3
  • 9
    “I found the following blurb” - Please cite your source
    – Ramhound
    Oct 15, 2020 at 8:18
  • 1
    Doing ifconfig on Ubuntu to get the IP address (192.168.207.172) and then updating my Windows hosts file to use it seems to do the trick. Altho it is a bit annoying... what if the IP address changes? Having to update my hosts file every time I get a new IP address is a bit of a PITA
    – neubert
    Oct 16, 2020 at 2:33
  • 2
    @neubert Use a scheduled task: abdus.dev/posts/fixing-wsl2-localhost-access-issue
    – abdusco
    Sep 20, 2021 at 11:31
28

Problem:

Windows 10 thinks localhost is ::1.

PS C:\Users\michael> ping localhost

Pinging DESKTOP[::1] with 32 bytes of data:
Reply from ::1: time<1ms

Ubuntu thinks localhost is 127.0.0.1

$ ~ > cat /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       DESKTOP.localdomain     DESKTOP

Current Solution: 5/31/2022

I haven't been having this problem recently. I think I've done "Network Reset" once in the last 4 months.

'Network Reset' Solution: 7/27/2021

WSL is changing relatively quickly.

My current solution w/ WSL2 is

  1. "Start->Network Reset (system settings)->Reset
  2. Casually reboot windows like it's 1998.
  3. Get on with my life.

Solution 1:

  1. Read this documentation
  2. Notice the obscure note in wsl settings about true except for some builds.
  3. Add a file with these contents to C:\Users\Me\.wslconfig:
[wsl2]
localhostForwarding=true

This is what worked for me.

Solution 2:

This didn't work so well for me, but might for you. Stop automatic generation as instructed in the comments in /etc/hosts, and change localhost to ::1 so they refer to the same adapter. For compatibility, 0.0.0.0 binds to all interfaces (127.0.0.1 and ::1) so it works.

# 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       ip4-localhost
::1 localhost

...

Will this mess things up? Probably. If it does, you'll have to remember to fiddle this to get that to work.

6
  • 3
    OP should mark this as the correct solution, the localhostForwarding config did the trick for me. Now I don't have to open up VSCode every time I want to start a dev server (for some reason clicking the localhost URL output from VSCode's terminal worked, but it wouldn't work if I manually typed it into the browser). Now all is working as expected, thanks!
    – mrossman
    Jan 1, 2021 at 18:32
  • This worked out of all hundred solutions over the internet +1 Jun 19, 2021 at 14:20
  • @Md.AbuTaher There are two solutions in Michael Cole's answer. Which one worked for you?
    – luisdev
    Jun 21, 2021 at 13:24
  • @luisdev I updated the answer to what I'm doing now. Jun 21, 2021 at 16:03
  • 1
    FYI as of now localhostForwarding = true is the default May 27, 2022 at 11:04
12

This is what worked for me, as with localhost/127.0.0.1 I had no luck:

  1. get WSL2 linux IP using ifconfig:
aristos32@DESKTOP-V493N00:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.21.115.144  netmask 255.255.240.0  broadcast 172.21.127.255
        inet6 fe80::215:5dff:fea0:1a98  prefixlen 64  scopeid 0x20<link>
  1. use this IP in the browser instead of localhost

http://172.21.115.144/

2
  • 2
    My problem is that IP changes every time you restart the computer. I need an IP that never (or seldom) changes (or access to localhost in Ubuntu WSL).
    – luisdev
    Jun 23, 2021 at 7:05
  • 1
    @luisdev Use a scheduled task to update the IP: abdus.dev/posts/fixing-wsl2-localhost-access-issue
    – abdusco
    Sep 20, 2021 at 11:31
5

You can either use the IPv6 address for localhost in Windows http://[::1]/ or http://localhost. Both work for me with Windows 11 and WSL2.

2
  • 1
    Avoid posting answers to old questions that already have well received answers unless you have something substantial and new to add.
    – Toto
    Oct 31, 2022 at 16:50
  • 1
    This was the most relevant answer for me as of 2022. Jan 23, 2023 at 23:36
4

My solution was adding both 127.0.0.1 and ::1 for the local domain to the Windows host file.

127.0.0.1       mydomain.local
::1             mydomain.local
2

I'm running Ubuntu 20.04 on WSL2 / Windows 10 and running my backend(nodejs + koa) on ubuntu (localhost) and make request using postman from windows.

using the url http://127.0.0.1:port I could not access my server

looks funny but it worked for me to simply change the url (on postman) http://127.0.0.1:port to http://localhost:port

1
  • Welcome to Super User! I'd recommend reading through the existing answers before adding a new answer. This solution seems to be identical to this one that was posted less than a month ago. Thanks! Nov 29, 2022 at 16:34
2

date: 2023Feb.
Wha I am using: windows 10 with VSCode WSL2.
What worked for me?
Disabling the IP Helper service resolved it. Go to servcies.msc>disable it or set it to manual. You may also check the resource monitor tool in windows which shows the ports that are being used by the app. Source of my solution:
https://github.com/microsoft/WSL/issues/5575

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 5, 2023 at 19:27
1

Suggestion: Do NOT disable IPv6 on WSL2

Running 22.04 WSL2 Windows 10 - Same issues as reported above.

All the advice and tips I could find on the internet regarding this issue couldn't help me. Then a loose reference to IPv6 made me think.

In an attempt to fix a completely separate issue, I had taken some advice to disable IPv6 for WSL2 by adding

kernelCommandLine=ipv6.disable=1

into C:\Users\MYUSER.wslconfig

This indeed stops IPv6 inside WSL2 ... but it now turns out that this was responsible for killing my localhost access to WSL2 from Windows.

Re-enabling IPv6 for WSL2 got it all going again.

I am assuming Windows is trying to use the IPv6 ::1 localhost channel to get to WSL2 ?!?!?

... Just something else for you to consider in trying to resolve this problem.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 31, 2023 at 8:12
-5

Restart the apache2 service:

sudo service apache2 restart

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .