Find out the DNS server for a DHCP connection in Linux

I know they're defined in /etc/resolv.conf, but what if it's not there? And more specifically, how do you find the DNS server returned by DHCP?

In GNOME you can use the NetworkManager applet to see the primary DNS for any connection, so how would you do the same from the command line?


Usually dhclient.leases file is located at /var/lib/dhcp3/dhclient.leases, type the following command:

less /var/lib/dhcp3/dhclient.leases

OR

cat /var/lib/dhcp3/dhclient.leases

OR

You can just use grep command to get DHCP server address, enter:

grep dhcp-server-identifier /var/lib/dhcp3/dhclient.leases

OR

dhclient eth0


I recently had this problem where my dhcpcd was misconfigured as was not setting DNS servers. I found out that I can query which nameservers are available by DHCP with the following command:

sudo dhcpcd -o domain_name_servers -T

The command will output a bunch of network connection information. Look for the line beginning with new_domain_name_servers.

From here I was able to manually set the nameservers.


One more solution to query the DHCP server:

sudo nmap --script broadcast-dhcp-discover

The output tells you the name server offered by the DHCP server:

Starting Nmap 7.60 ( https://nmap.org ) at 2019-12-12 23:50 UTC
Pre-scan script results:
| broadcast-dhcp-discover: 
|   Response 1 of 1: 
|     IP Offered: 144.123.211.44
|     DHCP Message Type: DHCPOFFER
|     Server Identifier: 144.123.99.200
|     IP Address Lease Time: 5m00s
|     Subnet Mask: 255.255.0.0
|     Router: 144.123.99.200
|     Domain Name Server: 144.123.99.200
|_    Domain Name: localdomain

I found my DHCP lease info at /var/lib/dhclient/dhclient-eth0.leases in case anyone can't find it at /var/lib/dhcp3/dhclient.leases