48

How to view the DNS address assigned by DHCP?

ifconfig can not show it.

$ ifconfig -a
eth0      Link encap:Ethernet  HWaddr 90:e6:ba:22:6a:f2  
          inet addr:192.168.1.111  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::92e6:baff:fe22:6af2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:224856 errors:0 dropped:0 overruns:0 frame:0
          TX packets:220040 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:166848627 (166.8 MB)  TX bytes:20256333 (20.2 MB)
          Interrupt:46 Base address:0x4000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:5889 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5889 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:468885 (468.8 KB)  TX bytes:468885 (468.8 KB)

wlan0     Link encap:Ethernet  HWaddr 00:26:5e:e8:4f:8e  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
10
  • What is returned by: ifconfig | grep 'inet addr:' ?
    – david6
    Sep 22, 2012 at 3:28
  • @david6 inet addr:192.168.1.111 Bcast:192.168.1.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.0
    – Victor S
    Sep 22, 2012 at 3:48
  • This should help you cat /var/lib/dhcp3/dhclient.leases | grep dhcp-server-identifier
    – devav2
    Sep 22, 2012 at 3:53
  • @devav2 $ cat /var/lib/dhcp/dhclient-529bed02-363e-4e97-bd5d-9f63f42f17f2-eth0.lease |grep dhcp-server-identifier option dhcp-server-identifier 192.168.1.1; option dhcp-server-identifier 192.168.1.1; option dhcp-server-identifier 192.168.1.1; option dhcp-server-identifier 192.168.1.1; option dhcp-server-identifier 192.168.1.1;
    – Victor S
    Sep 22, 2012 at 4:01
  • 1
    What are asking for? The DHCP service assigns an IP address from a pool (or range). A DNS server returns IP addresses for a hostname, domain, or mailbox (MX-record). Your ifconfig output looks normal ..
    – david6
    Sep 22, 2012 at 4:10

11 Answers 11

56

For at least Ubuntu 15.10 onward (possibly also earlier versions) run in a terminal:

nmcli dev show eth0 | grep IP4

eth0 is the most common iface, but it could be eth1, eth2, etc.

For older versions use nmcli dev list iface eth0 | grep IP4.

9
  • 1
    Awesome answer! Appreciate your sharing! I got it.
    – Victor S
    Sep 22, 2012 at 4:42
  • Glad it helped. I found it when I wanted a way to get that info, then parse it to display on demand, as i had 3 different possible DNS servers (ISP, Google, and OpenDNS). Sep 22, 2012 at 15:40
  • 8
    As of 15.04, the command is nmcli dev show Jun 18, 2015 at 22:06
  • 3
    With nmcli dev show eth0 (as on 15.04) it also works on Debian Buster. Thanks!
    – Luc
    Jul 31, 2017 at 22:47
  • 1
    @YandryPozo Posted an answer, as requested :) Sep 17, 2017 at 21:19
19

Now that Ubuntu (and Debian and almost all distros) use systemd (check with file /sbin/init) maybe the more elegant way to get this info is with:

systemd-resolve --status
# Or 
resolvectl dns
  • freedesktop.org docs
  • Arch Wiki: Use systemd-resolve --status where it says resolvectl (systemd-resolve is a symlink to resolvectl).
  • If you have problems with DNS subsystem I suggest removing resolvconf package. May conflicts with systemd-resolve.
4
  • 1
    The perfect answer for systems not running NetworkManager. Ubuntu 18.04, Ubuntu 19.10 Sep 21, 2019 at 21:10
  • It works in Raspbian too
    – riofly
    Nov 12, 2019 at 9:19
  • 2
    Doesn't work in Buster. I get Failed to get global data: Unit dbus-org.freedesktop.resolve1.service not found.
    – Aenfa
    Jun 15, 2022 at 20:56
  • @Aenfa Debian is out of scope, but maybe this helps Jun 15, 2022 at 21:16
14
$ nm-tool 

will also list DNS Servers.

1
  • 1
    +1 because this has been tested working up to 14.04 release. For 15.04 and newer release, use nmcli as suggested by the other answer instead.
    – user37165
    Dec 4, 2015 at 4:12
