How to Set a Static IP and DNS in Ubuntu 14.04 or Later

How To Set A Static IP And DNS In Ubuntu 14.04 or Later

This article details how to set a static IP and DNS in Ubuntu: version 14.04 and later no longer allow direct editing of /etc/resolv.conf directly.

Using something like “echo nameserver 8.8.8.8 >> resolv.conf” will work temporarily but will be overwritten as soon as the  machine is rebooted or the networking service restarted.

You must now modify /etc/resolvconf/resolv.conf.d/head or /etc/resolvconf/resolv.conf.d/tail.

/etc/resolv.conf is written from:

  1. /etc/resolvconf/resolv.conf.d/head
  2. DNS set on interfaces
  3. /etc/resolvconf/resolv.conf.d/base
  4. /etc/resolvconf/resolv.conf.d/tail

While any and all of these work, it is much simpler to set this in the interface config aong with a static IP:

Edit /etc/network/interfaces and change the following:

Notes:

  • Your network card may be referred to as eth0, eth1, ens32 etc.
  • Change your dns-nameservers to whatever you want them to be – the above ones are google’s own.
  • The IP addressing must also be tailored to whatever your home router is configured with.
  • Leaving the dns-nameservers directive out will result in the DNS servers from your ISP or home router being used instead.
auto eth0
iface eth0 inet dhcp

to:

auto eth0 
  iface eth0 inet static
  address 192.168.0.50
  netmask 255.255.255.0
  gateway 192.168.0.1
  network 192.168.0.0
  broadcast 192.168.0.255
  dns-nameservers 8.8.8.8 8.8.4.4

Finally, restart your ethernet:

$ sudo service networking restart
$ sudo ifdown eth0
$ sudo ifup eth0