Tag Archives: dns nameservers

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
+        netmask
+        gateway
+        network
+        broadcast
+        dns-nameservers
+        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)
search home.lan

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

inet addr: Bcast: Mask:

5.3 Check that dns lookup works:
nslookup lani.nu


Non-authoritative answer:
Name: lani.nu
Name: lani.nu

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

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

Done 🙂