Configure a static IP on Ubuntu Server 14.04

20151229113855At 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:

The Steps

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:

IP address10.10.10.50
Subnet Mask255.255.255.0
Default Gateway10.10.10.1
DNS Server 110.10.10.1
DNS Server 28.8.8.8

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 8.8.8.8

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 8.8.8.8

That’s it! Your Ubuntu Server is now running using a static IP.

References:
Linux Basics – Set A Static IP On Ubuntu
Official Ubuntu Documentation > Network Configuration > Interfaces

Configure a static IP Address in RHEL 6/Centos 6

20151224013419This is a quick post to show how to set a static IP in RHEL6 or CentOS 6. The process is fairly straight-forward however you need to be aware of the different files that need to be changed in order for it to work properly. So, to set a static IP in RHEL6 or CentOS 6 follow the steps below.

The Steps

1. The first step is to edit the interface that you want to set the static IP on. In my example I want to change the IP on eth0 from 172.16.0.21 to 10.10.10.50. This not only means that the IP address will change but the default gateway will also. So to proceed I will edit the interface config file for eth0, which is ifcfg-eth0 using vi, per the below:

[root@web01 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

2. What you see in this file can vary. The basic requirements for networking are listed below, change them to match your desired networking information:

DEVICE=eth0
BOOTPROTO=STATIC
IPADDR=10.10.10.50
NETMASK=255.255.255.0
GATEWAY=10.10.10.1
ONBOOT=yes

We can see from the above that I have configured the static IP of 10.10.10.50. You must also ensure the correct subnet mask and gateway for your network are configured. All the other parameters must match the above.

3. I tend to remove all superfluous information from the interface file apart from what is listed in step 2 above. If you’re wondering what I mean by this, see the example below:

DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=A5:BC:CB:48:F2:05
TYPE=Ethernet
BOOTPROTO=static
NAME="System eth0"
UUID=5cb07fe0-0cd0-7ddc-45c1-d6fbb43fa6c05
IPADDR=172.16.0.21
NETMASK=255.255.255.0

You do not need NM_CONTROLLED, HWADDR, TYPE, NAME & UUID for the interface to work. In some cases, having the HWADDR parameter present could prevent the interface from working, such as when you clone a VM and it has the HWADDR or mac address of the old server in the interface file. As as personal best practice I remove all of this unneeded gumph and keep the file as clean and as simple as possible. Another major point to highlight is that in the above config the GATEWAY parameter is not present, whereas in my config in step 2 it is present.

4. The next step is to check the /etc/sysconfig/network file. As before, edit it in vi:

[root@web01 ~]# vi /etc/sysconfig/network

I see the following:

NETWORKING=yes
HOSTNAME=web01

So there is nothing to change for me. However, this is where the GATEWAY parameter can also be configured. If it is not stated in the interface config file, per the example interface config file in step 3 you should see the settings below in the /etc/sysconfig/network file:

NETWORKING=yes
HOSTNAME=web01
GATEWAY=172.16.0.1

If your interface config file doesn’t contain the GATEWAY parameter, then you will need add it here to reflect your new default gateway. So using my example it would be changed to:

NETWORKING=yes
HOSTNAME=web01
GATEWAY=10.10.10.1

5. The next step is to update the /etc/hosts file. As before, edit it in vi:

[root@web01 ~]# vi /etc/hosts

You should see a line with the server’s old static IP followed by the hostname, per the below:

127.0.0.1   localhost
172.16.0.21     web01

Change the line to reflect the new IP, in my case 10.10.10.50:

127.0.0.1   localhost
10.10.10.50     web01

Exit and save the changes to the file.

6. To commit the changes made in the network and hosts files above in steps 4 & 5 respectively you can either reboot the server or restart the network services. If it’s a production system and you want to avoid any downtime restart the network services by running:

[root@web01 ~]# service network restart

After restarting the network services the operating system will use the new IP.

References:
How To Configure Static IP On CentOS 6
How to Configure a Static IP Address in Red Hat, CentOS and Fedora
Setup static IP in redhat 6