Setting Up Port Forwarding#

If the client is on a different machine than the WSL2 host, you need to set up port forwarding, as WSL2 uses NAT.

The process involves forwarding traffic from your Windows host’s external IP and port (8001) to the WSL2 instance’s internal IP and port (8001). All these steps require a Windows terminal with admin privilege.

  1. Find the WSL2 internal IP address.

    In the WSL2 terminal, run the following command:

    hostname -I
    
  2. Forward traffic from Windows host to WSL2.

    Open a Windows terminal with admin privilege and run the following command:

    netsh interface portproxy add v4tov4 listenport=8001 listenaddress=0.0.0.0 connectport=8001 connectaddress=<wsl2-ip>
    

    Tip

    WSL2 IP addresses change after every restart, so you need to update the forwarding rule with the correct IP every restart.

  3. Verify port forwarding.

    netsh interface portproxy show v4tov4
    

    You should see output similar to the following:

    Listen on IPv4:    Connect to IPv4:
    Address    Port    Address    Port
    --------------- ---------- --------------- ----------
    0.0.0.0    8001    <wsl2-ip>   8001
    
  4. Allow traffic through Windows Firewall.

    To allow the client from the other machine proper access, add a firewall rule for Port 8001 in Windows PowerShell with admin privilege:

    New-NetFirewallRule -DisplayName "Allow gRPC on Port 8001" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 8001