Change to static ip on the Ubuntu 12.04 (Precise Pangolin) server

This is an updated guide for Ubuntu 12.04 Server on how to set a static ip, as the approach has changed a bit since my previous guide for Ubuntu 8.04.

1 Setup a static ip address
1.1: Edit /etc/network/interfaces:
sudo nano /etc/network/interfaces

1.2: Change from dhcp to static:
Note that the changes below are edits, not the complete file. We change the word on the first line from dhcp to static. The rest of the file should be kept intact with the loopback interface settings.

- iface eth0 inet dhcp
+ iface eth0 inet static
+        address 192.168.0.2
+        netmask 255.255.255.0
+        gateway 192.168.0.1
+        network 192.168.0.0
+        broadcast 192.168.0.255
+        dns-nameservers 208.67.222.222 208.67.220.220
+        dns-search home.lan
+        dns-domain home.lan

2: Remove old configuration files used to generate resolv.conf:
The file resolv.conf should no longer be edited by hand. It is updated by the resolvconf script. To prevent resolvconf to still generate our resolv.conf file with our old dhcp settings we have to delete these two files:
sudo rm /run/resolvconf/interface/eth0.dhclient
sudo rm /run/resolvconf/interface/original.resolvconf

3: Uninstall the dhcp client (otherwise it will overwrite our changes on the next renew cycle):
sudo apt-get remove isc-dhcp-client

4: Restart the network to use the new settings:
The command “networking restart” has been deprecated. We can instead bring the interface down with ifdown and back up again with ifup to reload the settings. If we do it over an ssh connection we will lose connectivity when we bring the interface down. To solve this problem we can chain the two commands together. But doing so will prevent us from seeing the messages outputted from the commands, which can be useful in case something went wrong. We can then use the nohup command to direct the output to the file nohup.out:

sudo nohup sh -c "ifdown eth0 && ifup eth0"

The result can be check with the cat command:
sudo cat nohup.out

It should look something like this:
ssh stop/waiting
ssh start/running, process 2889

5. Check that everything is working ok:
5.1 The resolv.conf should now only contain the dns settings that we provided for our interface (eth0), check with:
sudo cat /etc/resolv.conf

The result should look like this:
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 208.67.222.222
nameserver 208.67.220.220
search home.lan

5.2 Check that the correct ip address has been set:
ifconfig eth0 | grep 'inet addr'

Result:
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0

5.3 Check that dns lookup works:
nslookup lani.nu

Result:
Server: 208.67.222.222
Address: 208.67.222.222#53

Non-authoritative answer:
Name: lani.nu
Address: 194.9.94.85
Name: lani.nu
Address: 194.9.94.86

Note that address in the reply might have changed after this was written.

5.4 And if we have gotten this far we have most likely set our gateway and other network parameters correctly and should also be able to reach the internet:
ping ping.sunet.se

Result:
PING ping.sunet.se (192.36.125.18) 56(84) bytes of data.
64 bytes from ping.sunet.se (192.36.125.18): icmp_req=1 ttl=249 time=7.94 ms
64 bytes from ping.sunet.se (192.36.125.18): icmp_req=2 ttl=249 time=6.94 ms
64 bytes from ping.sunet.se (192.36.125.18): icmp_req=3 ttl=249 time=8.14 ms
64 bytes from ping.sunet.se (192.36.125.18): icmp_req=4 ttl=249 time=6.99 ms

Done 🙂

Advertisement

7 thoughts on “Change to static ip on the Ubuntu 12.04 (Precise Pangolin) server

    1. lani78 Post author

      Hi Tim,

      The static address set for the server, 192.168.0.2 in the example, is one decided by you. It has however to be an address that is available on your network (not taken by another device) and that matches the same C-net as your network. That means that the first three parts of the address must match the one of your gateway/router. In this example the address would have to begin with 192.168.0. The 192.168.x.x addresses are special addresses that are reserved for private networks not directly visible to the Internet.

      To find an available address I would prefer to log-in to your router and check the interval that are being assigned by the DHCP server. You would usually configure the DHCP server to assign addresses in the scope 192.168.0.100 – 192.168.0.200. You can then use ping to ping an address below 192.168.0.100 to see if it is free (note however that not all devices responds to ping and all devices on your network may not be turned on). You should then keep a list of all your statically assigned IP-addresses for future references.

      You can use the command “route -n” to find out more information about your current network. The line listed that has the flags UG shows your gateway address in the Gateway column. The line listed with flags U shows the netmask to use in the Genmask column and the network address to use in the Destination column.

      The one remaining is the broadcast address, it should match your C-net (192.168.0 in the example) and end with 255, which is the broadcast address.

      The DNS server can be found by the command “nm-tool | grep DNS”.

      I hope it helps,
      Lani

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.