跳转至

Ubuntu配置DNS解析

国内国外都有很多的厂商提供了免费的公共DNS,比如:

DNSPod Public DNS:119.29.29.29 阿里云公共DNS:223.5.5.5/223.6.6.6 Google Public DNS:8.8.8.8/8.8.4.4

系统通过读取/etc/resolv.conf中的DNS地址进行查询,默认DNS服务器IP是127.0.0.53

# 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

该文件是一个软链接,其指向/run/systemd/resolve/resolv.conf

如何设置公共DNS

  • 第一步:调整/etc/resolv.conf的软链接,使其指向文件/run/systemd/resolve/resolv.conf

    $ cd /etc
    $ sudo ln -sf /run/systemd/resolve/resolv.conf 
    

  • 第二步:修改配置文件/etc/systemd/resolved.conf,添加新的DNS服务器IP

    $ cat /etc/systemd/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=1.1.1.1 192.168.1.1 119.29.29.29
    #FallbackDNS=
    #Domains=
    #LLMNR=no
    #MulticastDNS=no
    #DNSSEC=no
    #Cache=yes
    #DNSStubListener=yes
    

  • 第三步:重启系统服务

    sudo service systemd-resolved restart
    

完成后,可以查看新的/etc/resolv.conf配置