WSL subnet utility
This is a small Go utility to set the WSL2 host and subnet. It achieves this by:
- deleting the existing WSL network
- creating a new one with the specified subnet (defaulting to
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.
- this needs to be done before starting any WSL2 (or Docker on WSL2) environments
- if you need to do this afterwards, restart WSL2 environments by shutting them down with
wsl --shutdown; they should work correctly after they're re-launched.
- you may need to disable Docker auto-start: this needs to run first
- the utility needs to be run in an elevated mode (administrator) in order to alter the network configuration
- you'll get an
Access deniederror if you are not elevated
- run in an elevated console
- or schedule as a task (noted below)
- you'll get an
Basic command line help can be obtained with
Installing this as a task is possible via the Task Scheduler:
At system startupas the trigger
- Run under the
cd src go build .
This will yield a single, static executable:
References and acknowledgements
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.