At home I use Ubuntu Server 14.04 and have never had to set static IP as I would configure a static IP/mac address mapping in my router’s DHCP settings so it would always get the same address . That was until last week when I had to look up the exact steps on Google. Needless to say, the process is straight-forward, here are the steps to configure a static IP on Ubuntu Server 14.04:
1. The first thing to mention is that the interface config file in Ubuntu looks a little weird if you are used to working with only RHEL/CentOS. By default Ubuntu Server enables DHCP on a network interface. To configure the interface you have to edit the /etc/network/interfaces file. Below I am opening it with vi editor:
admin@web01:~$ sudo vi /etc/network/interfaces file
You will see something similar to the below:
This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet dhcp
2. As you can see from the above eth0 is set to use DHCP and not a static IP. I want to set a static IP with the following info:
|DNS Server 1||10.10.10.1|
|DNS Server 2||220.127.116.11|
To achieve this, change iface eth0 inet dhcp to iface eth0 inet static. Then add the network parameters per the below:
This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static address 10.10.10.50 netmask 255.255.255.0 gateway 10.10.10.1 dns-nameservers 10.10.10.1 18.104.22.168
Save the changes and exit.
3. The next step is to update the /etc/hosts file by opening it with vi or any other editor of your choice:
admin@web01:~$ sudo vi /etc/hosts
You should see the lines below:
127.0.0.1 localhost 127.0.1.1 web01
Add a new line under the existing lines containing the static IP and relevant hostname(s):
127.0.0.1 localhost 127.0.1.1 web01 10.10.10.50 web01
Save the changes and exit the file.
4. To commit the changes made in the steps above you can either reboot the server or restart the network services. Restart the network services by running:
admin@web01:~$ sudo ifdown eth0 & sudo ifup eth0
Please note that in previous version of Ubuntu server you could run service networking restart to restart services. This no longer works in Ubuntu Server 14.04. You need to shut down the interface and then bring it back up per step 4 above.
After restarting the network services the operating system and all applications will recognise and use the new IP.
Please also note that restarting the network services will force an update to the /etc/resolve.conf file. It will automatically add the DNS servers we configured in step 2 above to the file. We can confirm this by reading the file:
admin@web01:~$ cat /etc/resolv.conf
The output is:
# 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 10.10.10.1 nameserver 22.214.171.124
That’s it! Your Ubuntu Server is now running using a static IP.