9

here it is:

cat /etc/resolv.conf

but do not edit this file

3
  • 2
    In recent Ubuntu releases (post 2012 I think) it won't be useful, since it will contain nameserver 127.0.1.1, referring to localhost dnsmasq installation. It's better to query NetworkManager as written by @anwar-shah and @marty-fried
    – gerlos
    Jan 14, 2016 at 10:44
  • 2
    As you mention, that file can be edited and thus are not necessarily the addresses assigned by DHCP. Now if you edit it yourself then, obviously, it's going to be different -- but what if you use a company VPN? Or something else influences the file? That's why I think it's not a good answer, even if simple and universal. It's a good first place to look, but not a true answer by itself.
    – Luc
    Jul 31, 2017 at 22:48
  • In Ubuntu 22.04 the command grep nameserver /etc/resolv.conf outputs nameserver 127.0.0.53 also if the DNS server are correctly set.
    – User051209
    Jul 21, 2023 at 9:37
9

To get the DNS address open terminal (Ctrl+Alt+T) and type:

cat /var/lib/dhcp/dhclient.leases | grep dhcp-server-identifier
4
  • 3
    There is nothing in the /var/lib/dhcp/dhclient.leases, it is empty.
    – Victor S
    Sep 22, 2012 at 5:36
  • $ file /var/lib/dhcp/dhclient.leases /var/lib/dhcp/dhclient.leases: empty
    – Victor S
    Sep 22, 2012 at 5:38
  • try /var/lib/dhcp/dhclient.eth0.leases
    – frag
    Jan 24, 2015 at 20:15
  • 3
    That a useless use of cat. You probably mean grep domain-name-servers /var/lib/dhcp/dhclient.leases (domain-name, no dhcp-server). Sep 18, 2019 at 7:09
4

As a GUI alternative to @MartyFried's answer, try this.

  1. Click on the Network connectivity icon on the top panel.

  2. Select Wi-Fi Settings

  3. It should show you the DNS server address

    screenshot

3
  • 4
    My Ubuntu has no any GUI desktop.
    – Victor S
    Sep 22, 2012 at 4:44
  • @VictorS it shows the same information as the other answer. I checked it
    – Anwar
    Sep 22, 2012 at 4:46
  • 1
    Thanks for your answer, it is convenient for gnome or KDE users.
    – Victor S
    Sep 22, 2012 at 4:51
2

In Ubuntu 16.04, the info from dhcp is written in: /var/lib/NetworkManager/dhclient-<interface>.conf

0
2

As I mentioned in the comments (which by popular request has become an answer now), one can use nmcli dev show as of Ubuntu 15.04. Add grep to the mix and you're set:

$ nmcli dev show | grep 'DNS'                                                                                                                                                                     
IP4.DNS[1]:                  208.67.222.222
IP4.DNS[2]:                  208.67.220.220
2

My actual solution(est 2015, Ubuntu 14.04), is this:

  1. Start the CLI
  2. Type: sudo apt-get install gnome-system-tools
  3. After install succeeded, type at the terminal: network-admin

If you somehow can't do this then try this, though it's not good enough for me, but it got my job done:

  1. type dig google.com
  2. near the end you can see something like this(est. 2015) ;; SERVER: and then an IP address(at least, on my machine) THAT is the actual DNS server resolving the domain for you. That server can change per hostname though.
1

The below worked for me on 20.04 LTS and 22.04 LTS:

resolvectl status

It will show the DHCP advertised and current DNS servers for each interface. Any manually configured DNS servers will also show up.
You can see the DNS domain too (which I was looking for when I have found this thread).

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

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

Link 3 (wlp4s0)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 192.168.0.67
       DNS Servers: 192.168.0.67 192.168.0.66
        DNS Domain: home

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

If your computer run behind of Router/WiFi of your Internet Provider, you'll get a Private IP Address from DHCP of Router, something like 192.168.0.2 or 192.168.1.100.

To get the Public IP Address of DHCP of DNS from your Internet Provider, you need run the follow command line on terminal:

dig +short myip.opendns.com @resolver1.opendns.com

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .