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 220.127.116.11 18.104.22.168 + 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 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
5.2 Check that the correct ip address has been set:
ifconfig eth0 | grep 'inet addr'
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
5.3 Check that dns lookup works:
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 (22.214.171.124) 56(84) bytes of data.
64 bytes from ping.sunet.se (126.96.36.199): icmp_req=1 ttl=249 time=7.94 ms
64 bytes from ping.sunet.se (188.8.131.52): icmp_req=2 ttl=249 time=6.94 ms
64 bytes from ping.sunet.se (184.108.40.206): icmp_req=3 ttl=249 time=8.14 ms
64 bytes from ping.sunet.se (220.127.116.11): icmp_req=4 ttl=249 time=6.99 ms