【原创】Ubuntu systemd-resolve 命令使用

概要

1、简介

systemd-resolve 是 Ubuntu 下 DNS 解析相关的命令,能使用它来操作 DNS 相关的功能。

下面列举 systemd-resolve 常用的一些操作

2、相关环境

Ubuntu 18.04.5

正文

1、查看 DNS 服务状态

systemd-resolve --status 

示例:

$ systemd-resolve --status
Global
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa
                      168.192.in-addr.arpa
                      17.172.in-addr.arpa
                      18.172.in-addr.arpa
                      19.172.in-addr.arpa
                      20.172.in-addr.arpa
                      21.172.in-addr.arpa
                      22.172.in-addr.arpa
                      23.172.in-addr.arpa
                      24.172.in-addr.arpa
                      25.172.in-addr.arpa
                      26.172.in-addr.arpa
                      27.172.in-addr.arpa
                      28.172.in-addr.arpa
                      29.172.in-addr.arpa
                      30.172.in-addr.arpa
                      31.172.in-addr.arpa
                      corp
                      d.f.ip6.arpa
                      home
                      internal
                      intranet
                      lan
                      local
                      private
                      test
Link 2 (ens3)
      Current Scopes: DNS
       LLMNR setting: yes
MulticastDNS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
         DNS Servers: 192.168.0.1

2、解析域名的的 IP

systemd-resolve DOMAIN

示例:

jiangzl@jiangzl:~$ systemd-resolve baidu.com
baidu.com: 220.181.38.148
           39.156.69.79
-- Information acquired via protocol DNS in 159.3ms.
-- Data is authenticated: no

 也可以使用 nslookup domain 来解析,如下示例:

$ nslookup baidu.com          
Server:         127.0.0.53
Address:        127.0.0.53#53
Non-authoritative answer:
Name:   baidu.com
Address: 39.156.69.79
Name:   baidu.com
Address: 220.181.38.148

3、设置网卡的 DNS Server

systemd-resolve 命令可以用来设置指定网卡的 DNS Server,如下

systemd-resolve --set-dns={DNS_SERVER_IP} --interface {ITERFACE_NAME}

示例:

$ sudo systemd-resolve --set-dns '8.8.8.8' --interface ens3
$ 
$ systemd-resolve --status | grep 'DNS Servers'
         DNS Servers: 8.8.8.8

4、重置网卡的 DNS 设置

systemd-resolve --revert --interface {ITERFACE_NAME}

注:该命令会清除指定网上的所有 DNS Server 的设置,请慎用。

如下示例:

$ sudo systemd-resolve --revert --interface=ens33

5、刷新本地 DNS 缓存

如果有时候某些域名解析出现了问题,可尝试清除本地 DNS 缓存试试。

systemd-resolve --flush-caches

示例:

$ sudo systemd-resolve --flush-caches

6、查看 DNS 相关数据

systemd-resolve --statistics

示例:

$ systemd-resolve --statistics      
DNSSEC supported by current servers: no
Transactions
Current Transactions: 0
  Total Transactions: 420
Cache
  Current Cache Size: 0
          Cache Hits: 0
        Cache Misses: 0
DNSSEC Verdicts
              Secure: 0
            Insecure: 0
               Bogus: 0
       Indeterminate: 0

注:可以看到的是清除缓存后,本地的 Cache 大小已经是 0 了,而如果我们使用 systemd-resolve domain 解析了域名,Cache 便会增加。

如下示例:

$ systemd-resolve baidu.com          
baidu.com: 220.181.38.148
           39.156.69.79
-- Information acquired via protocol DNS in 19.5ms.
-- Data is authenticated: no
$ 
$ systemd-resolve taobao.com         
taobao.com: 140.205.94.189
            140.205.220.96
-- Information acquired via protocol DNS in 10.1ms.
-- Data is authenticated: no
$ 
$ systemd-resolve --statistics       
DNSSEC supported by current servers: no
Transactions
Current Transactions: 0
  Total Transactions: 431
Cache
  Current Cache Size: 2
          Cache Hits: 0
        Cache Misses: 34
DNSSEC Verdicts
              Secure: 0
            Insecure: 0
               Bogus: 0
       Indeterminate: 0

总结

以上就是 Ubuntu 系统下的 sytemd-resolve DNS 解析相关的一些常用命令的使用,更多命令可参考帮助文档。

附录

systemd-resolve 常用参数及命令:

jiangzl@jiangzl:~$ systemd-resolve --help
systemd-resolve [OPTIONS...] HOSTNAME|ADDRESS...
systemd-resolve [OPTIONS...] --service [[NAME] TYPE] DOMAIN
systemd-resolve [OPTIONS...] --openpgp EMAIL@DOMAIN...
systemd-resolve [OPTIONS...] --statistics
systemd-resolve [OPTIONS...] --reset-statistics
Resolve domain names, IPv4 and IPv6 addresses, DNS records, and services.
  -h --help                 Show this help
     --version              Show package version
     --no-pager             Do not pipe output into a pager
  -4                        Resolve IPv4 addresses
  -6                        Resolve IPv6 addresses
  -i --interface=INTERFACE  Look on interface
  -p --protocol=PROTO|help  Look via protocol
  -t --type=TYPE|help       Query RR with DNS type
  -c --class=CLASS|help     Query RR with DNS class
     --service              Resolve service (SRV)
     --service-address=BOOL Resolve address for services (default: yes)
     --service-txt=BOOL     Resolve TXT records for services (default: yes)
     --openpgp              Query OpenPGP public key
     --tlsa                 Query TLS public key
     --cname=BOOL           Follow CNAME redirects (default: yes)
     --search=BOOL          Use search domains for single-label names(default: yes)
     --raw[=payload|packet] Dump the answer as binary data
     --legend=BOOL          Print headers and additional info (default: yes)
     --statistics           Show resolver statistics
     --reset-statistics     Reset resolver statistics
     --status               Show link and server status
     --flush-caches         Flush all local DNS caches
     --reset-server-features Forget learnt DNS server feature levels
     --set-dns=SERVER       Set per-interface DNS server address
     --set-domain=DOMAIN    Set per-interface search domain
     --set-llmnr=MODE       Set per-interface LLMNR mode
     --set-mdns=MODE        Set per-interface MulticastDNS mode
     --set-dnssec=MODE      Set per-interface DNSSEC mode
     --set-nta=DOMAIN       Set per-interface DNSSEC NTA
     --revert               Revert per-interface configuration

You may also like...

发表评论

电子邮件地址不会被公开。