Command-line to list DNS servers used by my system

Solution 1:

resolv.conf isn't really used anymore, unless you implement it yourself. The network manager does it now. I created an alias to list the DNS servers on my system, as I sometimes switch from OpenDNS to Google's open DNS.

Ubuntu >= 15

nmcli device show <interfacename> | grep IP4.DNS

Ubuntu <= 14

nmcli dev list iface <interfacename> | grep IP4

In my case, <interfacename> is eth0, which is common, but not always the case.

See if this is what you want.

EDIT:

I think resolv.conf is actually used indirectly, because the network manager creates the server that listens on 127.0.0.1, but I was told that this is an implementation detail that should not be counted on. I think that if you enter DNS addresses before this entry, they might get used, but I'm not sure exactly how this works. I think it's best to use the network manager in most cases, when possible.

Solution 2:

In Ubuntu 18.04 and 20.04 you can use systemd-resolve --status

Solution 3:

This is valid for Ubuntu 13.10 and earlier. For Ubuntu 14.04 and above, see Koala Yeung's answer to: How to know what DNS am I using in Ubuntu from 14.04 onwards


Use

nm-tool

You will get an output similar to

NetworkManager Tool

State: connected (global)

- Device: eth0  [Wired connection 1] -------------------------------------------
  Type:              Wired
  Driver:            e1000e
  State:             connected
  Default:           yes
  HW Address:        00:11:22:33:44:55

  Capabilities:
    Carrier Detect:  yes
    Speed:           1000 Mb/s

  Wired Properties
    Carrier:         on

  IPv4 Settings:
    Address:         10.21.6.13
    Prefix:          24 (255.255.255.0)
    Gateway:         10.21.6.1

    DNS:             10.22.5.133
    DNS:             10.22.5.3

Or to see just the DNS do

nm-tool | grep DNS

Solution 4:

The two top-scoring answers, nmcli dev list iface <interfacename> | grep IP4 and nm-tool both assume that network-manager is in control. Which it is - on desktop machines most of the time at least. But the fuller answer is that sometimes network-manager is not in control. E.g. vpnc messes with /etc/resolv.conf directly.

So: First check if 127.0.0.1/localhost is used. This could be done with dig:

> dig something.unknown  | grep SERVER:
;; SERVER: 127.0.0.1#53(127.0.0.1)

Now you know that we are using localhost. Go ahead with one of the popular answers. I like:

> nm-tool | grep DNS:
    DNS:             8.8.8.8

But if 127.0.0.1/localhost is not used, then nm-tool's and nmcli's output will be misleading:

> dig something.unknown  | grep SERVER:
;; SERVER: 172.22.216.251#53(172.22.216.251)
> nm-tool | grep DNS:
    DNS:             8.8.8.8

Here, dig is correct and nm-tool's information is misleading. In reality addresses local to the environment I've VPN-ed into are resolved correctly. All of which Google's DNS 8.8.8.8 doesn't know about.

This is because after connecting to a VPN with vpnc, it puts a line in /etc/resolv.conf so it looks like:

# 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
nameserver 1.2.3.4
nameserver 127.0.0.1
search MyDomain