396

Is there a command to list DNS servers used by my system?

I tried

$ 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
nameserver 127.0.0.1
$ cat /etc/network/interfaces 
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

But it doesn't list any servers. If I go to "Network Manager GUI Tool", in the Wireless section it lists "DNS 192.168.1.1 8.8.8.8 8.8.4.4".

Can I get the same information from the command line?

I am using Ubuntu 12.04 LTS.

14 Answers14

365

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. You can see your interfaces with

nmcli device status

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.

Niko Fohr
  • 1,531
Marty Fried
  • 18,466
  • 3
    thanks, yes that seems to be working, ubuntu networking seems to be confusing, so I can set dns servers in resolve.conf/base or in /etc/network/interfaces or in network manager, is there a definitive guide for ubuntu networking? – Anurag Uniyal Jun 18 '12 at 15:26
  • If you use the GUI, then the best place to set it is by creating profiles in the Network Connections Dialog. I duplicated the default, then edited the duplicate to make the changes I wanted, keeping the default to make sure I always had a working profile. Then, it's easy to switch profiles. I don't know how to do this without the GUI, but there is a user "James Henstridge" who is very knowledgeable on Ubuntu's networking; you might try searching askubuntu for his information. He told me about the command I gave you in this post. – Marty Fried Jun 18 '12 at 15:35
  • 1
    http://www.stgraber.org/2012/02/24/dns-in-ubuntu-12-04/ is a nice article abount DNS resolution in ubuntu 12.04 – Anurag Uniyal Jun 18 '12 at 15:37
  • Nice link - a lot of good information there to digest. – Marty Fried Jun 18 '12 at 15:56
  • That didn't work for me, but nm-tool does. – hookenz Apr 08 '14 at 00:58
  • Are you using 12.04, the version specified? Did you substitute something like "eth0" for ? (My original post actually made it clearer, but someone changed it, and I added the note below). It still works for me, every day. – Marty Fried Apr 09 '14 at 00:02
  • This doesn't work for me. With a vpnc VPN connected, nmcli dev list iface eth0 shows the original DNS server, but the VPN's DNS server is not listed, even if I use tun0 for <interfacename>. So it might work some of the time but is not reliable in every case. – Peter V. Mørch May 21 '14 at 07:06
  • @MartyFried Your answer seems to be stuck at 42 votes :) – Felix Rabe Sep 23 '14 at 21:44
  • It would be great to figure out a way to do this without network-manager, which would be preferable for servers. Actually, as suggested by one of the below answers, it looks like cat /etc/resolv.conf does exactly that. I have the DNS servers configured in /etc/network/interfaces and they do land in resolv.conf as expected. – sxc731 May 02 '15 at 13:33
  • Seems nmcli has changed its syntax. Command nmcli dev list iface no longer works. I have written an answer to similar question for Ubuntu 14.04 and later: https://askubuntu.com/questions/637893/how-to-know-what-dns-am-i-using-in-ubuntu-14-04 – Koala Yeung Jun 18 '15 at 05:16
  • It still works for me using 14.04. You do need to add the device at the end, of course (ie, eth0, eth1, or whatever device you are using). – Marty Fried Jun 19 '15 at 14:37
  • By the way, nmcli dev show doesn't work for 14.04. That's the version I have, so I can't speak for future versions – Marty Fried Jun 19 '15 at 14:43
  • 6
    I'm using 15.04 and 'nmcli dev show |grep DNS' works for me instead. – flickerfly Aug 11 '15 at 18:37
  • ffs. i think you just made me install Gnome on my server. – chovy Jan 14 '16 at 06:38
  • 16.04: The program 'nmcli' is currently not installed. – vcardillo May 22 '17 at 23:01
  • 2
    @vcardillo: the original question stated: "I am using Ubuntu 12.04 LTS". It's been 5 years since I posted my answer. Nothing lasts forever. – Marty Fried May 22 '17 at 23:17
  • 1
    I would like to warn readers that the information provided by nmcli and nm-tool might not be correct. In testing my router setup, I changed the DHCP server to configure a DNS. On my client (linux mint 17.3) I did sudo dhclient -r; sudo dhclient to renew IP configuration. At this point both commands I mentioned showed the old DNS, not the new one. cat /etc/resolv.conf did work for me, but according to op it did not work for him. The only reliable way to figure out which DNS is used appears to be to a lookup with for example dig, but I doubt dig will show you all configured DNS. – Erik Lievaart Sep 11 '19 at 21:32
  • 1
    On Ubuntu 20, running nmcli device show eth0 shows Error: Could not create NMClient object: Could not connect: No such file or directory. (my network interface is eth0) – Andrew Koster Jul 27 '21 at 01:36
  • Doesn't work, even on on an 22.04 Server. – OttoEisen Jul 23 '22 at 23:28
  • 1
    Works on Kubuntu 22.04, with nmcli device status first to discover the device name – Alex Potapenko Nov 25 '22 at 18:13
  • nmcli will not work on any Ubuntu SERVER version. Ubuntu Server uses netplan. These instructions are only for Ubuntu Desktop out of the box. For Ubuntu server you need to cat /etc/netplan/00-installer-config.yaml, or check with systemd-resolve --status at the very end of the output. OP should update their post to say this is only for ubuntu desktop. – Dave Jan 11 '23 at 19:04
  • @AlexPotapenko I was about to post the same thing about 20.04. Also, once we get to "nmcli dev show " then I'm guessing the IP4 address after "IP4.DNS[1]" is the DNS I'm using? – Mr. Nichan Apr 02 '23 at 19:44
  • My only problem is that I think what comes after "IP4.DNS[1]" is the same as after "IP4.GATEWAY" (1st 3 bytes are my LAN/intranet address, and last byte is 1), so I don't think it's the true DNS, which I guess my computer lets the the modem or router or something determine. – Mr. Nichan Apr 02 '23 at 19:48
  • That seems like a whole different problem/potential question, though. – Mr. Nichan Apr 02 '23 at 19:53
