406

How can I check which DNS server am I using (in Linux)? I am using network manager and a wired connection to my university's LAN. (I am trying to find out why my domain doesn't get resolved)

Braiam
  • 35,991
Grzenio
  • 5,867
  • 1
    The Linux kernel itself is not aware about DNS services. To answer it's better to know your userland. What distribution are you using? Ubuntu? Debian GNU/Linux? Centos? – Valerio Bozz May 18 '21 at 07:49

14 Answers14

316

You should be able to get some reasonable information in:

$ cat /etc/resolv.conf 
  • 45
    However, please be aware that (on modern Linuxen) the contents of /etc/nsswitch.conf dictate what name services are used (DNS, LDAP, etc) and in what order. Say fgrep hosts: /etc/nsswitch.conf. If it only references DNS, /etc/resolv.conf is the right place to look for your nameservers. But chances are you're also using mDNS (aka ZeroConf, aka Avahi, aka Bonjour, etc), etc. In that case, things depend on what you're using. – Alexios Jan 12 '12 at 13:35
  • 74
    This file typically points at 127.0.1.1 on Ubuntu - it's the local DNS cache server, not the actual upstream. – Barry Kelly Mar 08 '16 at 10:24
  • 3
    @BarryKelly Check what your router uses, then – Geremia Mar 11 '16 at 17:10
  • 5
    And if you have several upstream server configured ? How to know which one is currently used ? – Sylvain Leroux Nov 24 '16 at 23:31
  • 4
    I would suggest to mention that file is a link and dynamically generated for systems using resovconf (like Ubuntu). I've seen this answer millions of times and until today is that I think it is correct, because I understand now that it is actually a dynamically generated file. – jgomo3 Dec 26 '17 at 18:00
  • 10
    See the answers by @G32RW or @Lonniebiz for a more robust approach under various circumstances, e.g. when you get an answer like 127.0.0.53 – nealmcb Nov 11 '18 at 22:13
  • 2
    @Geremia router? I'm on office wifi and I want to verify which DNS server I'm using. Or I'm on VPN and want to verify, similarly. Usually I don't have access to the router. – Barry Kelly Mar 20 '19 at 10:12
  • 7
    This solution is no more up to date. I find the systemd-resolve --status suggested by @G32RW most up-to-date solution for this problem. – Georgi Stoyanov Mar 27 '19 at 15:59
  • 1
    There is no "systemd-resolve" in my Ubuntu 22.04 . Also "nmcli device show | grep -i dns" returns nothing. However, "resolvectl --protocol=dns | grep 'DNS Servers'" seems to work. – Marnix A. van Ammers Nov 25 '22 at 17:15
  • I would suggest adding: cat /run/systemd/resolve/resolv.conf to your answer if you are running a caching resolver on 127.X.X.X and actually want to know what's behind the cache. Or, as others have answered, you can also run resolvectl, or just resolvectl dns – B. Shea Mar 26 '24 at 18:09
305

Here's how I do it:

( nmcli dev list || nmcli dev show ) 2>/dev/null | grep DNS

This worked previous to the way above:

nm-tool | grep DNS

On Debian, you need to have the network-manager package installed.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Lonnie Best
  • 5,123
  • 19
    This one is usefull if you are using VPN and NetworkManager. Your /etc/resolv.conf will point to your machine, with dnsmasq resolving names as configured by NetworkManager. – Grzegorz Żur May 30 '13 at 11:32
  • 7
    On Debian this requires the network-manager package. – Neurotransmitter Feb 03 '15 at 19:44
  • 3
    nm-tool is not available in newer linuxes. for example it is not in the 'network-manager' package of debian 8. – don bright Oct 31 '15 at 15:06
  • nm-tool | grep DNS No command 'nm-tool' found, did you mean: Command 'dm-tool' from package 'lightdm' (main) nm-tool: command not found – chovy Aug 31 '16 at 07:22
  • 2
    I've updated the answer to reflect what's working for me in 2016. – Lonnie Best Sep 01 '16 at 16:36
  • 2
    This did not work for me on an Ubuntu VM. nmcli dev show did not have the DNS info. However, systemd-resolve --status did have the real DNS server. https://unix.stackexchange.com/a/434756 – wisbucky Nov 14 '18 at 02:22
  • @wisbucky : That didn't work for me in 16.04, but people say its the new way in 18.04+. – Lonnie Best Nov 15 '18 at 00:29
  • 1
    To add, you can also supply an interface name, e.g., nmcli dev show eth1 to show information about a specific adapter. Useful if you're troubleshooting a specific adapter that's connected differently than others. – Michael Stum Jun 30 '19 at 23:52
  • 1
    nmcli is only installed for GUI distros, through network-manager package, not evailable in Ubuntu Server by default, for example – xCovelus Oct 03 '19 at 12:52
  • +1 for systemd-resolve --status as per https://unix.stackexchange.com/a/434756/36362 (as @wisbucky mentions above) on Ubuntu 18.04. – Johnny Utahh May 14 '20 at 18:38
  • What if I'm not using network manager? – Freeman May 18 '20 at 19:47
  • The question specifically indicates the use of Network Manager. If you're not using network manager, see other answers. – Lonnie Best Nov 18 '21 at 13:46
