【親測有效】Ubuntu18.04 sudo apt update無法解析域名的解決方案

問題描述如下:

拿起了封塵已久的ThinkPad,輸入 sudo apt update 的時候,發現這個命令變得不好使了,具體出現的問題如下圖所示:

#( 09/08/19@ 2:44下午 )( python@Sakura ):~/下載/shadowsocksr@manyuser✔
   sudo apt update
忽略:1 http://dl.google.com/linux/chrome/deb stable InRelease                  
命中:2 http://dl.google.com/linux/chrome/deb stable Release                    
0% [正在連接 cn.archive.ubuntu.com] [正在連接 cz.archive.ubuntu.co0% [正在連接 c0% [錯誤:4 http://ppa.launchpad.net/obsproject/obs-studio/ubuntu bionic InRelease      
  無法解析域名“ppa.launchpad.net”
錯誤:5 http://cz.archive.ubuntu.com/ubuntu bionic InRelease                        
  無法解析域名“cz.archive.ubuntu.com”
錯誤:6 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease           
  無法解析域名“packages.microsoft.com”
錯誤:7 https://download.sublimetext.com apt/stable/ InRelease                      
  無法解析域名“download.sublimetext.com”
錯誤:8 https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic InRelease         
  無法解析域名“mirrors.ustc.edu.cn”
錯誤:9 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease           
  無法解析域名“archive.ubuntukylin.com”
錯誤:10 http://ppa.launchpad.net/ondrej/php/ubuntu bionic InRelease                
  無法解析域名“ppa.launchpad.net”
錯誤:11 http://cn.archive.ubuntu.com/ubuntu bionic InRelease                 
  無法解析域名“cn.archive.ubuntu.com”
錯誤:12 http://packages.microsoft.com/repos/vscode stable InRelease          
  無法解析域名“packages.microsoft.com”
錯誤:13 http://ppa.launchpad.net/openjdk-r/ppa/ubuntu bionic InRelease
  無法解析域名“ppa.launchpad.net”
錯誤:14 http://cn.archive.ubuntu.com/ubuntu bionic-updates InRelease
  無法解析域名“cn.archive.ubuntu.com”
錯誤:15 http://ppa.launchpad.net/openshot.developers/ppa/ubuntu bionic InRelease
  無法解析域名“ppa.launchpad.net”
錯誤:16 http://cn.archive.ubuntu.com/ubuntu bionic-security InRelease
  無法解析域名“cn.archive.ubuntu.com”
錯誤:17 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic InRelease
  無法解析域名“ppa.launchpad.net”
錯誤:18 http://cn.archive.ubuntu.com/ubuntu bionic-proposed InRelease
  無法解析域名“cn.archive.ubuntu.com”
錯誤:19 http://ppa.launchpad.net/webupd8team/java/ubuntu bionic InRelease
  無法解析域名“ppa.launchpad.net”
正在讀取軟件包列表... 完成
正在分析軟件包的依賴關係樹       
正在讀取狀態信息... 完成       
有 382 個軟件包可以升級。請執行 ‘apt list --upgradable’ 來查看它們。
W: 無法下載 http://cn.archive.ubuntu.com/ubuntu/dists/bionic/InRelease  無法解析域名“cn.archive.ubuntu.com”
W: 無法下載 http://cn.archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease  無法解析域名“cn.archive.ubuntu.com”
W: 無法下載 http://cn.archive.ubuntu.com/ubuntu/dists/bionic-security/InRelease  無法解析域名“cn.archive.ubuntu.com”
W: 無法下載 http://cn.archive.ubuntu.com/ubuntu/dists/bionic-proposed/InRelease  無法解析域名“cn.archive.ubuntu.com”
W: 無法下載 http://cz.archive.ubuntu.com/ubuntu/dists/bionic/InRelease  無法解析域名“cz.archive.ubuntu.com”
W: 無法下載 https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/dists/bionic/InRelease  無法解析域名“mirrors.ustc.edu.cn”
W: 無法下載 https://packages.microsoft.com/ubuntu/18.04/prod/dists/bionic/InRelease  無法解析域名“packages.microsoft.com”
W: 無法下載 http://ppa.launchpad.net/obsproject/obs-studio/ubuntu/dists/bionic/InRelease  無法解析域名“ppa.launchpad.net”
W: 無法下載 http://ppa.launchpad.net/ondrej/php/ubuntu/dists/bionic/InRelease  無法解析域名“ppa.launchpad.net”
W: 無法下載 http://ppa.launchpad.net/openjdk-r/ppa/ubuntu/dists/bionic/InRelease  無法解析域名“ppa.launchpad.net”
W: 無法下載 http://ppa.launchpad.net/openshot.developers/ppa/ubuntu/dists/bionic/InRelease  無法解析域名“ppa.launchpad.net”
W: 無法下載 http://archive.ubuntukylin.com:10006/ubuntukylin/dists/xenial/InRelease  無法解析域名“archive.ubuntukylin.com”
W: 無法下載 https://download.sublimetext.com/apt/stable/InRelease  無法解析域名“download.sublimetext.com”
W: 無法下載 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/dists/bionic/InRelease  無法解析域名“ppa.launchpad.net”
W: 無法下載 http://packages.microsoft.com/repos/vscode/dists/stable/InRelease  無法解析域名“packages.microsoft.com”
W: 無法下載 http://ppa.launchpad.net/webupd8team/java/ubuntu/dists/bionic/InRelease  無法解析域名“ppa.launchpad.net”
W: 部分索引文件下載失敗。如果忽略它們,那將轉而使用舊的索引文件。

我們可以看到,大概我們無法對像"cn.archive.ubuntu.com"、"packages.microsoft.com"之類的網站進行域名解析。出現這一問題主要是因爲DNS解析有誤,或者是DNS未配置。我們可以查看一下DNS Server:

cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0
search DHCP HOST

只有一個DNS Server的地址 127.0.0.53,我們可以採取如下方法給其配置DNS Server。

解決方案:

方案一:重啓會失效

我們只需要加入DNS服務器地址,讓其能夠自動去解析即可。不過有個問題就是重啓以後可能會失效,需要重新對其進行配置。

1. sudo vim /etc/resolv.conf # 添加如下內容
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 127.0.0.1
2. 輸入Esc,:wq,保存並退出
3. sudo /etc/init.d/networking restart

我們再輸入 sudo apt update 進行更新即可。

方案二:永久有效

1. sudo apt install resolvconf
2. sudo vim /etc/resolvconf/resolv.conf.d/base
在裏面插入:
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 127.0.0.1
3. 輸入Esc,:wq,保存並退出
4. sudo resolvconf -u
5. cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.8.8
nameserver 8.8.4.4

在這裏,我還要提一點相關內容。雖然我按照方案二並沒有達到想要的這種結果(理論上應該是沒問題的),可能是因爲我這邊配置全局代理,讓它默認自動走本地代理路線,所以DNS Server解析的地址爲127.0.0.1,不過至少已經可以使用了,如果方案二行不通的同學,你可以採用方案一,稍微麻煩一點,每次都要進行修改罷了。

最終的結果如下:

#( 09/08/19@ 2:58下午 )( python@Sakura ):~/下載/shadowsocksr@manyuser✔
   sudo apt update                    
忽略:1 http://dl.google.com/linux/chrome/deb stable InRelease
命中:2 http://dl.google.com/linux/chrome/deb stable Release                        
獲取:3 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease [18.1 kB] 
獲取:4 http://packages.microsoft.com/repos/vscode stable InRelease [3,181 B]       
命中:6 http://ppa.launchpad.net/obsproject/obs-studio/ubuntu bionic InRelease      
命中:7 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease           
獲取:8 http://packages.microsoft.com/repos/vscode stable/main amd64 Packages [139 kB]
命中:9 http://cz.archive.ubuntu.com/ubuntu bionic InRelease                        
命中:10 http://cn.archive.ubuntu.com/ubuntu bionic InRelease                       
命中:11 http://ppa.launchpad.net/ondrej/php/ubuntu bionic InRelease                
命中:12 https://download.sublimetext.com apt/stable/ InRelease                     
獲取:13 http://cn.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]     
命中:14 http://ppa.launchpad.net/openjdk-r/ppa/ubuntu bionic InRelease             
命中:15 http://ppa.launchpad.net/openshot.developers/ppa/ubuntu bionic InRelease   
命中:16 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic InRelease   
命中:17 http://ppa.launchpad.net/webupd8team/java/ubuntu bionic InRelease          
獲取:18 http://cn.archive.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]    
獲取:19 http://cn.archive.ubuntu.com/ubuntu bionic-proposed InRelease [242 kB]     
命中:20 https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic InRelease        
獲取:21 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 DEP-11 Metadata [285 kB]
獲取:22 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main DEP-11 48x48 Icons [70.9 kB]
獲取:23 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main DEP-11 64x64 Icons [140 kB]
獲取:24 http://cn.archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 DEP-11 Metadata [2,464 B]
獲取:25 http://cn.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 DEP-11 Metadata [253 kB]
獲取:26 http://cn.archive.ubuntu.com/ubuntu bionic-updates/universe DEP-11 48x48 Icons [197 kB]
獲取:27 http://cn.archive.ubuntu.com/ubuntu bionic-updates/universe DEP-11 64x64 Icons [447 kB]
獲取:28 http://cn.archive.ubuntu.com/ubuntu bionic-security/main amd64 DEP-11 Metadata [22.7 kB]
獲取:29 http://cn.archive.ubuntu.com/ubuntu bionic-security/main DEP-11 48x48 Icons [10.4 kB]
獲取:30 http://cn.archive.ubuntu.com/ubuntu bionic-security/main DEP-11 64x64 Icons [31.7 kB]
獲取:31 http://cn.archive.ubuntu.com/ubuntu bionic-security/multiverse amd64 DEP-11 Metadata [2,464 B]
獲取:32 http://cn.archive.ubuntu.com/ubuntu bionic-security/universe amd64 DEP-11 Metadata [42.1 kB]
獲取:33 http://cn.archive.ubuntu.com/ubuntu bionic-security/universe DEP-11 48x48 Icons [16.4 kB]
獲取:34 http://cn.archive.ubuntu.com/ubuntu bionic-security/universe DEP-11 64x64 Icons [111 kB]
獲取:35 http://cn.archive.ubuntu.com/ubuntu bionic-proposed/main amd64 DEP-11 Metadata [7,160 B]
已下載 2,220 kB,耗時 35秒 (63.8 kB/s)                                             
正在讀取軟件包列表... 完成
正在分析軟件包的依賴關係樹       
正在讀取狀態信息... 完成       
有 383 個軟件包可以升級。請執行 ‘apt list --upgradable’ 來查看它們。

這樣就OK了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章