ubuntu18.04 netplan 设置dns,dns不生效

ubuntu18.04通过netplan设置DNS,而查看DNS时发现DNS并没生效,其原因是系统指向的问题。

以下是解决步骤:

1.编辑netplan下的yaml文件
2.重启网络 netplan apply
3.删除原resolv.conf, rm -rf /etc/resolv.conf
4.创建软连接,ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

对于ubuntu18.04的环境,提供了用yaml文件形式修改网络配置,但系统初始设置的/etc/resolv.conf -> /run/systemd/resolv/stub-resolv.conf,如果我们通过/etc/netplan/xxx.yaml修改我们的网络配置,假如更改了DNS,这相当于用户设置的,对应在网卡重启后,即netplan apply后更新的dns配置文件是/run/systemd/resolv/resolv.conf,系统的stub-resolv.conf还是原内容,此时我们通过/etc/resolv.conf查看DNS的配置,其实还是系统初始的软连接,所以如果存在文件用到/etc/resolv.conf时,一定要在网卡重启后,删除原先的/etc/resolv.conf,重新创建软连接,从而使根据用户修改的网络DNS配置指向/etc/resolv.conf,否则还是指向系统的链接指向。

重点

  • 系统初始链接: /etc/resolv.conf -> /run/systemd/resolv/stub-resolv.conf
  • /run/systemd/resolv/resolv.conf: 用户配置网络的DNS,重启后更新此文件
  • /run/systemd/resolv/stub-resolv.conf: 系统配置文件,默认127.0.0.53
  • 用户更改网络的DNS,指向/etc/resolv.conf的文件应改为 /run/systemd/resolv/resolv.conf

更多

外网解答:

Using resolv.conf instead of stub-resolv.conf will bypass a lot of systemd-resolved configuration, such as DNS answer caching, per-interface DNS configuration, DNSSec enforcement, etc.

When using stub-resolv.conf, applications will make DNS requests to the DNS stub resolver provided by systemd on address 127.0.0.53. This stub resolver will proxy the DNS requests to the upstream DNS resolvers configured in systemd-resolved, applying whatever logic it wants to those requests and answers, like caching them.

When using resolv.conf, applications will directly make DNS requests to the "real" (aka. upstream) DNS resolvers configured in systemd-resolved. In this case, systemd-resolved only acts as a "resolv.conf manager", not as a DNS resolver itself.

参考:

posted on 2021-10-23 19:21  进击的davis  阅读(2436)  评论(0编辑  收藏  举报

导航