- Créer un switch dans Hyper-V, par exempke "VirtualSwitch"
- Ajuster les règles du pare-feu de WSL
Set-NetFirewallHyperVVMSetting -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -DefaultInboundAction Allow
Get-NetFirewallHyperVVMSetting -PolicyStore ActiveStore -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}'
-
Télécharger WSLAttachSwitch.exe sur le disque Windows, par exemple
/mnt/c/Windows/System32/WSLAttachSwitch.exe
-
Connecter WSL au switch via le fichier WSL
/etc/rc.local
(sudo chmod u+rx /etc/rc.local
)
#!/bin/sh -e
cmd.exe /c "c:\Windows\System32\WSLAttachSwitch.exe VirtualSwitch"
# Adresse IP statique
ip addr add 192.168.0.2 dev eth1
- Charger
/etc/rc.local
à chaque démarrage
Fichier /etc/wsl.conf
[boot]
command=/etc/rc.local
# RunAs Administrator
# paramètres
$switchName="StaticIPs"
$submaskCidrBits=24
$winIp="192.168.10.1"
$wslIp="192.168.10.2"
# WSL path for WSLAttachSwitch.exe, will be downloaded via curl
$exeFileWsl="/mnt/c/Windows/System32/WSLAttachSwitch.exe"
$exeFile="C:/Windows/System32/WSLAttachSwitch.exe"
# vérifier les règles de pare-feu WSL
Set-NetFirewallHyperVVMSetting -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -DefaultInboundAction Allow
Get-NetFirewallHyperVVMSetting -PolicyStore ActiveStore -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}'
# Création d’un switcch virtuel et de l'adresse IP de l’hôte Windows
New-VMSwitch -SwitchName $switchName -SwitchType Internal
$netadapter=Get-Netadapter | where-Object Name -Like "*$switchName*"
New-NetIPAddress -PrefixLength $submaskCidrBits -InterfaceIndex $netadapter.ifIndex -IPAddress $winIp
# hors sujet : connecter d’autres machines virtuelles au switch
# télécharger WSLAttachSwitch
$cmd = "curl -L -o $exeFileWsl https://github.com/dantmnf/WSLAttachSwitch/releases/download/latest/WSLAttachSwitch.exe"
wsl -u root bash -c $cmd
## Modification permanentees via /etc/rc.local and /etc/wsl.conf
# si nécessaire, crééer /etc/rc.local
$cmd = "[ ! -f /etc/rc.local ] && echo '#!/bin/sh -e' > /etc/rc.local"
wsl -u root bash -c $cmd
# connecter la machine WSL au switch
$cmd = "echo `$(which cmd.exe) /c $exeFile \`"$switchName\`" | sudo tee -a /etc/rc.local"
wsl -u root bash -c $cmd
# affecter une adresse IP à la vm WSL
$cmd = "echo 'ip addr add $wslIp dev eth1' >> /etc/rc.local"
wsl -u root bash -c $cmd
# configuration de /etc/wsl.conf pour charger /etc/rc.local
wsl -u root bash -c "sudo chmod u+x /etc/rc.local"
wsl -u root bash -c "echo '[boot]' >> /etc/wsl.conf"
wsl -u root bash -c "echo 'command=/etc/rc.local' >> /etc/wsl.conf"
# Les machines connectées au switch peuvent communiquer entre elles !
Voir aussi
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\NatGatewayIpAddress Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\NatNetwork Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss