Windows10 WSL2 Ubuntu / Debian # 无网络

Jun*_*ndl 45 ubuntu debian apt windows-subsystem-for-linux wsl-2

从 WSL 升级到 WSL2 后

sudo apt-get update
Run Code Online (Sandbox Code Playgroud)

不再起作用。后:

wsl --set-version Ubuntu-18.04 2

输出是:

> sudo apt-get update
Err:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
  Temporary failure resolving 'security.ubuntu.com'
Err:2 http://archive.ubuntu.com/ubuntu bionic InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Reading package lists... Done
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease  Temporary failure resolving 'security.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.
Run Code Online (Sandbox Code Playgroud)

回到 WSL1 后,问题再次消失。在 Debian 上和在 CentOS 上类似......所以 WSL2 一定有一个错误。

Windows10 构建版本是 19041,今天安装。

WSL2 的任何解决方法?问候

Jun*_*ndl 99

生成的文件 /etc/resolv.conf 是:

名称服务器 172.24.0.1

..不得不把它改成

名称服务器 8.8.8.8

这解决了问题

  • 如果您想永久使用外部 DNS 服务器,请将 `[network]generateResolvConf = false` 添加到 /etc/wsl.conf :) (6认同)
  • 这应该是公认的答案。就像魅力一样,谢谢! (3认同)
  • 谢谢。很简单,但它很有帮助,就像一个魅力) (2认同)
  • 如果您使用 VPN,您可能希望避免使用答案中提到的 Google DNS 服务器。例如,Mullvad VPN [建议](https://mulvad.net/en/help/dns-leaks/) 使用 10.8.0.1。 (2认同)
  • 请注意,如果您使用 VPN,则 8.8.8.8 可能会被阻止。在这种情况下,您需要发现网络的 DNS 服务器并将它们作为“nameserver”条目放入 /etc/resolve.conf 文件中。这些可以在 Windows 中使用“netsh interface ip show dnsservers”查看 (2认同)

Jes*_*sta 22

原答案:

就我而言,我在 Windows 中使用了 VPN,当与 VPN 断开连接时,问题解决了。

编辑:

然而,这正在成为一个更广泛的问题,它再次发生在我身上,我通过删除 Hyper-V 虚拟交换机扩展适配器解决了它。

目前针对此问题最广泛使用的两种解决方案是:

1防止 /etc/resolvv.conf 变得“损坏”

  1. 创建文件:/etc/wsl.conf.
  2. 将以下几行放入文件中
[network]
generateResolvConf = false
Run Code Online (Sandbox Code Playgroud)
  1. 在一个cmd窗口中,运行wsl --shutdown
  2. 重启 WSL2
  3. 创建文件:/etc/resolv.conf. 如果存在,用这个新文件替换现有文件。
  4. 将以下几行放入文件中
nameserver 8.8.8.8
Run Code Online (Sandbox Code Playgroud)
  1. 重复第 3 步和第 4 步。您将看到git现在工作正常。

感谢在github上共享它的NonStatic

2删除损坏的网络接口驱动程序(可能是永久性的)

  1. 首先进入设备管理器 在此处输入图片说明

  2. 显示隐藏的设备 在此处输入图片说明

  3. 删除所有 Hyper-V 虚拟交换机扩展适配器 在此处输入图片说明

感谢在GitHub 上分享的Jaysonsantos

  • 请注意,如果您使用 VPN,则 8.8.8.8 可能会被阻止。在这种情况下,您需要发现网络的 DNS 服务器并将它们作为“nameserver”条目放入 /etc/resolve.conf 文件中。这些可以在 Windows 中使用“netsh interface ip show dnsservers”查看 (10认同)

kel*_*ove 8

我遇到了同样的问题。

cat /etc/resolv.conf,看看输出是否有这样的东西:

# 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.X.X.X
Run Code Online (Sandbox Code Playgroud)

您需要将名称服务器更改为8.8.8.8,因此运行sudo nano /etc/resolv.conf、编辑并保存文件。

但是,这或多或少是一个临时解决方案,因为每次 WSL 启动时您都需要这样做。您可能想要运行一个脚本来检查名称服务器并在每次重新启动 wsl 时将其更新为 8.8.8.8。此更改确实适用于我的 WSL2-Debian。但我不需要重新启动 WSL。


Edu*_*scu 7

最有可能的是,发行版有自己的虚拟适配器,首先你可以尝试一些步骤:

  1. 需要检查数据包是否真的通过了 Windows 防火墙 在此处输入图片说明 在此处输入图片说明 然后检查 %systemroot%\system32\LogFiles\Firewall\pfirewall.log

  2. 如果数据包没有通过防火墙,很可能分发获得了它自己的虚拟适配器,请检查从 Debian 内部获得的 IP 分发:

    ifconfig

或者如果你没有ifconfig

perl -MSocket -le 'socket(S, PF_INET, SOCK_DGRAM, getprotobyname("udp"));
connect(S, sockaddr_in(1, inet_aton("8.8.8.8")));
print inet_ntoa((sockaddr_in(getsockname(S)))[1]);'
Run Code Online (Sandbox Code Playgroud)

ipconfig在 Windows WSL2 主机上,查看哪些 IP 占用了 WSL 适配器下的机器

  1. 如果您需要通过 Windows IP 访问互联网,请检查此问题:https : //github.com/microsoft/WSL/issues/4150

解决方法是使用执行以下操作的脚本:

一种。获取 WSL 2 机器的 IP 地址

湾 删除之前的端口转发规则

C。添加端口转发规则

d. 删除之前添加的防火墙规则

e. 添加新的防火墙规则

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if( $found ){
  $remoteport = $matches[0];
} else{
  echo "The Script Exited, the ip address of WSL 2 cannot be found";
  exit;
}

#[Ports]

#All the ports you want to forward separated by coma
$ports=@(80,443,10000,3000,5000);


#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";


#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";

#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";

for( $i = 0; $i -lt $ports.length; $i++ ){
  $port = $ports[$i];
  iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
  iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectpor t=$port connectaddress=$remoteport";
  }
Run Code Online (Sandbox Code Playgroud)

另一种解决方案是转到 Hyper-V 管理器并更改绑定到物理 NIC 的虚拟交换机 在此处输入图片说明


Ser*_*gio 7

我解决了问题改为/etc/resolv.conf

nameserver 8.8.8.8
Run Code Online (Sandbox Code Playgroud)

进而

$ sudo chattr -f +i /etc/resolv.conf
Run Code Online (Sandbox Code Playgroud)

实际上用该属性“锁定”文件+i==immutable,因此操作系统无法重新生成。这样用户修改的内容/etc/resolv.conf就会被持久化。

我发现创建/etc/wsl.conf不起作用,但还是留下了它:

[network]
generateResolvConf = false
Run Code Online (Sandbox Code Playgroud)


srb*_*ton 5

在 cmd 上运行这些命令:

$ wsl --shutdown
$ netsh winsock reset
$ netsh int ip reset all
$ netsh winhttp reset proxy
$ ipconfig /flushdns
$ netsh winsock reset
$ shutdown /r 
Run Code Online (Sandbox Code Playgroud)