0

I’m trying to set up a test platform for some server software. I’m using Windows 10 IoT and Ubuntu 24 on WSL2. Due to the limitations of WSL on Windows 10, I must use port forwarding to allow the server software to receive outside connections. I’m using a batch script on startup to set up the port forwarding and to start the server software. This works perfectly most of the time. However, about 15% of the time, the port forwarding does not work after boot.

When the port forwarding does not work, the only solution I have found is to reboot the PC until it does work. I have tried removing and resetting the port forwarding rules. I have tried shutting down and restarting WSL. I double-checked that my script shows the IP address that Ubuntu currently has in WSL. I have tried restarting the iphlpsvc service. I have tried restarting the Hyper-V networking service and resetting the WSL networking. Nothing works aside from rebooting the PC. Even rebooting the PC may take two or three reboots to resolve the issue.

I know the best solution would be to upgrade to Windows 11 and use WSL2 with mirrored networking. I have already done this on a different PC. However, due to some library issues, I cannot use this PC at this time.

My first question is, how can I set up port forwarding to work 100% of the time?

My second question is, if I cannot get port forwarding to work 100% of the time, then how can I fix it without having to reboot the PC?

NOTE: This is on a closed network, so the firewall is turned off.

NOTE: I didn't create a wslconfig file, so WSL is using its default configuration.

Here is my startup script for reference:

@echo off
setlocal enabledelayedexpansion

:: Check for administrative privileges
fsutil dirty query %systemdrive% >nul 2>&1
if %errorlevel% neq 0 (
    echo Cannot set up networking and start SERV without admin permissions.
    cmd /k
)

setlocal disabledelayedexpansion

:: List of ports to forward (space-separated)
set PORTS=61002 61003 61006 61008 61030
set PROTOCOL=tcp

:: Get WSL2 IP address
for /f %%a in ('wsl hostname -I') do set WSL_IP=%%a

:: Check if IP was retrieved
if "%WSL_IP%"=="" (
    echo Failed to retrieve WSL2 IP address.
    cmd /k
)

echo WSL2 IP Address: %WSL_IP%
echo.

:: Loop through each port and set up forwarding
for %%P in (%PORTS%) do (
    echo Setting up port forwarding for port %%P...
    netsh interface portproxy add v4tov4 listenport=%%P listenaddress=0.0.0.0 connectport=%%P connectaddress=%WSL_IP% protocol=%PROTOCOL%
    
    :: Firewall is off so comment out (rem) this line
    rem netsh advfirewall firewall add rule name="WSL2 Port Forward %%P" dir=in action=allow protocol=%PROTOCOL% localport=%%P
)

echo.
echo Port forwarding complete.
echo.

:: Start the SERV software
echo Starting SERV...
echo -------------------------------------------------------

wsl cd /directory/location/of; ./SERV


:: Remove the port forwarding
echo.
echo Cleaning up networking...

for %%P in (%PORTS%) do (
    echo Removing port forwarding for port %%P...
    netsh interface portproxy delete v4tov4 listenport=%%P listenaddress=0.0.0.0
)

echo Done

endlocal
4
  • @Ramhound I didn't create a wslconfig file so WSL is using default configuration. Commented Nov 24 at 16:49
  • @Ramhound WSL doesn't allow me to use mirrored networking mode on Windows 10 like it does for Windows 11. I wish I could use mirrored mode on this PC because then I wouldn't have to use port forwarding at all. Microsoft says that in order to have WSL receive outside connections on Windows 10 you have to forward the ports. They give instructions on how to do it with netsh. It does work most of the time. The firewall is turned off. I'm also on a closed network without any routers or switches. Just a PC with a few embedded devices all with static IPs. Commented Nov 24 at 17:11
  • I commented out the netsh firewall rules but the "netsh interface portproxy" commands are setting up port forwarding. I can verify that the port forwarding rules are set using "netsh interface portproxy show all". The learn.microsoft.com site shows how to set this up but it doesn't tell how to get diagnostic info when things don't work. Commented Nov 24 at 20:18
  • No proxy server. Just a PC with multiple NICs connected to some embedded controllers. Commented Nov 25 at 19:09

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.