216

In Ubuntu 18.04 and 20.04 you can use systemd-resolve --status. In newer versions use resolvectl status.

PLA
  • 2,782
88

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
JamesThomasMoon
  • 273
  • 2
  • 14
tgm4883
  • 7,912
78

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
39

cat /etc/resolv.conf should show your DNS servers.

You may not modify the resolv.conf directly with Ubuntu 12.04. If you need to change them though, you can add new DNS servers in your /etc/network/interfaces file by adding the following:

 dns-nameservers x.x.x.x x.x.x.x

where x is the DNS servers you wish to use.

If I were you, I would uninstall network-manager. In my opinion it's a pile of crap.

You can accomplish everything you need to do manually without worrying about changing your settings, especially if you have multiple NICs on the computer.

Adam
  • 862
ssc
  • 407
  • 4
  • 2
16

In Ubuntu 20.04, if your resolve.conf points to loopback:

$ sudo cat /run/systemd/resolve/resolv.conf
nameserver 127.0.0.53
options edns0 trust-ad

and:

$ ss -plnt | grep ':53'
LISTEN   0        4096            127.0.0.53%lo:53               0.0.0.0:*    

you can use resolvectl to show status for interfaces:

$ resolvectl status

Global LLMNR setting: no
MulticastDNS setting: no
DNSOverTLS setting: no
DNSSEC setting: no
DNSSEC supported: no
DNSSEC NTA: 10.in-addr.arpa ....

Link 4 (wlo1) Current Scopes: DNS
DefaultRoute setting: yes
LLMNR setting: yes
MulticastDNS setting: no
DNSOverTLS setting: no
DNSSEC setting: no
DNSSEC supported: no
Current DNS Server: 10.128.128.128 DNS Servers: 10.128.128.128 DNS Domain: ~.

Zanna
  • 70,465
Marek-A-
  • 275
  • "resolvectl status" does the trick, even on 22.04. Also: why is this not the accepted / highest ranking answer? It's so far down the list that I got my server-specific (no nm* tools) shut down. – OttoEisen Jul 23 '22 at 23:22
13

nmcli version 0.9.10

You can use either of these commands:

nmcli -t -f IP4.DNS device show eth0
IP4.DNS[1]:192.168.1.1
IP4.DNS[2]:8.8.8.8

