Skip to content

Instantly share code, notes, and snippets.

@micheldiemer
Created April 18, 2024 23:48
Show Gist options
  • Save micheldiemer/e32a294cd484eff7bcd362bf8f3330b3 to your computer and use it in GitHub Desktop.
Save micheldiemer/e32a294cd484eff7bcd362bf8f3330b3 to your computer and use it in GitHub Desktop.
WSL : adresse IP statique et switch Hyper-V avec WSLAttachSwitch

WSL : adresse IP statique et switch Hyper-V avec WSLAttachSwitch

Description textuelle

  1. Créer un switch dans Hyper-V, par exempke "VirtualSwitch"
  2. 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}'
  1. Télécharger WSLAttachSwitch.exe sur le disque Windows, par exemple /mnt/c/Windows/System32/WSLAttachSwitch.exe

  2. Connecter WSL au switch via le fichier WSL /etc/rc.local (sudo chmod u+rx /etc/rc.local)

#!/bin/sh -e
/mnt/c/Windows/System32/WSLAttachSwitch.exe VirtualSwitch
# bonus : décommenter pour une adresse ip fixe
# ip addr add 192.168.0.2 dev eth1
  1. Charger /etc/rc.local à chaque démarrage

Fichier /etc/wsl.conf

[boot]
command=/etc/rc.local

Script

# 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
$exeFile="/mnt/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 $exeFile 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 $exeFile $switchName | sudo tee -a /etc/rc.local"
wsl 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 !
@micheldiemer
Copy link
Author

micheldiemer commented Apr 18, 2024

Voir aussi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment