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)
15 Answers
You should be able to get some reasonable information in:
$ cat /etc/resolv.conf - 47However, please be aware that (on modern Linuxen) the contents of
/etc/nsswitch.confdictate what name services are used (DNS, LDAP, etc) and in what order. Sayfgrep hosts: /etc/nsswitch.conf. If it only references DNS,/etc/resolv.confis 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– Alexios2012-01-12 13:35:31 +00:00Commented Jan 12, 2012 at 13:35 - 83This file typically points at 127.0.1.1 on Ubuntu - it's the local DNS cache server, not the actual upstream.Barry Kelly– Barry Kelly2016-03-08 10:24:21 +00:00Commented Mar 8, 2016 at 10:24
- 5And if you have several upstream server configured ? How to know which one is currently used ?Sylvain Leroux– Sylvain Leroux2016-11-24 23:31:00 +00:00Commented Nov 24, 2016 at 23:31
- 12See 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.53nealmcb– nealmcb2018-11-11 22:13:57 +00:00Commented Nov 11, 2018 at 22:13 - 7This solution is no more up to date. I find the
systemd-resolve --statussuggested by @G32RW most up-to-date solution for this problem.Georgi Stoyanov– Georgi Stoyanov2019-03-27 15:59:51 +00:00Commented Mar 27, 2019 at 15:59
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.
- 19This one is usefull if you are using VPN and NetworkManager. Your
/etc/resolv.confwill point to your machine, withdnsmasqresolving names as configured by NetworkManager.Grzegorz Żur– Grzegorz Żur2013-05-30 11:32:22 +00:00Commented May 30, 2013 at 11:32 -
nm-tool | grep DNS No command 'nm-tool' found, did you mean: Command 'dm-tool' from package 'lightdm' (main) nm-tool: command not foundchovy– chovy2016-08-31 07:22:52 +00:00Commented Aug 31, 2016 at 7:22 - 2This did not work for me on an Ubuntu VM.
nmcli dev showdid not have the DNS info. However,systemd-resolve --statusdid have the real DNS server. unix.stackexchange.com/a/434756wisbucky– wisbucky2018-11-14 02:22:25 +00:00Commented Nov 14, 2018 at 2:22 - 1To add, you can also supply an interface name, e.g.,
nmcli dev show eth1to show information about a specific adapter. Useful if you're troubleshooting a specific adapter that's connected differently than others.Michael Stum– Michael Stum2019-06-30 23:52:22 +00:00Commented Jun 30, 2019 at 23:52 - 1The question specifically indicates the use of Network Manager. If you're not using network manager, see other answers.Lonnie Best– Lonnie Best2021-11-18 13:46:56 +00:00Commented Nov 18, 2021 at 13:46
On systems running systemd use:
systemd-resolve --status Or:
resolvectl - 13systemd-resolve: unrecognized option '--status'Asclepius– Asclepius2018-04-01 18:03:05 +00:00Commented Apr 1, 2018 at 18:03
- 20Says
Failed to get global data: Unit dbus-org.freedesktop.resolve1.service not found.xji– xji2018-04-12 11:19:03 +00:00Commented Apr 12, 2018 at 11:19 - 16This is the new default way to do it in Ubuntu 18.04 Bionic Beaver - get used to it, everybody!Avery Freeman– Avery Freeman2018-04-24 20:32:18 +00:00Commented Apr 24, 2018 at 20:32
- 2What am I reading? I only see DNSSEC records.rosstex– rosstex2020-05-02 16:22:31 +00:00Commented May 2, 2020 at 16:22
- 1resolvctl worked for me in Ubuntu 22.04 . nmcli and systemd-resolve solutions did not work.Marnix A. van Ammers– Marnix A. van Ammers2022-11-25 17:28:05 +00:00Commented Nov 25, 2022 at 17:28
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
- 14On Debian this requires the
dnsutilspackage.Faheem Mitha– Faheem Mitha2012-01-14 20:54:43 +00:00Commented Jan 14, 2012 at 20:54 - 9If you use any DNS masking/caching service that is run on your local machine, it will hide the real DNS servers.karatedog– karatedog2015-09-07 09:12:41 +00:00Commented Sep 7, 2015 at 9:12
- 21Ubuntu 18.04 just shows the local dns cache:
SERVER: 127.0.0.53#53(127.0.0.53)wisbucky– wisbucky2018-11-14 02:16:04 +00:00Commented Nov 14, 2018 at 2:16 - 2This only tells you which server was used for that query. It doesn't tell you all the DNS servers that your host might use.Marnix A. van Ammers– Marnix A. van Ammers2021-11-28 01:14:06 +00:00Commented Nov 28, 2021 at 1:14
- 1Beware that this obviously fails if the DNS server is not available. That's not a common thing, but I've managed to get a bad result or timeout when looking for a DNS server running on IPv6 (using
dig -6).Maarten Bodewes– Maarten Bodewes2022-10-27 13:03:18 +00:00Commented Oct 27, 2022 at 13:03
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 - 9On Debian this requires the
dnsutilspackage.Gayan Weerakutti– Gayan Weerakutti2017-08-13 07:00:57 +00:00Commented Aug 13, 2017 at 7:00 - 24On a recent Ubuntu, this again points to the local cache server 127.0.0.1 as already hinted at in this commentFriendFX– FriendFX2017-12-01 01:50:54 +00:00Commented Dec 1, 2017 at 1:50
- In CentOS 7 it quits with error, but it is a vm so I did
nslookup google.comin the Windows host and I found the nameserver. Add it in/etc/resolv.conflike:nameserver xx.xx.xx.xxand restart servicenetwork, and all is fine. Praise you.WesternGun– WesternGun2018-05-03 15:22:29 +00:00Commented May 3, 2018 at 15:22 - Note that Nslookup is deprecatedDaniel– Daniel2020-03-13 01:22:44 +00:00Commented Mar 13, 2020 at 1:22
- 1Didn't work: Server: 127.0.0.53, Address: 127.0.0.53#53Warren Parad– Warren Parad2021-05-26 19:38:20 +00:00Commented May 26, 2021 at 19:38
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 - My results:
order «con» «show» is not valid.Sopalajo de Arrierez– Sopalajo de Arrierez2016-01-30 22:03:20 +00:00Commented Jan 30, 2016 at 22:03 - It works fine for me with network-manager 1.0.4 on Ubuntu 15.10. Maybe you have an older version?Sameer– Sameer2016-03-17 05:40:13 +00:00Commented Mar 17, 2016 at 5:40
- The tabular format is pretty bad. I hope to get a column like format similar to Powershell.CMCDragonkai– CMCDragonkai2017-03-14 06:42:53 +00:00Commented Mar 14, 2017 at 6:42
- 4Returns
Error: invalid field 'ip4.dns'; allowed fields: NAME,UUID,TYPE,TIMESTAMP,TIMESTAMP-REAL,AUTOCONNECT,AUTOCONNECT-PRIORITY,READONLY,DBUS-PATH,ACTIVE,DEVICE,STATE,ACTIVE-PATH.FriendFX– FriendFX2017-12-01 01:53:23 +00:00Commented Dec 1, 2017 at 1:53 - Ugh, as long as
nmclidoesn't keep backwards compatibility with specific parameters and output format, I guess this is not a good way of testing for anything.Maarten Bodewes– Maarten Bodewes2022-10-27 13:06:59 +00:00Commented Oct 27, 2022 at 13:06
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
grepoption:-C NUM,-NUM,--context=NUMPrint 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 - 1works perfect on Ubuntu 18.10.Georgi Stoyanov– Georgi Stoyanov2019-03-27 16:02:13 +00:00Commented Mar 27, 2019 at 16:02
- see also askubuntu.com/q/1149364/157358 about the name of the programglS– glS2021-06-12 11:38:03 +00:00Commented Jun 12, 2021 at 11:38
- 1As with other answers, thiis will simply return the caching service on your local host.Maarten Bodewes– Maarten Bodewes2022-10-27 13:09:10 +00:00Commented Oct 27, 2022 at 13:09
to get the first DNS SERVER (IP only) :
cat /etc/resolv.conf|grep -im 1 '^nameserver' |cut -d ' ' -f2 catwill output DNS configgrepfilters only nameserver-igrep ignore case-m 1grep stop after first matchcuttake the ip part of the row (second column with ' ' as separator)
If you prefer sed:
cat /etc/resolv.conf | sed -rn "s/^nameserver ([.0-9]*).*$/\1/p" - Sed command: after nameserver return everything what is a number or a dot.
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) - 1To lighten the pipeline even more, capture groups with Perl regexp is very neat, and grep takes a file argument:
grep -Pom 1 '^nameserver \K\S+' /etc/resolv.conf. Just wrote up Capture groups with grep perl regular expressionsshow– sshow2017-07-28 09:04:14 +00:00Commented Jul 28, 2017 at 9:04 - There's both IPv4 and IPv6 DNS servers. Also, secondary servers exist for a reason. Which one this command returns? Is it advised to take the first one, and just ignore the others?André Werlang– André Werlang2020-01-16 02:53:57 +00:00Commented Jan 16, 2020 at 2:53
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.
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.
- perhaps alternativelyAmos Folarin– Amos Folarin2019-06-12 17:44:41 +00:00Commented Jun 12, 2019 at 17:44
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).
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
- Only returns local caching DNS, i.e. localhost.Maarten Bodewes– Maarten Bodewes2022-10-27 13:11:38 +00:00Commented Oct 27, 2022 at 13:11
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 Some dudes use tcpdump and are happy with the result:
tcpdump -i any port 53 - 1You may want to add some explanation on how to interpret the result, which is not completely trivial. Also, you should probably mention that this requires triggering the corresponding traffic in the first place. In addition, using that tool usually requires root privileges.AdminBee– AdminBee2024-10-18 12:56:55 +00:00Commented Oct 18, 2024 at 12:56