Port forwarding from WLS2

·

2 min read

Port forwarding is a technique that allow us to access services running on different machine. If we have a service running on WLS2, and we want to access it from self Windows host machine or another PC in LAN, follow these following steps:

  1. Specify eth0 inet address in WSL2 terminal

    (Ex: 172.20.197.6)

     ifconfig
     --------------------------------------------------------------------
     eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
             inet 172.20.197.6  netmask 255.255.240.0  broadcast 172.20.207.255
             inet6 fe80::215:5dff:fe17:a1ed  prefixlen 64  scopeid 0x20<link>
             ether 00:15:5d:17:a1:ed  txqueuelen 1000  (Ethernet)
             RX packets 6094  bytes 586476 (586.4 KB)
             RX errors 0  dropped 0  overruns 0  frame 0
             TX packets 72  bytes 5136 (5.1 KB)
             TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
     lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
             inet 127.0.0.1  netmask 255.0.0.0
             inet6 ::1  prefixlen 128  scopeid 0x10<host>
             loop  txqueuelen 1000  (Local Loopback)
             RX packets 0  bytes 0 (0.0 B)
             RX errors 0  dropped 0  overruns 0  frame 0
             TX packets 0  bytes 0 (0.0 B)
             TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
  2. Open Command Line in Windows as Administrator & run these command for each ports
    netsh interface portproxy add v4tov4 listenport=<LISTEN_PORT> listenaddress=<LISTEN_IP_ADDRESS> connectport=<CONNECT_PORT> connectaddress=<CONNECT_IP_ADDRESS>

    • LISTEN_PORT: port number that forwarded

    • LISTEN_IP_ADDRESS:

      • 127.0.0.1 if we want only self Windows host machine access

      • 0.0.0.0 if we want all other machines can access

    • CONNECT_PORT: port number of service in WSL2

    • CONNECT_IP_ADDRESS: IP of WLS2 from eth0.inet above

    C:\Windows\System32>netsh interface portproxy add v4tov4 listenport=5432 listenaddress=0.0.0.0 connectport=5432 connectaddress=172.20.197.6
    C:\Windows\System32>netsh interface portproxy add v4tov4 listenport=9200 listenaddress=0.0.0.0 connectport=9200 connectaddress=172.20.197.6
    C:\Windows\System32>netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=8080 connectaddress=172.20.197.6
    C:\Windows\System32>netsh interface portproxy add v4tov4 listenport=6379 listenaddress=0.0.0.0 connectport=6379 connectaddress=172.20.197.6
    C:\Windows\System32>netsh interface portproxy add v4tov4 listenport=8181 listenaddress=0.0.0.0 connectport=8181 connectaddress=172.20.197.6
  1. Verify ports

     C:\Windows\System32>netsh interface portproxy show all
    
     Listen on ipv4:             Connect to ipv4:
     Address         Port        Address         Port
     --------------- ----------  --------------- ----------
     0.0.0.0         8080        172.20.197.6   8080
     0.0.0.0         5432        172.20.197.6   5432
     0.0.0.0         9200        172.20.197.6   9200
     0.0.0.0         6379        172.20.197.6   6379
     0.0.0.0         8181        172.20.197.6   8181
    
  2. If we want to reset these ports

     netsh interface portproxy reset