How to Access HTTP Server on Host from WSL2
If you are using Windows Subsystem for Linux 2 (WSL2) and want to access an HTTP server running on your host machine from within WSL2, this guide will walk you through the steps. This can be useful when you want to test your web applications or access local development servers from your WSL2 environment.
Step 1: Find Your Host Machine's IP Address
To access the HTTP server on your host machine, you need to know its IP address. Open a Command Prompt or PowerShell window on your host machine and run the following command:
ipconfig
Look for the "IPv4 Address" under the network adapter you are using (e.g., Ethernet or Wi-Fi). Note down the IP address for later use.
Step 2: Configure Firewall on Host Machine
By default, Windows Firewall may block incoming connections to your HTTP server. To allow access from WSL2, you need to create a new inbound rule to open the port used by your HTTP server. Follow these steps:
- Open the Windows Defender Firewall by searching for it in the Start menu.
- Click on "Advanced settings" in the left-hand sidebar.
- In the "Inbound Rules" section, click on "New Rule..." to create a new rule.
- Choose "Port" as the rule type and click "Next".
- Select "TCP" and enter the port number used by your HTTP server (e.g., 80 for HTTP, 443 for HTTPS) and click "Next".
- Select "Allow the connection" and click "Next".
- Ensure that all three options (Domain, Private, and Public) are checked and click "Next".
- Provide a name for the rule (e.g., "Allow HTTP Server") and click "Finish".
Step 3: Access HTTP Server from WSL2
Now that you have the IP address of your host machine and the firewall is configured, you can access the HTTP server from your WSL2 environment. Follow these steps:
- Open your WSL2 distribution (e.g., Ubuntu) and launch a terminal.
- Enter the following command, replacing
HOST_IPwith the IP address of your host machine:
curl HOST_IP - If the HTTP server is running and reachable, you will see the output of the request in the terminal.
Conclusion
By following these steps, you can easily access an HTTP server running on your host machine from within WSL2. This allows you to test web applications or access local development servers seamlessly. Remember to configure the firewall on your host machine to allow incoming connections to the appropriate port used by your HTTP server.
References
| Source | Link |
|---|---|
| Microsoft Docs | https://docs.microsoft.com/en-us/windows/wsl/ |