How to configure a static IP address, but DNS via DHCP

I've configured a static IP address in file /etc/network/interfaces:

auto eth0 iface eth0 inet static address 10.152.187.122 gateway 10.152.187.1 netmask 255.255.255.0 

But I'd like DNS to be configured automatically with DHCP. For example, if I run dhclient, it populates /etc/resolv.conf properly. But I would like this to run at startup, and I'm not sure where the proper way to set that up.

This is on the Ubuntu 12.04 (Precise Pangolin) cloud image.

1

3 Answers

With DHCP, you get the DNS nameserver addresses that the router gets when it gets an IP address from your internet service provider (ISP). You can use the DNS nameserver addresses the router has like this:

auto eth0 iface eth0 inet static address 10.152.187.122 netmask 255.255.255.0 gateway 10.152.187.1 dns-nameservers 10.152.187.1 
5

What you're asking for is essentially a hybrid configuration of static IP but DHCP DNS. There is no such type of network setup - you either have static, or DHCP.

You can explicitly define DNS nameservers, though, in your configuration, like so. This keeps static addresses, but unfortunately will not set DNS dynamically (which isn't really possible in a hybrid setup like this one).

auto eth0 iface eth0 inet static address 10.152.187.122 netmask 255.255.255.0 gateway 10.152.187.1 dns-nameservers 10.152.187.1 dns-nameservers 8.8.8.8 dns-nameservers 8.8.4.4 ... 

TL;DR: There's no way to get DHCP DNS, Static IP, unless you have a static entry set in the DHCP assignments table at the router - which guarantees that you'll always get that IP because it's specifically already reserved for that specific system/MAC address

I'm also interested in querying DNS servers from DHCP without using other information.

Dhcpcd might allow to query it (dhcpcd -o domain_name_servers -T), but I couldn't find anything for dhclient.

Besides using a modified dhclient (see here on GitHub and here on Stack Overflow) or a test-client (as in this blog) or different scripts (see this on Stack Overflow) I found a powershell solution here on indented.co.uk.

This made me think of using "the basics" on Linux and I ended up with a crafted dhcp-request using printf.

Some fields may be bogus (like mac aa:bb:cc:dd:ee:ff) but it contains options 53=0x35 (len 1 / value 1 = Request) and 55=0x37 (len 1 / value 6 for DNS).

For broadcasting socat is used:

printf "\x1\x1\x6\x0\xd3\x44\x33\xeb\x0\x0\x80\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\xaa\xbb\xcc\xdd\xee\xff\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x63\x82\x53\x63\x35\x01\x01\x37\x01\x06\xff" | socat - UDP4-DATAGRAM:255.255.255.255:67,broadcast 

Simliar socat is used for receiving an reply

socat -u udp-recv:68,reuseaddr - 

or

socat -u udp-recv:68 - | dd bs=1 skip=$( printf "%d" 0xec ) 

Using xxd I found what I was looking for...

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like