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