Expose WSL2 To LAN

I need to connect to my WSL2 container from other machines on the network over SSH: My container is assigned 172.24.208.2 and I will map 5022 to 22 on the host.

While you can assign a static IP to the container, occasionally windows will change the subnet on the vSwitch, breaking everything. The commands here need to be updated with the new range and run again.

# SSH to WSL2
netsh interface portproxy add v4tov4 listenport=50822 listenaddress=0.0.0.0 connectport=22 connectaddress=172.24.208.2
# Wall frame static content hosting
netsh interface portproxy add v4tov4 listenport=5022 listenaddress=0.0.0.0 connectport=80 connectaddress=172.24.208.2
# ???
netsh interface portproxy add v4tov4 listenport=8089 listenaddress=0.0.0.0 connectport=8089 connectaddress=172.24.208.2
# Stringer RSS
netsh interface portproxy add v4tov4 listenport=443 listenaddress=0.0.0.0 connectport=58443 connectaddress=172.24.208.2
netsh interface portproxy add v4tov4 listenport=80 listenaddress=0.0.0.0 connectport=5088 connectaddress=172.24.208.2

 # to view rules:
 # netsh interface portproxy show v4tov4
 # to delete the rules:
 # netsh int portproxy reset all

This is assuming SSH server is installed and running which is not the WSL2 default.

Other WSL2 Tips

All my mappings in /mnt to the host disks are empty:

# Just remount it, you will need to cd out of the mount point first.
mount -t drvfs C: /mnt/c
sudo apt install openssh-server
sudo /etc/init.d/ssh start

Cron jobs not running on Ubuntu WSL2

To keep the shell snappy WSL does not initialize all the Linux daemons such as cron so if you want to use cron to schedule jobs you must get windows to launch it for you at startup.

First allow the cron daemon to be started in the sudoers file:

sudo visudo

Add this line:

brent ALL=(ALL) NOPASSWD:/etc/init.d/cron start
brent ALL=(ALL) NOPASSWD:/etc/init.d/apache2 start

See the startup programs on windows with this explorer.exe shell:startup

In the folder that opens create a windows shortcut as below to start whatever service is required:

C:\Windows\System32\wsl.exe sudo /etc/init.d/cron start

******* Static IP on WLS2*******

WSL-IpHandler github repo will help with assigning a deterministic IP address to the WSL2 machines. It works for me although I got this error shown which I can’t be bothered looking into since it still works:

Sometimes after a reboot or windows update it is required to run this again. - It needs to be run from a PowerShell 7 terminal. - After running you will be able to ping the ubuntu IP from Windows.

This error can be fixed with: Import-Module .\WSL-IpHandler\SubModules\IPNetwork.psm1

PowerShell installing Wsl-IpHandler to Ubuntu-20.04...
Test-IsValidStaticIpAddress: The term 'Get-IpNet' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
PS C:\Users\brent>Import-Module .\WSL-IpHandler\SubModules\IPNetwork.psm1
PS C:\Users\brent>Install-WslIpHandler -WslInstanceName  Ubuntu-20.04 -GatewayIpAddress 172.16.0.1 -WslInstanceIpAddress 172.24.208.2
PowerShell installing Wsl-IpHandler to Ubuntu-20.04...
[sudo] password for brent:
Remove-HostFromRecords: C:\Users\brent\Documents\PowerShell\Modules\Wsl-IpHandler\Scripts\Powershell\FunctionsHostsFile.ps1:326
Line |
326 |              Remove-HostFromRecords -Record $_ -HostName $HostName -Mo …
   |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   | The property 'Count' cannot be found on this object. Verify that the property exists.

PowerShell finished installation of Wsl-IpHandler to Ubuntu-20.04 with Errors:

Pinging Ubuntu-20.04 from Windows failed:

Pinging Ubuntu-20.04 [172.27.16.2] with 32 bytes of data:
Request timed out.

Ping statistics for 172.27.16.2:
   Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),

No internet on WSL2

For some reason something broke on the WSL2 vSwitch, so this command reset it.

if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
$CmdLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CmdLine
Exit
}
# Restart the Host Network Service 
Restart-Service -Force -Name hns
# Restart the Windows Subsystem for Linux Manager
Restart-Service LxssManager
# Restart the WSL Network adapter
Restart-NetAdapter -Name "vEthernet (WSL)"
wsl --shutdown
wsl ping google.com -c 1

Comments

comments powered by Disqus