Create a known IP address for WSL2 VM #7395
Replies: 7 comments 8 replies
-
Hi there! Can this be used for #7690 ? |
Beta Was this translation helpful? Give feedback.
-
@Biswa96 My (example) script reconfigures subnet that WSL2 uses using a static network range. This change doesn't have any major side-effects. Unfortunately configuring static IP the right way would require configuring static MAC for WSL2 VM and static MAC to IP rule for DHCP server. All this probably is doable, but reverse-engineering isn't worth the time. If you don't care about possible side-effects, you can set desired static IP by running a script inside the WSL2 vm. If you want to minimize risk of side-effects (IP conflict), prefer IP addresses from end of the network range |
Beta Was this translation helpful? Give feedback.
-
To add a specific IP address in WSL2 VM, follow this code.
#include <windows.h>
#include <computecore.h>
#include <computenetwork.h>
#ifdef _MSC_VER
#pragma comment(lib, "computecore")
#pragma comment(lib, "computenetwork")
#endif
static const GUID NetworkId = {
0xB95D0C5E,
0x57D4,
0x412B,
{ 0xB5, 0x71, 0x18, 0xA8, 0x1A, 0x16, 0xE0, 0x05 } };
static const GUID EndpointId = {
0x12345678,
0x1234,
0x1234,
{ 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB } };
static const wchar_t NetworkSettings[] = LR"(
{
"Name" : "WSL",
"Type" : "ICS",
"IsolateSwitch" : true,
"Flags" : 9,
"Subnets": [
{
"AddressPrefix":"172.16.0.0/24"
}
]
})";
// VirtualNetwork should be same with NetworkId
// Others are random at your choice
static const wchar_t EndpointSettings[] = LR"(
{
"ID": "12345678-1234-1234-1234-1234567890AB",
"IPAddress": "172.16.0.2",
"MacAddress": "12-12-12-12-12-12",
"VirtualNetwork" : "B95D0C5E-57D4-412B-B571-18A81A16E005",
"Namespace" : {
"ID" : "00000000-0000-0000-0000-000000000000",
"IsDefault" : false
}
})";
static const wchar_t NicAdapterRemove[] = LR"(
{
"ResourcePath" : "VirtualMachine/Devices/NetworkAdapters/Default",
"RequestType" : "Remove"
})";
// EndpointId and MacAddress should match with EndpointSettings
static const wchar_t NicAdapterAdd[] = LR"(
{
"ResourcePath" : "VirtualMachine/Devices/NetworkAdapters/Default",
"RequestType" : "Add",
"Settings" : {
"EndpointId" : "12345678-1234-1234-1234-1234567890AB",
"MacAddress" : "12-12-12-12-12-12"
}
})";
int main(int argc, char *argv[])
{
HRESULT hRes;
HCN_NETWORK HcnNet;
HCN_ENDPOINT HcnEndp;
HCS_OPERATION HcsOp;
HCS_SYSTEM HcsSys;
PWSTR Result;
// Put the VmId from `hcsdiag.exe list` command
wchar_t VmId[] = L"";
HcsOp = HcsCreateOperation(NULL, NULL);
hRes = HcsOpenComputeSystem(VmId, GENERIC_ALL, &HcsSys);
// 1. Delete existing network
hRes = HcnDeleteNetwork(NetworkId, &Result);
// 2. Create network and endpoint
hRes = HcnCreateNetwork(NetworkId, NetworkSettings, &HcnNet, &Result);
hRes = HcnCreateEndpoint(HcnNet, EndpointId, EndpointSettings, &HcnEndp, &Result);
// 3. Detach existing network
hRes = HcsModifyComputeSystem(HcsSys, HcsOp, NicAdapterRemove, NULL);
hRes = HcsWaitForOperationResult(HcsOp, INFINITE, &Result);
// 4. Add our own network
hRes = HcsModifyComputeSystem(HcsSys, HcsOp, NicAdapterAdd, NULL);
hRes = HcsWaitForOperationResult(HcsOp, INFINITE, &Result);
hRes = HcnCloseEndpoint(HcnEndp);
hRes = HcnCloseNetwork(HcnNet);
HcsCloseOperation(HcsOp);
HcsCloseComputeSystem(HcsSys);
return 0;
}
// Now run these commands in WSL2:
// sudo ip address add 172.16.0.2/24 broadcast + dev eth0
// sudo ip link set dev eth0 up
// sudo ip route add default via 172.16.0.1 dev eth0
// Feel free to modify and add this code in your project. Also ask any questions here. A proper attribution would be appreciated ❤️ |
Beta Was this translation helpful? Give feedback.
-
Hi @Biswa96 , I have tried using the code from the original post. It is not working for me.
I am using w10 enterprise, 21H2 Is there anything else that needs to be adapted to work on my computer? |
Beta Was this translation helpful? Give feedback.
-
Hi, could someone help me understand why we have to have a dynamic IP for the WSL instance? I've seen many posts talking about how to set the WSL IP to static. Why this cannot be part of the WSL official feature? |
Beta Was this translation helpful? Give feedback.
-
Here a detailed working procedure on how to use WSLAttachSwitch Script : #4799 (comment) Text : #4799 (comment) |
Beta Was this translation helpful? Give feedback.
-
See also
|
Beta Was this translation helpful? Give feedback.
-
172.16.0.0/24
(for example).There are many things can be added or modify. Please make the code free and open-source if you modify it so that everyone can learn and share this knowledge.
Beta Was this translation helpful? Give feedback.
All reactions