nmcli -t -f IP4.DNS connection show conn-name
IP4.DNS[1]:192.168.1.1
IP4.DNS[2]:8.8.8.8
iman
  • 875
  • 9
  • 15
5

Amazing how many ways there are to do it. On an Ubuntu Server 18.04, if you don't want to install anything extra like nm-tool, then systemd-resolve --status will work out of the box for DNS information.

If you're interested getting not only your DNS servers, but also default gateway, IP address, network mask, etc, then netplan ip leases eth0 will give you all that information in an easy-to-read form (assuming you're interested in the eth0 interface).

Phil
  • 301
4

Since Ubuntu 20.04, you can use resolvectl which has been already mentioned. But I would like to highlight the dns which is a little bit more clean compared to status, see

resolvectl dns

$ resolvectl dns
Global:
Link 2 (ens34): 8.8.8.8
Link 3 (docker0):

resolvectl [status]

$ resolvectl 
Global
       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub

Link 2 (ens34) Current Scopes: DNS Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported Current DNS Server: 8.8.8.8 DNS Servers: 8.8.8.8 DNS Domain: company.com

Link 3 (docker0) Current Scopes: none Protocols: -DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported

Murmel
  • 295
3

On Ubuntu 20.04

systemd-resolve --status | grep -B 9 -A 6 "Current DNS Server"

gave me a clear result as to what DNS was in use for each adapter using DNS.

To reconfigure DNS

man systemd-resolved

reveals the DNS servers contacted are determined by global settings in /etc/systemd/resolved.conf.

Sudo edit that file and uncomment and set DNS= and FallbackDNS= to the IPv4 DNS servers you want. For example, DNS=8.8.8.8 and FallbackDNS=8.8.4.4 would use Google Public DNS. Restart the machine to apply the change.

@PLA, thank you for this answer which led me down this path.

3

In Ubuntu 15.10 you can get DNS

nmcli device show <interface name> 
2

On systems where systemd-resolved is NOT installed :

$ host -v something.unknown | awk -F "[ #]" '/Received /{print$5}' | uniq
192.168.1.1

On systems where NetworkManager is running :

$ ( nmcli -f IP4.DNS,IP6.DNS dev list || nmcli -f IP4.DNS,IP6.DNS dev show ) 2>/dev/null | awk '/DNS/{print$NF}'
192.168.1.1
SebMa
  • 2,291
0

Many of the posts here have two problems:

  1. They provide only an answer for Ubuntu Desktop systems that employ NetworkManger out of the box
  2. Provide false information of checking /etc/resolv.conf

First of all, Stop checking or editing /etc/resolv.conf on current Ubuntu systems. Ubuntu is deeply embedded in systemd. This means ntp, hostnames, services, and DNS are being supported by systemd services. You will noticed that /etc/resolv.conf will only have one address 127.0.0.53 which is yourself, mainly talking to systemd dns services. Dont change this file, stop looking at it, stop messing with it.

How to check Ubuntu Desktop DNS settings and status:

  • As @iman pointed out You can check settings from nmcli:
ip -br a 
# Make note of interface name
nmcli -t -f IP4.DNS device show $interface_name

How to check Ubuntu SERVER DNS settings and status:

  • You can check settings from the netplan conf file
cat /etc/netplan/00-installer-config.yaml

How to check status of DNS on Ubuntu Desktop and Ubuntu Server:

  • You can check status of DNS with this command
systemd-resolve --status --no-pager | grep "DNS Servers" -A 2

Note the above is only to list the number of dns servers if there are only two. If you want all of the DNS servers (a variable ammount) use this

systemd-resolve --status | awk '{ for(i=1;i<=NF;i++) if($i~/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) print $i }'
Dave
  • 193
0

Seems to be managed by network manager. Have a look here http://manpages.ubuntu.com/manpages/precise/man5/NetworkManager.conf.5.html

for a large explanation.

Or the short version it to look in

 /etc/NetworkManager/NetworkManager.conf

or

 <SYSCONFDIR>/NetworkManager/NetworkManager.conf
mbs
  • 672