友情提示:未必有用。有时候跟网卡配置有关
rm /etc/resolv.conf
vi /etc/resolv.conf
写入下面的内容
nameserver 192.168.2.1
nameserver 114.114.114.114
domain localnet.local
一开始是查Temporary failure in name resolution 的相关问题 ,结果一点用没有,浪费时间
结论:关于WSL2 一定要带上 "WSL2"
转载自
WSL2 Ubuntu: ping: hostname: Temporary failure in name resolution – CT Studio
Now it does what I want it to do. Maybe not what the WSL guys intended, but there you go!
改用外部网络也能解决(缺点,每次开机都要设置)
github 大佬编写的bat
https://github.com/microsoft/WSL/issues/4210
编写一个bat,然后超级管理员运行就行了
wsl2.static.ip.bat 文件内容如下,wsl -d xxx(子系统名称)
wsl -d Ubuntu -u root ip addr del $(ip addr show eth0 ^| grep 'inet\b' ^| awk '{print $2}' ^| head -n 1) dev eth0
wsl -d Ubuntu -u root ip addr add 192.168.50.2/24 broadcast 192.168.50.255 dev eth0
wsl -d Ubuntu -u root ip route add 0.0.0.0/0 via 192.168.50.1 dev eth0
wsl -d Ubuntu -u root echo nameserver 192.168.50.1 ^> /etc/resolv.conf
powershell -c "Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress 192.168.50.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 192.168.50.0/24;"