Configuring DNS when DHCP is Used on Ubuntu

Photo by panumas nikhomkhai on Pexels.com

When eth0 is configured to use DHCP on Ubuntu (14.04 LTS), the contents of /etc/resolv.conf are overwritten by resolvconf (man 8 resolvconf), which in turn is called by dhclient. So you can neither set “dns-nameservers” and “dns-search” in /etc/resolv.conf nor /etc/network/interfaces.d/eth0.cfg.

The solution is to supersede the “domain-name-servers” and “domain-search” values in /etc/dhcp/dhclient.conf (man 5 dhclient.conf):

supersede domain-name-servers 172.16.101.11;
supersede domain-search "example.com";

And you may need to renew DHCP lease to make above change effective:

sudo dhclient -r eth0
sudo dhclient eth0