19

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?

1
  • 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

5 Answers 5

7

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
  • 6
    “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
18

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: 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
  • 2
    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
  • 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. Jan 3 at 19:43
4

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
3

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
-3

Restart the apache2 service:

sudo service apache2 restart

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.