Ubuntu sudo apt-get update无法解析域名的解决方法

发布于 2021-11-24  93 次阅读


前言

这是个很常见的问题,记录下解决方案

参考链接

Ubuntu换源

Ubuntu18.04 sudo apt update无法解析域名的解决方案

报错信息

错误:xx http://xxx...
W: 无法下载 http://xxx...
W: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。

问题1. 因为DNS解析有误,或者是DNS未配置

问题2. source.list 镜像源信息发生改变,需要重新配置

对于问题1:

查看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

解决二

  1. 先做源备份,当然如果目前源不可用,可以忽略这一步
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
  1. 输入如下指令打开sources.list配置文件更换源(下方是阿里云的源)
sudo vim/etc/apt/sources.list

//配置内容如下

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse 

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse 

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse 

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse 

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse 

deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse 

deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse 

deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse 

deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

中科大镜像源:

# 默认注释了源码仓库,如有需要可自行取消注释
deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

推荐看看中科大的镜像源更换说明,非常详细

Ubuntu 源帮助

  1. 输入命令更新源
sudo apt-get update
  1. 输入命令更新下软件
sudo apt-get upgrade

结语

centos中的后续遇到在补充,最近没怎么用233……


随心所至