34

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
  • 7,574
  • 39
  • 94
  • 156

11 Answers11

30

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, or, respectively, C:\Users\%UserProfile%\.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.

zx485
  • 2,337
19

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
  • 291
  • 2
  • 4
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
  • 474
  • 3
  • 9
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
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
3

To anyone on Windows 11 still having this issue, I worked on this tirelessly for 3 days, until I finally came across the the app shortcut labeled "WSL Settings" while in the windows search bar about ohhh.... 3 minutes ago.

Search bar

Under "Network Settings":

  1. Set Networking Mode to "Mirrored" (Default = "NAT")
  2. Turn on "Host Address Loopback"

WSL Settings screen

Worked immediately afterwards. My Home Assistant running on Pi5 finally connected to my separate desktop machine.

Hope this helps someone.

Markus Meyer
  • 1,910
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

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

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.

0

Check the firewall, the default one for me was UFW

iptables -Lf

ufw status

another common issue : use other network flag rather than loopback or dhcp check ip address by ifconfig and use the desired ip- in my case "inet 172.30.122.107"

-5

Restart the apache2 service:

sudo service apache2 restart
ZygD
  • 2,577