mike-barber / wsl-subnet-utility

Simple utility to set the WSL2 subnet to a specific range
Apache License 2.0
14 stars 1 forks source link

WSL subnet utility

This is a small Go utility to set the WSL2 host and subnet. It achieves this by:

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.

Important notes

Basic command line help can be obtained with wsl-subnet --help

Installing this as a task is possible via the Task Scheduler:

This 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

Build

cd src
go build .

This will yield a single, static executable: wsl-subnet.exe

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.