12

The issue with my WSL is the following my WSL can't make a cURL request anywhere under localhost. For this example, I have created a page that returns hello world. My cURL looks like this (note that the response is the same even with http://)

curl -vvv localhost:80/hello_world.html

and the response is the following

*   Trying 127.0.0.1...
* TCP_NODELAY set
* connect to 127.0.0.1 port 80 failed: Connection refused
* Failed to connect to localhost port 80: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 80: Connection refused

now if i run the following cURL on Powershell curl http://localhost:80/hello_world.html

the response I get is

StatusCode        : 200
StatusDescription : OK
Content           : <h1> Hello World </h1>
RawContent        : HTTP/1.1 200 OK
                    Keep-Alive: timeout=5, max=100
                    Connection: Keep-Alive
                    Accept-Ranges: bytes
                    Content-Length: 22
                    Content-Type: text/html
                    Date: Mon, 24 Feb 2020 09:52:20 GMT
                    ETag: "16-59f4f46118cab...
Forms             : {}
Headers           : {[Keep-Alive, timeout=5, max=100], [Connection, Keep-Alive], [Accept-Ranges, bytes], [Content-Length, 22]...}    Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : System.__ComObject
RawContentLength  : 22

which returns the actual hello world and not the error message i get on WSL

eg curl: (7) Failed to connect to localhost port 80: Connection refused

EDIT:

the server runs in windows 10 machine and the curl also in the same windows 10 machine. Also, the windows are on Insider version 19569.100

UPDATE: I checked out this Microsoft article that @Anaksunaman shared in the comments and followed the step that says to cat /etc/resolv.conf then from there I got my IP and tried the curl with my IP from resolv.conf and it worked but still with localhost doesn't seem to work also in the article it says

Depending on which version of Windows you're using, you might need to retrieve the IP address of the virtual machine. If your build is 18945 or higher, you can use localhost just like normal.

My build is higher than 18945 as I'm a windows insider user. Is there any way that I can map my IP (EG 111.222.333.444) to localhost? so when I do a CURL to localhost the curl actually uses the IP 111.222.333.444?

7
  • To clarify, is the server running under Windows and the plain curl command under WSL? – Anaksunaman Feb 24 '20 at 12:15
  • They are all on the same windows pc both application and wsl – Theodosis Feb 25 '20 at 8:36
  • At a guess, it's possible that this issue is due to WSL. It's a little old but see this Microsoft article. It's also probably worth mentioning that curl has up-to-date native Windows builds you can test with that don't rely on WSL. Otherwise, you may want to check your server software and firewalls to ensure none of those is blocking curl's access to your server. – Anaksunaman Feb 25 '20 at 9:08
  • That said, if you still run into problems, it might be good to edit your question and detail the exact steps and software (including relevant config files) you are using, which could help resolve any non-obvious issues. – Anaksunaman Feb 25 '20 at 9:12
  • Okay, thank you I'm going to check it. Also, note that the server is a simple apache2 and right now I have disabled some parts of the firewall in order to ping my public IP and this works. (Eg curl -vvv localhost:80/hello_world.html doesn't work but curl -vvv 111.222.333.4444:80/hello_world.html works.) (the IPs are fake only for this example) – Theodosis Feb 25 '20 at 9:13
12

I suspect you're actually running WSL2, which handles networking almost entirely differently from how WSL handled it. See this issue on git for a very long breakdown of what's happening and why.

The short answer: it's not going to be easy to get localhost running the way you'd like on WSL2, and I've personally downgraded to WSL (the first version) for the moment until they finally offer a method to do that without both having to map all of your requests to an arbitrary assigned-on-startup IP and opening your Windows firewall rules up to allow public inbound connections on your service port.

If you do want to downgrade and magically have localhost interactions back, just open a Powershell console as admin and enter the following (assuming running an Ubuntu release):

wsl --set-version Ubuntu 1

You'll lose any WSL2 features but regain your sanity. Note that there may be one or two migration tasks to perform on the Ubuntu instance itself.

2
  • Thanks for the response, this is useful. However I wonder if there is a less terminal response like the one stated here – Manuel Martinez Jul 11 '20 at 5:22
  • 1
    @ManuelMartinez Yeah, that's what I was referring to with the inbound connections. If you follow that approach, you're no longer using a loopback address and now need to accept inbound connections on your service ports, which is a complete non-starter for many organizations (security and all). Also, fun fact: organization IPs are often unstable, so you'll have the pleasure of getting random networking errors and rewriting your resolv.conf whenever that happens. – bsplosion Jul 13 '20 at 13:14
1

A quick workaround is to set an alias to the cmd version of curl and use that from wsl2.

In you ~/.bashrc, set following:

alias curl="cmd /C curl"

After you restart or source .bashrc, you can use curl as usual.

enter image description here

If you need to use it in a bash script, where your aliases is not set, you can use cmd /C curl in the scripts. It is ugly but it works.

2
  • My WSL Debian distro doesn't recognize cmd. I can't find anything on the googles about cmd except for Windows CMD. Any suggestions? – gabe Jan 15 at 20:13
  • Windows shell has a different resolution of executable names than popular Linux shells. Just cmd is not going to be enough as there is no such executable actually. You can get around this with simple alias: alias cmd="cmd.exe /c" – Aleksander Stelmaczonek Jan 25 at 17:04

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.