May 8, 2024
解决wsl2重启 内网ip发生变化
#
@echo off
setlocal enabledelayedexpansion
wsl --shutdown
::重新拉起来,并且用root的身份,启动ssh服务和docker服务
wsl -u root service docker start | findstr "Starting Docker" > nul
if !errorlevel! equ 0 (
echo docker start success
:: 看看我要的IP在不在
wsl -u root ip addr | findstr "172.21.0.2" > nul
if !errorlevel! equ 0 (
echo wsl ip has set
) else (
wsl -u root ip addr add 172.21.0.2/24 broadcast 172.21.0.0 dev eth0 label eth0:1
echo set wsl ip success: 172.21.0.2
)
::windows作为wsl的宿主,在wsl的固定IP的同一网段也给安排另外一个IP
ipconfig | findstr "172.21.0.1" > nul
if !errorlevel! equ 0 (
echo windows ip has set
) else (
netsh interface ip add address "vEthernet (WSL)" 172.21.0.1 255.255.255.0
echo set windows ip success: 172.21.0.1
)
)
pause