Saturday, January 29, 2011

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

    Ivan : Great, thanks! In case anybody looks for this, the IPCop file is found on /var/ipcop/dhcpc/dhcpcd-*.info
    bortzmeyer : This is specific of one DHCP client but there are others (such as pump).
    From KPWINC
  • 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)

    KPWINC : I was thinking the same thing originally, but I don't that that would show ALL of the assigned DNS servers, would it? I think dig only returns the one it used for that particular lookup. On second look, the poster didn't specify so if he's happy with it then your answer is definitely the easiest. :-)
    Marcus Spiegel : yes you are right output only shows up the dns used by that query... so you can see what __is__ really happening. I found your solution fine to figure out what __should__ happen, thy for that :-)
    Ivan : That wasn't exactly what I was looking for, but thanks anyway.
    Ivan : When no DNS server responds, dig doesn't show any useful info, is it possible to make it spit out the DNS servers it tried to ask?
    Ivan : So after RingTFM, I see it just uses whatever is on /etc/resolv.conf
  • Read /etc/resolv.conf. That's all.

    man resolv.conf is useful, too.

    warren : except he said what to do if it's *not* there..
    bortzmeyer : Yes, but I ignored this point, which makes no sense. Since the libc resolver uses resolv.conf, it has to be there.
    Aizaz : Guyz tell me please how to get list of leased IP by LINUX DHCP server any flavor? without running any kind of script on Server rather accessing it through C#. Like in case of Microsoft DHCP server.
    From bortzmeyer
  • If there is nothing in /etc/resolv.conf, DNS resolution (that is, resolution using dns as defined in /etc/nsswitch.conf) will not work. This is what dig, host and the libc routines use to do DNS resolution. If there is nothing specified in this file, DNS resolution will not work (though host resolution may work via other means, if another host resolution method is configured, such as NIS (this is unlikely)).

    From BrianEss

0 comments:

Post a Comment