skip to Main Content

So i have just recently migrated to WSL2. And all the tools I used on WSL1 are now broken.

Commands like:

$ expo start
$ npm start

are running the app on the IP Address 172.--.---.---
This breaks many of the tools I Use that still run on 192.---.--.-

For example, when I try to connect to my Expo React Native Project with Expo go. It throws me the error:-

errorImage

The above picture is not accurate to the error I am getting. But as it is shown, I cannot access Expo Projects with a Port Other than that is configured on my phone.

Is there a way to change my WSL2 subsystem’s IP to my host machine’s so that I can connect to my Expo project from my phone?

2

Answers


  1. So I have just recently migrated to WSL2, and all the tools I used on WSL1 are now broken.

    In general, networking is "easier" on WSL1, so if the tools you are using work there, it’s okay to keep using a WSL1 instance for them. You can have a separate WSL2 instance if you’d like for tools that work better there.

    WSL1 runs as a syscall translation layer in Windows, and as such it shares the same network interface as Windows.

    WSL2, on the other hand, is a virtualized environment, using a virtual NIC behind a virtual Hyper-V switch. The WSL2 network is NAT’d behind/inside the Windows network, which is why you can’t see it from other devices on the network.

    WSL2 does provide a feature known as "local forwarding" that allows the Windows host itself to access WSL2 through the localhost address. However, that doesn’t extend to other devices on the network.

    Is there a way to change my WSL2 subsystem’s IP to my host machine’s?

    Well, maybe. If you are on Windows 11 Pro, the latest Preview releases of WSL include a feature that enables bridged networking in WSL (vs. NAT). I have not tested it myself, but you can find details in this blog post.

    Other alternatives:

    • I find a good SSH configuration to be a swiss-army knife in WSL, and this is no exception. If you enable OpenSSH server in Windows, you can easily create a reverse tunnel that will forward traffic from Windows back to the WSL2 instance. See my "Option 2" in this Ask Ubuntu answer for instructions.

      This is actually my preferred approach.

    • It’s possible to use Windows port forwarding through the netsh interface portproxy command to route the traffic to WSL2. The problem is that the WSL2 address is dynamically assigned each time it restarts), so you have to erase old port forwards and recreate them after each reboot. Here are partial instructions, at least, from the Microsoft WSL doc. I say "partial" because it currently covers only the creation, but not deletion of old forwards.

    Login or Signup to reply.
  2. You can use a tunnel with Expo (docs are here).

    npm start -- --tunnel
    

    This will ask to install the @expo/ngrok package. Once that is done, you’ll see the Metro is waiting message has a full URL instead of just an IP address.

    Scan the QR code and Expo Go will connect through the tunnel.

    This works because the tunnel is exposed to the internet, instead of just your local network.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search