Original link: https://editor.leonh.space/2023/wsl/
The story starts from “Microsoft Speaking of Linux”, since Satya Nadella made a high-profile love for Linux in 2019, there has been WSL. Whether this love across business models can go on smoothly, let us see.
WSL allows us to run a nearly complete set of Linux in Windows without installing dual boots. The reason behind this is the Hyper-V virtualization technology. In essence, WSL is a special VM. Therefore, the first question we will encounter is “How to interconnect WSL and Windows through IP?”
How WSL and Windows interconnect via IP are actually two issues, see below.
The service is opened on WSL and connected to Windows
The first question, the service is opened on WSL, how to connect to Windows?
Suppose a service is running on port 8086 of WSL, and try to connect to Windows, the easiest way to test is to open the browser to see if it works, and test the following two URLs:
- http://localhost:8086/
- http://127.0.0.1:8086/
Testing with http://127.0.0.1:8086/ on Windows may result in connection rejection, because as mentioned earlier, WSL is essentially a specially-made VM with its own IP, and 127.0.0.1 points to Windows itself. If Windows does not listen to the service on port 8086, the connection will be rejected.
On the other hand, if you use http://localhost:8086/ to test instead, it should be successful, because Windows has intimately opened the WSL forwarding mechanism, and all requests to localhost will be forwarded to WSL.
What is a bit counterintuitive here is that localhost is not pointing to the common sense 127.0.0.1, but WSL. Once this point is neglected, it is possible to waste an afternoon wondering whether it does not work or not.
As for the specific IP of WSL, you can query it in the shell in WSL:
$ ip addr
Then the second question, the service is opened on Windows, how to connect to WSL?
Open the service on Windows, connect to WSL
Based on the understanding that “WSL is essentially a VM”, the two must have their own IPs. There is a special method to find the IP of Windows in WSL:
$ cat /etc/resolv.conf
The content is as follows:
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf: # [network] # generateResolvConf = false nameserver 172.22.128.1
172.22.128.1 is the IP of Windows on the WSL network. Therefore, as long as you open http://172.22.128.1/, you should be able to open the Windows web service.
References
< Using WSL to access web applications >
This article is transferred from: https://editor.leonh.space/2023/wsl/
This site is only for collection, and the copyright belongs to the original author.