设置wsl2固定ip
修改子系统默认的dns
创建/etc/wsl.conf,写入以下内容,默认为true,true 将 WSL 设置为生成 /etc/resolv.conf
[network]
generateResolvConf=false
resolv.conf是/run/resolvconf/resolv.conf(generateResolvConf=true的时候自动生成)符号链接文件,需要先删除;DNS列表自行设置
rm /etc/resolv.conf
echo 'nameserver 114.114.114.114' > /etc/resolv.conf
ip配置脚本
这个脚本每次ubuntu 重新启动都需要重新运行一遍
@echo off
rem 设置网段
set localnet=192.168.174
rem 设置子系统的ip
set ip=%localnet%.2
rem 需要设置的子系统名称
set linux=Ubuntu-18.04
wsl -d %linux% -u root ip addr del $(ip addr show eth0 ^| grep 'inet\b' ^| awk '{print $2}' ^| head -n 1) dev eth0
wsl -d %linux% -u root ip addr add %ip%/24 broadcast %localnet%.255 dev eth0
wsl -d %linux% -u root ip route add 0.0.0.0/0 via %localnet%.1 dev eth0
powershell -c 'Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress %localnet%.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix %localnet%.0/24;'
赞 (0)