June 27th 2014

To ensure that a machine on a local network has the same ip after it is rebooted, you can set up a static ip on ubuntu. However, for Ubuntu 14.04 there is a lot of misinformation on the internet, so I'm writing this down in the hopes that somebody might benefit from it.

To set up the static IP, all you have to do is edit /etc/network/interfaces. When using dhcp (automatical allocation of ip) your interfaces file will usually look like:

# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet dhcp

However, eth0 might be p1p1 or something else. We'll call it <device>. To change the interfaces file to use a static ip, first we need know your dns server, network, network gateway, broadcast address, and netmask.

If you have network manager on a machine on the same network you can find the DNS server and gateway by running nm-tool and looking at the nicely formatted output. You can find your netmask and broadcast ip by typing ifconfig <device>, (in my case ifconfig eth0) where the broadcast address is under 'Bcast' and the netmask is under 'Mask'. Finally you can find the network (and the network gateway) by typing route -n. Network is under 'Destination'.

Now edit <device> part your interfaces file (/etc/network/interfaces) to say:

# The primary network interface
auto <device>
iface <device> inet static
    address <new ip>
    netmask <netmask>
    network <network>
    broadcast <broadcast address>
    gateway <gateway>
    dns-nameservers <dns server 1> <optional dns server 2>

In my case it looks like this:

auto eth0
iface eth0 inet static

You do not need to edit /etc/resolv.conf contrary to what many guides will tell you. This file is automatically generated based on the information for your device as well as the files in /etc/resolvconf/resolv.conf.d/. If you add name servers to the device in the interfaces file, then you should be set.

Finally to test that the changes, you need to restart the network on your machine. You can do this by typing:

sudo service networking stop
sudo service networking start

For some reason (and contrary to what a lot of guides will tell you), sudo service networking restart does not work in 14.04 for some reason.

After restarting you can test by pinging google: ping www.google.com.