27

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?

neubert
  • 6,863
  • 35
  • 76
  • 138
  • 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 '20 at 07:54

9 Answers9

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.

Michael Cole
  • 717
  • 6
  • 8
  • 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 01 '21 at 18:32
  • This worked out of all hundred solutions over the internet +1 – Md. Abu Taher Jun 19 '21 at 14:20
  • @Md.AbuTaher There are two solutions in Michael Cole's answer. Which one worked for you? – luisdev Jun 21 '21 at 13:24
  • @luisdev I updated the answer to what I'm doing now. – Michael Cole Jun 21 '21 at 16:03
  • By far the most simplest and effective answer at the time I'm writing this (Jan. 1, 2021). Great work, thank you! This should be the accepted answer. – bananabrann Jan 03 '22 at 19:43
  • 1
    FYI as of now `localhostForwarding = true` is the default – Max Filippov May 27 '22 at 11:04
11

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/

Aris
  • 211
  • 2
  • 4
  • 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 '21 at 07:05
  • 1
    @luisdev Use a scheduled task to update the IP: https://abdus.dev/posts/fixing-wsl2-localhost-access-issue/ – abdusco Sep 20 '21 at 11:31
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.

Mark Scheck
  • 444
  • 3
  • 8
  • 8
    “I found the following blurb” - Please cite your source – Ramhound Oct 15 '20 at 08: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 '20 at 02:33
  • 2
    @neubert Use a scheduled task: https://abdus.dev/posts/fixing-wsl2-localhost-access-issue/ – abdusco Sep 20 '21 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.

Jano
  • 51
  • 1
  • 2
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
Edd
  • 141
  • 4
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

  • 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](https://superuser.com/a/1750410/1210833) that was posted less than a month ago. Thanks! – NotTheDr01ds Nov 29 '22 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

  • 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](/help/how-to-answer). – Community Feb 05 '23 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.

  • 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](/help/how-to-answer). – Community May 31 '23 at 08:12
-5

Restart the apache2 service:

sudo service apache2 restart
ZygD
  • 2,459
  • 12
  • 26
  • 43