Access WSL localhost from LAN — For mobile testing

Web development Jul 6, 2023

There is a scenario where you need to test on iPhone and debug on local.

Oops … we all hate the safari bug !

First is firewall

Click left panel ‘Advanced Setting’

It will open fire wall screen

Right click new rule on inbound rule specific port that localhost run. Mine is TCP 3000.

If you don’t know if it is TCP or not it is TCP. Next until end.

Second is forward to WSL

This step depends on machine. Mine have problem.

Open cmd in admin mode

$ wsl hostname -I
// 172. ... remember this$ netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=<172 ... ip from above>

This will bridge your Windows machine IP over LAN (192…) to WSL IP (172…) so that iPhone can access WSL localhost.

Thanks to this Stack Overflow Answer.

Finally see the IP

$ ipconfig

See you interface
If you don't know it's likely to be the longest one
...
IPv4 Address. . . . . . . . . . . : 192.168.1.22

Type 192.168.1.22:3000 on your iPhone.

Here we go now we can access it over LAN.

Hope this helps !

Tags

TeamCMD

We are CODEMONDAY team and provide a variety of content about Business , technology, and Programming. Let's enjoy it with us.