Find out the DNS server for a DHCP connection in Linux

Solution 1:

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

Solution 2:

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.


Solution 3:

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


Solution 4:

well, why not simply dig that?

marcus@marcus ~ $ dig google.de

; <<>> DiG 9.4.3-P3 <<>> google.de
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27579
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 6, ADDITIONAL: 0

;; QUESTION SECTION:
;google.de.         IN  A

;; ANSWER SECTION:
google.de.      1785    IN  A   209.85.229.104
google.de.      1785    IN  A   216.239.59.104
google.de.      1785    IN  A   74.125.77.104

;; AUTHORITY SECTION:
de.         23431   IN  NS  L.DE.NET.
de.         23431   IN  NS  Z.NIC.de.
de.         23431   IN  NS  F.NIC.de.
de.         23431   IN  NS  C.DE.NET.
de.         23431   IN  NS  S.DE.NET.
de.         23431   IN  NS  A.NIC.de.

;; Query time: 1 msec
;; SERVER: 192.168.1.15#53(192.168.1.15)
;; WHEN: Thu Sep 17 19:33:39 2009
;; MSG SIZE  rcvd: 181

look at the last lines: ;; SERVER: 192.168.1.15#53(192.168.1.15)