Ubuntu 永久修改DNS

前言

话说Ubuntu的DNS设置,跟centos还不太一样。centos只要把dns写在网络配置项,怎么重启网络都能启用。

然而Ubuntu就不行,因为它有/etc/resolv.conf, 每次一重启网络,这个文件会被覆盖,如下:

root@home:~# 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 127.0.0.53

每次要通外网我还得手动改这个文件,烦求得很。

于是我就想,是不是有办法永久修改这个文件,使得每次重启网络的时候,都可以省去手动修改文件的麻烦。

 

方法

某位伟人讲过,办法总比困难多,于是我就去找办法。

终于看到一篇文章,他是这么讲的:

修改resolved.conf文件,然后reload服务

我们看看他的要点是什么啊:

1. 停掉NetworkManager

systemctl disable --now NetworkManager

 

2. 修改resolved.conf文件

#  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=192.168.1.2    #把这个DNS服务器修改为你要改的
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

 

3. 重启systemd-resolved服务

systemctl restart systemd-resolved.service

 

4. 查看是否修改成功

systemd-resolve --status
Global
         DNS Servers: 192.168.1.2
          DNSSEC NTA: 10.in-addr.arpa

修改成功。

于是我们接下来每次重启网络,它都会使用我们希望的DNS做域名服务器了,就不需要重新改了。

 

鸣谢

码农家园

Ubuntu 永久修改DNS

上一篇:进程间通信的集中方式(含实例源码)


下一篇:Macbook M1芯片录制屏幕声音