ubuntu-22.04自定义DNS并配置永久生效

更新的时候发现没有网

root@ubuntu22:~# sudo apt update
Ign:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Ign:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Ign:3 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Ign:4 http://archive.ubuntu.com/ubuntu jammy-security InRelease
Ign:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Ign:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Ign:3 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Ign:4 http://archive.ubuntu.com/ubuntu jammy-security InRelease
Ign:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Ign:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Ign:3 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Ign:4 http://archive.ubuntu.com/ubuntu jammy-security InRelease
Err:1 http://archive.ubuntu.com/ubuntu jammy InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu jammy-security InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy-security/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.

只能先临时改下DNS

cat /etc/resolv.conf
root@ubuntu22:~# cat /etc/resolv.conf
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# 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 "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically 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 trust-ad
search localdomain

备份一下

cp /etc/resolv.conf /etc/resolv.conf"$(date +'%Y-%m-%d_%H-%M-%S')"

编辑DNS

vim /etc/resolv.conf

加入如下两行

nameserver 8.8.4.4
nameserver 8.8.8.8

完整示例

# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# 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 "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically 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 trust-ad
search localdomain
nameserver 8.8.4.4
nameserver 8.8.8.8

此时再更新就正常了

root@ubuntu22:~# sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:5 http://archive.ubuntu.com/ubuntu jammy/main amd64 c-n-f Metadata [30.3 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy/restricted amd64 c-n-f Metadata [488 B]
Get:7 http://archive.ubuntu.com/ubuntu jammy/universe amd64 c-n-f Metadata [286 kB]
Get:8 http://archive.ubuntu.com/ubuntu jammy/multiverse amd64 c-n-f Metadata [8,372 B]
Get:9 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [698 kB]
Get:10 http://archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [159 kB]
Get:11 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 c-n-f Metadata [10.8 kB]
Get:12 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [417 kB]
Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [63.9 kB]
Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 c-n-f Metadata [580 B]
Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [747 kB]
Get:16 http://archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [123 kB]                                                                                                                                                
Get:17 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 c-n-f Metadata [4,404 B]                                                                                                                                         
Get:18 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [7,220 B]                                                                                                                                             
Get:19 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse Translation-en [2,360 B]                                                                                                                                             
Get:20 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 c-n-f Metadata [420 B]                                                                                                                                         
Get:21 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [3,008 B]                                                                                                                                                 
Get:22 http://archive.ubuntu.com/ubuntu jammy-backports/main Translation-en [1,432 B]                                                                                                                                                 
Get:23 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 c-n-f Metadata [272 B]                                                                                                                                             
Get:24 http://archive.ubuntu.com/ubuntu jammy-backports/restricted amd64 c-n-f Metadata [116 B]                                                                                                                                       
Get:25 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [6,744 B]                                                                                                                                             
Get:26 http://archive.ubuntu.com/ubuntu jammy-backports/universe Translation-en [9,360 B]                                                                                                                                             
Get:27 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 c-n-f Metadata [352 B]                                                                                                                                         
Get:28 http://archive.ubuntu.com/ubuntu jammy-backports/multiverse amd64 c-n-f Metadata [116 B]                                                                                                                                       
Get:29 http://archive.ubuntu.com/ubuntu jammy-security/main amd64 Packages [462 kB]                                                                                                                                                   
Get:30 http://archive.ubuntu.com/ubuntu jammy-security/main Translation-en [102 kB]                                                                                                                                                   
Get:31 http://archive.ubuntu.com/ubuntu jammy-security/main amd64 c-n-f Metadata [3,924 B]                                                                                                                                            
Get:32 http://archive.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [376 kB]                                                                                                                                             
Get:33 http://archive.ubuntu.com/ubuntu jammy-security/restricted Translation-en [57.6 kB]                                                                                                                                            
Get:34 http://archive.ubuntu.com/ubuntu jammy-security/restricted amd64 c-n-f Metadata [524 B]                                                                                                                                        
Get:35 http://archive.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [606 kB]                                                                                                                                               
Get:36 http://archive.ubuntu.com/ubuntu jammy-security/universe Translation-en [77.1 kB]                                                                                                                                              
Get:37 http://archive.ubuntu.com/ubuntu jammy-security/universe amd64 c-n-f Metadata [2,408 B]                                                                                                                                        
Get:38 http://archive.ubuntu.com/ubuntu jammy-security/multiverse amd64 c-n-f Metadata [228 B]                                                                                                                                        
Fetched 4,594 kB in 7s (633 kB/s)                                                                                                                                                                                                     
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
96 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@ubuntu22:~# 

但是这样配置DNS在重启了服务器之后DNS还会被还原,因此需要更改DNS使其永久生效

因为我是国内机子,因此需要先更换成国内的源,这里以清华源为例:

https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

备份一下

cp /etc/apt/sources.list /etc/apt/sources.list"$(date +'%Y-%m-%d_%H-%M-%S')"

删除旧的“源”文件并重新创建

rm -rf /etc/apt/sources.list
vim /etc/apt/sources.list

将下面的“源”配置粘贴到/etc/apt/sources.list

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

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

安装resolvconf并允许resolvconf开机自启

sudo apt update
sudo apt install resolvconf
sudo systemctl status resolvconf.service
sudo systemctl start resolvconf.service
sudo systemctl enable resolvconf.service
sudo systemctl status resolvconf.service

e9a7fe0b50262775fd4962a0385dcd26.png

接下来设定永久生效的DNS

vim /etc/resolvconf/resolv.conf.d/head

加入如下两行

nameserver 8.8.4.4
nameserver 8.8.8.8

9ae09c259237a251450bdb3b39367abe.png

重启resolvconf

sudo systemctl restart resolvconf.service
sudo systemctl restart systemd-resolved.service

重启之后可以看到resolv.conf已经被自动更新了,之后重启服务器之后该DNS也会自动生效

root@ubuntu22:/etc/ssh# 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
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 8.8.8.8 
nameserver 8.8.4.4
nameserver 127.0.0.53
search localdomain
options edns0 trust-ad

ubuntu-22.04-live-server-amd64.iso

这篇文章有用吗?

点击星号为它评分!

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

发表评论