This is a small Go utility to set the WSL2 host and subnet. It achieves this by:
192.168.100.0/24
)Windows automatically creates the WSL
network when a WSL2 environment is started, so we need to pre-empt this by creating our own network with our settings before that happens.
wsl --shutdown
; they should work correctly after they're re-launched.Access denied
error if you are not elevatedBasic command line help can be obtained with wsl-subnet --help
Installing this as a task is possible via the Task Scheduler:
At system startup
as the triggerSYSTEM
accountThis can done in an elevated console. Adjust the following example to point to your file location, of course.
schtasks /create /tn "WSL Subnet Configure" /tr c:\tools\wsl-subnet.exe /sc onstart /ru System
cd src
go build .
This will yield a single, static executable: wsl-subnet.exe
We're making use the Windows Host Compute Network interfaces to do this:
Fortunately, Microsoft has supplied the hcsshim
Go library to interact with these interfaces at a higher level. This library is used by various projects, including (at some stage), Docker itself.
This is a simple utility inspired partly by the various Powershell scripts out there to control WSL booting and subnet assignment, including:
Check those out if you need something more complex.