39

How can I tell which DNS servers DNSMasq is using to resolve a name to an IP address? I can't figure it out. All of the existing questions here seem to just go as far as telling users that they are using DNSMasq.

1
  • Add this to your config: log-facility=/var/log/mydnsmasq.log and then run a nslookup on your domain and tail the log file, you will see which server was used.
    – Shayan
    Oct 11 at 14:37
24

For a more generic answer when you are using dnsmasq without network manager and with several upstream DNS :

sh# kill -USR1 <PID OF DNSMASQ>
sh# tail /var/log/syslog

Upon receiving SIGUSR1, dnsmasq will log various statistics, including the number of DNS request send to each upstream server. Based on that, you can easily determine the most frequently used DNS server on your system.

3
  • 4
    Also, when any changes are made to the connection (e.g. connecting to VPN), dnsmasq logs these to /var/log/syslog: dnsmasq[xxx]: setting upstream servers from DBus dnsmasq[xxx]: using nameserver 10.xx.xx.xx#53(via tun0) dnsmasq[xxx]: using nameserver 192.xx.xx.x#53(via wlp2s0) Aug 28 '17 at 23:45
  • This is a better answer because it tells you which servers were actually used as opposed to just which servers are configured to be used. Sep 12 '17 at 16:20
  • Thanks, your answer gave me a hint to look into the logs, I used sudo journalctl -b -u dnsmasq | grep nameserver.
    – selurvedu
    Jan 12 '18 at 21:21
23

You can find the DNS servers with the NetworkManagerCLI (nmcli) tool:

nmcli dev show | grep DNS
IP4.DNS[1]:                             172.22.216.251
IP6.DNS[1]:                             2a01:4f0:400c:1::1

Ubuntu 14.04 and older

The command is slightly different in older (<15.04) versions of Ubuntu:

> nmcli dev list | grep DNS
IP4.DNS[1]:                             172.22.216.251
IP6.DNS[1]:                             2a01:4f0:400c:1::1
4
  • Don't you mean nmcli dev show?
    – einpoklum
    Jan 29 '16 at 17:34
  • @einpoklum: No. On my 14.04 box, nmcli dev show gives: Error: 'dev' command 'show' is not valid. Jan 29 '16 at 18:42
  • On my Lubuntu 15.10 I get the opposite.
    – einpoklum
    Jan 29 '16 at 18:42
  • Note that this answer assumes that dnsmasq is managed by NetworkManager. You can verify this in pstree: the dnsmasq process should be a child of NetworkManager. This is the case at least on Ubuntu 16.04 Desktop, but I'm not certain about older or newer versions, or the Server edition.
    – Thomas
    Feb 18 at 8:26
3

Generally, for the system using systemd, the DNS servers dnsmasq is using can be found via

journalctl -u dnsmasq

1
  • 2
    This does not work when dnsmasq is launched by NetwokrManager (and configuration is passed via dbus) Sep 27 '18 at 5:17
1

In package:

network-manager 0.9.8.8-0ubuntu7.2 amd64 

The command is:

root@D-MIS-001:~# nmcli dev list | grep DNS
IP4.DNS[1]:                             172.16.10.173
IP4.DNS[2]:                             172.16.10.133

This is what seems to be the current package version for Ubuntu 14.04 LTS. The correct command for 15.* may have changed.

0

On Ubuntu 15.04 the command is:

$ nmcli dev show | grep DNS
IP4.DNS[1]:                             8.8.8.8
0

It's also possible to ask NetworkManager by DBus directly. (This is what nmcli does under the hood). For example, on Ubuntu 14.04 or later, run (without the leading $):

$ env -i gdbus introspect -y -o /org/freedesktop/NetworkManager/IP4Config -d org.freedesktop.NetworkManager | perl -ne 'if (m@^  node (\d+) [{]$@) { $_ = readpipe("env -i gdbus introspect -y -o /org/freedesktop/NetworkManager/IP4Config/$1 -d org.freedesktop.NetworkManager\n"); while (m@ Nameservers = \[(?!\])((?=\d)[\d, ]*\d)\]@g) { for (split(m@, *@, $1)) { print join(".", unpack("C4", pack("I", $_))), "\n" } } }'
12.34.56.78
90.123.45.6

For a more indirect approach, use ActiveConnection instead of IP4Config.

0

I'm expanding the answers given by others, showing sligthly longer commands, which print only the DNS servers.

Ubuntu 14.04 has nmcli 0.9.8.8 in the network-manager package. The list of DNS servers is within the output of nmcli -f IP4 -t -m tabular dev list. To get only the list of DNS servers, run (without the leading $):

$ env -i nmcli -f IP4 -t -m tabular dev list | perl -ne 'chomp; my @l = split(m@:@, $_); if (@l > 3) { $_ = $l[3]; if (m@^\d+(?=[.])[\d. |]+[.]\d+$@) { for (split(m@ *[|] *@, $_)) { print "$_\n" } } }'
12.34.56.78
90.123.45.6

Ubuntu 15.04 or later also has nmcli in the network-manager package, but expects different command-line arguments. The list of DNS servers is within the output of nmcli -f IP4.DNS -t -m tabular dev show. To get only the list of DNS servers, run (without the leading $):

$ env -i nmcli -f IP4.DNS -t -m tabular dev show | perl -ne 'if (m@^\d+(?=[.])[\d. |]+[.]\d+$@) { chomp; for (split(m@ *[|] *@, $_)) { print "$_\n" } }'
12.34.56.78
90.123.45.6

Since nmcli connects to NetworkManager via DBus, the command above works for any connections managed by NetworkManager, even for those which don't use dnsmasq. However, not all connections are managed by NetworkManager. The list of DNS servers should be extracted from the file /etc/resolv.conf, and if it contains addresses starting with 127.0., NetworkManager should be consulted. Here is how to extract:

$ perl -ne 'print "$1\n" if m@^\s*nameserver\s+(\S+)\s*$@' /etc/resolv.conf 
127.0.1.1

Please note that 127.0.1.1 (and possibly others) indicates dnsmasq, and 127.0.0.53 indicates systemd-resolved.

If the current active connection is not managed by NetworkManager (and systemd-resolved), then the DNS servers will be enumerated in the file /etc/resolv.conf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.