ubuntu18 修改dns服务器地址为google

域名解析被干扰的有点严重,直接使用谷歌dns服务器来解析ip

第一、永久修改DNS方法

1、修改 /etc/systemd/resolved.conf 文件

vi  /etc/systemd/resolved.conf

这里我们可以看到这些参数:

# 指定 DNS 服务器,以空白分隔,支持 IPv4 或 IPv6 位置
DNS=8.8.8.8 114.114.114.114
# 备用 DNS 服务器
FallbackDNS=8.8.8.8
# 设置搜索域名
Domains=domain.com
# 设置 LLMNR 是否激活,可用的选项有 yes、no、resolve
LLMNR=yes
# 设置 MulticastDNS 是否激活,可用的选项有 yes、no、resolve
MulticastDNS=yes
# 设置 DNSSEC 是否激活,可用的选项有 yes、no、allow-downgrade
DNSSEC=yes
# 设置缓存是否激活,可用的选项有 yes、no、no-negative
Cache=no-negative

根据需要修改 resolved.conf 文件中的DNS,然后保存。
我的配置是

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
DNS=8.8.8.8 114.114.114.114
FallbackDNS=8.8.4.4
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
Cache=no
#DNSStubListener=yes

2、重启 systemd-resolved 服务

systemctl restart systemd-resolved

3、设置开机启动 systemd-resolved 服务

systemctl enable systemd-resolved

4、备份 systemd-resolved 托管文件 resolv.conf

mv /etc/resolv.conf /etc/resolv.conf.bak

重新生成。

ln -s /run/systemd/resolve/resolv.conf /etc/

第二、临时修改DNS方法

如果我们临时使用的话,也可以临时修改DNS。

修改下面文件:

vi /etc/resolv.conf

加入想要修改的DNS

nameserver 8.8.8.8
nameserver 8.8.4.4

如果多个DNS,就一行一个,修改之后保存退出即可;此方法修改后即刻生效,但重启后失效。

清理现有dns缓存

sudo systemd-resolve --flush-caches
sudo systemctl restart systemd-resolved

查看现在的dns

 cat /etc/resolv.conf


posted @ 2023-06-20 15:44  HumorChen99  阅读(18)  评论(0编辑  收藏  举报  来源