WSL: Local sites running in WSL2 not accessible in browser

When starting a site/app locally on my computer in WSL 2 ( Ubuntu 20.04 LTS (GNU/Linux 4.19.84-microsoft-standard x86_64)), the browser won’t connect to the local server.

This happens every time I start my computer (has been happening for a weekish now), but only when starting after the computer has been shut down. If I restart the computer, the local servers work fine

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: Running a local server for dev site. Happens regardless of site. eg.:

// Eleventy Site
$ npx @11ty/eleventy --serve

[Browsersync] Access URLs:
 --------------------------------------
       Local: http://localhost:8080
    External: http://172.25.237.22:8080
 --------------------------------------
          UI: http://localhost:3001
 UI External: http://localhost:3001
 --------------------------------------
[Browsersync] Serving files from: _site
[Browsersync] Watching files...


// Gatsby site
$ gatsby develop

success run queries - 5.329s - 9/9 1.69/s
 DONE  Compiled successfully in 2177ms                                                                               2:10:28 PM
⠀
 I  Netlify CMS is running at http://localhost:8000/admin/
⠀
You can now view climate-clever in the browser.
⠀
  http://localhost:8000/
⠀
View GraphiQL, an in-browser IDE, to explore your site's data and schema
⠀
  http://localhost:8000/___graphql
⠀
Note that the development build is not optimized.
To create a production build, use gatsby build
⠀
success Building development bundle - 3.192s
  • What’s wrong / what should be happening instead: I should be able to access the apps at the provided local URL (localhost:8080 for eleventy and localhost:8000 for Gatsby), but when loading the page in the web browser it says it’s unable to connect (in both Firefox and Edge)

  • 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). image image

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 9
  • Comments: 33

Most upvoted comments

To avoid disabling Fast Startup, restarting WSL as pointed in #4636 worked for me (wsl --shutdown).

This happens every time I start my computer (has been happening for a weekish now), but only when starting after the computer has been shut down. If I restart the computer, the local servers work fine

That hint sounds a lot like Fast Startup. I always disable it and had no localhost problems so far. https://www.tenforums.com/tutorials/4189-turn-off-fast-startup-windows-10-a.html

When will this be available for non insider Users?

Not sure if this will help anyone but I had the same with firebase and React. The issue turned out to be:

  • in WSL2, /etc/hosts lists ‘localhost’ as ‘127.0.0.1’
  • in Windows (build 19041) ‘localhost’ resolves to ::1
  • so the server in WSL was listening on 127.0.0.1 and the browser was trying to reach ::1

My fix was to remove the 127.0.0.1 alias from /etc/hosts, and add ‘localhost’ to the ::1 line - after that it worked like a charm, though that could have side effects in other software.

Until the bug is fixed, make sure you disable Fast Startup. If you still have localhost forwarding problems with Fast Startup disabled then it’s another issue.

@matze19999 Same here - would you mind providing your bash script? Thank you

#!/bin/bash

NEWIP=`ifconfig | grep eth0 -A1 | tail -n1 | cut -d ' '  -f10 `

# delete IP from windows hosts file
sed '/wsl/d' -i '/mnt/c/Windows/System32/drivers/etc/hosts' || true

echo "$NEWIP		wsl" >> '/mnt/c/Windows/System32/drivers/etc/hosts'

Thats the script. Make sure your user has the permission to modify / create files in the “/Windows/System32/drivers/etc/” folder because sed will create a temp file in this directory. Run this script everytime you start wsl and you’ll be fine.

same problem here on 19640.mn_release.200530-1731 on Surface Pro X. wsl --shutdown doesn’t work

I had to revert back to WSL1.

@Scarysize @matze19999 That’s another issue. To access WSL2 from other computer you need port forwarding. See https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723

Had the issue, fixed it temporarily (until bug is fixed) by disabling Fast Startup as recommended above. Alternatively, opening a Powershell script, and issue wsl --shutdown, re-open WSL terminal brought me back to business every-time.

Didn’t work for me, I fixed it by writing a little bash script, which modifies the windows host file everytime wsl is starting. Works perfectly 😃

Had the issue, fixed it temporarily (until bug is fixed) by disabling Fast Startup as recommended above. Alternatively, opening a Powershell script, and issue wsl --shutdown, re-open WSL terminal brought me back to business every-time.

I’m in the same situation, except for the fact that I disabled Fast startup a long time ago. I have a Ubuntu 20.4 WSL2 vm that last week became unreachable by ssh. For the first time, restarting the pc did not fix the issue. No logged firewall interference. wsl --shutdown restart is no good either, nor is a service restart. Sometimes, but it’s becoming increasingly rare, I find the connection to be working until the next reboot. Ugly to get back to a plain vm, after having a taste of the deeply integrated features WSL1. In the name of progress…

Awesome, that’s working now! At least I have a workaround for now (that doesn’t require me restarting my computer as soon as I turn it on)

I wonder what the issue is that’s making it not work when computer has fast booted