177

On systems running systemd use:

systemd-resolve --status

Or:

resolvectl
G32RW
  • 1,952
  • 1
  • 10
  • 5
111

I think you can also query DNS and it will show you what server returned the result. Try this:

dig yourserver.somedomain.xyz

And the response should tell you what server(s) returned the result. The output you're interested in will look something like this:

;; Query time: 91 msec
;; SERVER: 172.xxx.xxx.xxx#53(172.xxx.xxx.xxx)
;; WHEN: Tue Apr 02 09:03:41 EDT 2019
;; MSG SIZE  rcvd: 207

You can also tell dig to query a specific DNS server by using dig @server_ip

Freiheit
  • 9,669
85

Just do an, nslookup. Part of its results include the server that it's using.

In the example below, it shows that the DNS server used is at 8.8.8.8.

$ nslookup google.com
Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
Name:   google.com
Address: 172.217.22.174
Sam
  • 859
26

With the new network-manager command nmcli, do this:

nmcli --fields ipv4.dns,ipv6.dns con show [connection_name]

On newer versions of network-manager (such as in Ubuntu 16.04), the field names are slightly different:

nmcli --fields ip4.dns,ip6.dns con show [connection_name]

If you don't know the connection name, use:

nmcli -t --fields NAME con show --active

For example, on old versions of nmcli :

$ nmcli --fields ip4.dns,ip6.dns con show 'Wired connection 1'
IP4.DNS[1]:                             172.21.0.13
IP4.DNS[2]:                             172.21.0.4
Duncan Lock
  • 145
  • 7
Sameer
  • 421
14

to get the first DNS SERVER (IP only) :

cat /etc/resolv.conf|grep -im 1 '^nameserver' |cut -d ' ' -f2
  • cat will output DNS config
  • grep filters only nameserver
  • -i grep ignore case
  • -m 1 grep stop after first match
  • cut take the ip part of the row (second column with ' ' as separator)

To put DNS ip in an environment variable, you could use as follow:

export THEDNSSERVER=$(cat /etc/resolv.conf|grep -im 1 '^nameserver' |cut -d ' ' -f2)
boly38
  • 349
13

Using resolvectl

$ resolvectl status | grep -1 'DNS Server'
    DNSSEC supported: no
  Current DNS Server: 1.1.1.1
         DNS Servers: 1.1.1.1
                      1.0.0.1

grep option: -C NUM, -NUM, --context=NUM Print NUM lines of output context.

For compatibility, systemd-resolve is a symbolic link to resolvectl on many distros as for Ubuntu 18.10:

$ type -a systemd-resolve
systemd-resolve is /usr/bin/systemd-resolve

$ ll /usr/bin/systemd-resolve lrwxrwxrwx 1 root root 10 nov. 15 21:42 /usr/bin/systemd-resolve -> resolvectl

$ type -a resolvectl resolvectl is /usr/bin/resolvectl

$ file /usr/bin/resolvectl /usr/bin/resolvectl: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=09e488e849e3b988dd2ac93b024bbba18bb71814, stripped

oHo
  • 1,258
  • 1
  • 12
  • 22
9

If you are using network manager probably you get all network parameters from your dhcp server at your university.

If you don't want use your shell to check your dns settings (as described by hesse and Alexios), you can see them from the panel "Network information".

You can reach this panel by pressing right mouse button on network manager icon and selecting "Connection Information" from the menu.

6

I have Fedora 25 and also had similar slow response on command line to sudo commands.

nmcli dev show | grep DNS 

showed that only one of my 3 adapters (two active) had DNS entries. By adding DNS entries to the one active card that didn't have an entry - presto! All is good and response time is immediate.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
4

In Ubuntu >= 15

nmcli device show <interfacename> | grep IP4.DNS

Replace <interfacename> with yours.

In Ubuntu <= 14

The command

 nmcli dev list iface <interfacename> | grep IP4

Replace <interfacename> with yours.

Examples

 nmcli device show eth0 | grep IP4.DNS

Or

 nmcli dev list iface eth0 | grep IP4

This will list all DNS servers(If you use more than one).

Maythux
  • 1,888
3

On a recent Fedora 33, you can just use

# resolvectl dns
Global:
Link 2 (enp0s31f6):
Link 3 (wlp4s0): 1.1.1.1 8.8.8.8
Link 4 (virbr0):
Link 5 (virbr0-nic):

To check if systemd-resolved is active, do cat /etc/resolv.conf It will tell you who is controlling the file.

If you want more details, you can use resolvectl status resolvectl statistics, you can also flush caches ...

To troubleshoot, you can use journalctl -u systemd-resolved -f -o cat | grep Looking after setting the level to DEBUG: sudo resolvectl log-level debug

BrnVrn
  • 79
1

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,149
0

In CentOS, you can use:

/usr/sbin/named -v