Ubuntu – How to Configure DNS Servers Manually via Bash

dhcpdnsnetworkingUbuntu

  • I still want to use DHCP to obtain IP4 and IP6 addresses.
  • DHCP delivers DNS servers
  • I want that the DNS servers from DHCP are ignored and two servers I specify manually are used instead.
  • Must be done on a headless server (no GUI) via Bash.
  • ubuntu-14.04.2-server-amd64, standard minimal installation + sshd

How is this configured correctly?

How to verify that the configuration works as expected?

/etc/network/interfaces is:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto em1
iface em1 inet dhcp

resolvconf is installed.

Best Answer

Add the line

supersede domain-name-servers 8.8.8.8, 8.8.4.4;

to the DHCP client configuration file /etc/dhcp/dhclient.conf.

To verify get the names of your network interfaces with ifconfig, shut down the interface(s) with ifdown ifname (e.g. ifdown eth0), restart it with ifup ifname (e.g. ifup eth0). After that (or after a reboot) /etc/resolv.conf should contain the two lines

nameserver 8.8.8.8
nameserver 8.8.4.4

Thanks to Big Chris.

Related Question