[WSL] Could not access the service hosted in WSL from host by IP

If you just started to use WSL, you may encounter the issue that You only can access your service hosted in the WSL from the host by localhost, but not the IP address. For example, you have a web service that listens to port 8080. In your web browser, you can browse the site with http://localhost:8080, but http://127.0.0.1:8080.

The reason is that you need to use the WSL IP address. An easy way to find the address is to execute the below command in WSL terminal.

ip addr | grep eth0 | cut -d ' ' -f 6 | tail -1 | cut -d '/' -f 1

The output is the WSL IP address. For example, 172.22.110.133. So you can browse the site with http://172.22.110.133:8080


If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!
Buy Me A Coffee
DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !