1. 临时生效(随时会被刷掉)
关闭systemd-resolved进程
# systemctl stop systemd-resolved
# echo "nameserver 8.8.8.8" /etc/resolv.conf
  1. 永久生效(使用ystemd-resolved, ubuntu18.04以及之后的版本默认使用此工具管理dns)
    (方案一)
    2.1 安装resolvconf工具
# apt install resolvconf

2.2 配置

# echo "nameserver 8.8.8.8" >/etc/resolvconf/resolv.conf.d/head

2.3 更新配置

# resolvconf -u

2.4 检查配置是否生效

# cat /etc/resolv.conf
nameserver 8.8.8.8

方案二
2.1 打开 /etc/systemd/resolved.conf,修改为

[Resolve]
DNS=8.8.8.8 # 设置的是域名解析服务器的IP地址
#FallbackDNS=
#Domains=
LLMNR=no # 设置的是禁止运行LLMNR(Link-Local Multicast Name Resolution),否则systemd-resolve会监听5535端口
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

2.2 重新启动systemd-resolved服务

$ sudo systemctl restart systemd-resolved

2.2.3 查看当前dns状态

$ sudo systemd-resolve --status
Global 
            DNS Servers: 8.8.8.8
            ...