WSL2 Ubuntu中apt update命令報錯,無法解析域名解決方法


問題描述

今天在WSL2 (Ubuntu)上運行apt update命令,結果出現以下錯誤:

$ apt update
Err:1 http://archive.ubuntu.com/ubuntu bionic InRelease
  Could not connect to archive.ubuntu.com:80 (91.189.91.23). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.149). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.152). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.161). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.162). - connect (111: Connection refused)
Err:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
  Unable to connect to archive.ubuntu.com:http:
Err:3 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
  Unable to connect to archive.ubuntu.com:http:
Err:4 http://security.ubuntu.com/ubuntu bionic-security InRelease
  Could not connect to security.ubuntu.com:80 (91.189.88.162). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.91.23). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.91.26). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.149). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.152). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.161). - connect (111: Connection refused)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic/InRelease  Could not connect to archive.ubuntu.com:80 (91.189.91.23). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.149). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.152). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.161). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.162). - connect (111: Connection refused)
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease  Unable to connect to archive.ubuntu.com:http:
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease  Unable to connect to archive.ubuntu.com:http:
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease  Could not connect to security.ubuntu.com:80 (91.189.88.162). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.91.23). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.91.26). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.149). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.152). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.161). - connect (111: Connection refused)
W: Some index files failed to download. They have been ignored, or old ones used instead.

嘗試重新修改了/etc/apt/source.list,但沒效果。

解決方案

# WSL2 Ubuntu
# Delete auto-generated files
$ rm /etc/resolv.conf

# Enable changing /etc/resolv.conf
# Enable extended attributes on Windows drives
$ cat /etc/wsl.conf
# Enable extra metadata options by default
[automount]
# enabled = true
# root = /mnt/
# options = "metadata,umask=22,fmask=11"
# mountFsTab = false
root = /
options = "metadata"
[network]
generateResolvConf = false

$ vi /etc/resolv.conf
# Add the lines 
nameserver 8.8.8.8
nameserver 8.8.4.4

在這里插入圖片描述

# Exit WSL2
$ exit

Win10中以管理員身份打開CMDPowershell,輸入以下命令

> wsl --shutdown
> netsh winsock reset
> netsh int ip reset all
> netsh winhttp reset proxy
> ipconfig /flushdns

最后在Windows Search > Network Reset中重置下網絡即可。
本機測試有效。
在這里插入圖片描述

在這里插入圖片描述

參考GitHub Issue
https://github.com/microsoft/WSL/issues/5382 @rescenic


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM