目录

(可能是)完美解决WSL2重启变IP问题

目录

WSL2的升级对比WSL1 IO升级是巨大的,以及完整的Linux内核, 等等都是完美的Linux发行版___Windows10

解决方法有几步一步一步解决

编辑bat脚本

此方法在 [microsoft/WSL/issues/418][1] 获得,开机启动为WSL2新增加一个IP,为本地"vEthernet (WSL)“设置IP,编辑好脚本之后,右键添加快捷方式,将快捷方式放到C:\Users\用户名\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup,此步骤是将脚本快捷方式放到开机启动目录,让脚本每次开机自启,原脚本尽量保存在不会被删除掉的位置, 我放在C:\bin

::强制获取管理员运行权限
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
wsl -d Ubuntu-16.04(注意,这里是你下载Linux子系统的名称,我这里是16.04你那边可能不是) -u root ip addr add 192.168.50.16/24 broadcast 192.168.50.255 dev eth0 label eth0:1
netsh interface ip add address "vEthernet (WSL)" 192.168.50.88 255.255.255.0

编辑WSL2内hosts

注意,WSL2默认/etc/hosts每次重启会自动改变,需要添加/etc/wsl.conf,内容如下

[network]
generateHosts = false

cat /etc/hosts, 最下面的 192.168.50.88 win 则是我们添加的内容

cexll@DESKTOP-328GKFR:/mnt/c/Windows/system32$ cat /etc/hosts
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
[network]
generateHosts = false
127.0.0.1       localhost
127.0.1.1       DESKTOP-328GKFR.localdomain     DESKTOP-328GKFR

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

192.168.50.88 win

一切设置好之后,在命令行 ping win能ping通就表示ok了 [1]: https://github.com/MicrosoftDocs/WSL/issues/418#issuecomment-648570865