- No network connection in any distribution under WSL 2
- No internet connection on WSL Ubuntu (Windows Subsystem for Linux)
核心原因是PC端为WSL服务的虚拟网卡异常,导致wsl的网络无法访问网络。重置PC端的网络——使用管理员权限打开CMD命令行,重置如下的所有网络,然后重启PC
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns
wsl端的linux上的/etc/resolv.conf
文件中可以看到自动生成的nameserve地址为PC端的虚拟网卡地址——
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.19.176.1
可以定制wsl端的linux的启动选项,增加network端的配置(不自动生成resolv.conf
文件)。实测还是自动生成文件比较靠谱,只有确保PC端的网络正常,wsl端的网络就可以恢复。(遇到无法编辑这个文件的情况,使用sudo vim /etc/resolv.conf操作时,提示"resolv.conf" E166: Can't open linked file for writing
)
可以通过stat /etc/resolv.conf
看到这个文件的状态,类似——
File: /etc/resolv.conf -> ../run/resolvconf/resolv.conf
Size: 29 Blocks: 0 IO Block: 4096 symbolic link
Device: 810h/2064d Inode: 44358 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-07-02 08:38:47.580000000 +0800
Modify: 2021-05-31 11:27:04.000000000 +0800
Change: 2021-05-31 17:01:15.490000000 +0800
Birth: -