Changing & Adding Server IP Addresses
Published 29-03-2015
Linux distributions including CentOS allow you to use IP address aliasing. IP address aliasing is the method used to bind multiple IP addresses onto one network interface. Here we will cover how to add additional IP addresses or change your current IP addresses.
First lets list all the IP address that we have on our system. To do this issue the following command.
sudo ip addr list dev eth0
The outcome will look a little like this, depending on how many IP addresses your system has. This system currently has one IP address.
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 74:d4:35:9b:38:ae brd ff:ff:ff:ff:ff:ff
inet 217.39.5.60/28 brd 217.39.5.255 scope global eth0
inet6 fe80::76d4:35ff:fe9b:38ae/64 scope link
valid_lft forever preferred_lft forever
In order to change or add any IP address we need to edit the eth0 config file. To open the eth0 config file issue the following command.
vi /etc/sysconfig/network-scripts/ifcfg-eth0
This file will give you an output like the example below. This is the file we need to change in order to add additional IP addresses.
DEVICE=eth0
TYPE=Ethernet
UUID=2021d1cc-ffa6-46d7-a30a-47a8ab167d8d
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
HWADDR=74:d4:35:9b:38:ae
IPADDR=217.39.5.60
PREFIX=28
GATEWAY=217.39.5.62
DNS1=8.8.8.8
DNS2=8.8.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
NETMASK=255.255.255.240
USERCTL=no
The only lines we need to be concerned with are as follows.
IPADDR=217.39.5.58
PREFEX=28
GATEWAY=217.39.5.62
If we simply need to change the servers IP address all we need to do is edit the line IPADDR but if you are changing your IP address you need to make sure the gateway and netmask don’t need changing as well. If these have changed you will also need to edit the line for the GATEWAY and at the bottom of the file the line NETMASK, once you have made your changes save and exit the file.
If you need to change the NETMASK but you don’t see that line in your file you can just add this line. Once your network services have been restarted they will pick up the new netmask along with the gateway and all the servers IP’s.
If you have more than one IP address you will see something like the following, and will need to edit each accordingly. Note that your servers second IP alias is on line IPADDR2, third and forth IP address would be on line IPADDR3, IPADDR4
IPADDR=217.39.5.58
PREFEX=28
GATEWAY=217.39.5.62
IPADDR2=217.39.5.58
PREFEX2=28
GATEWAY2=217.39.5.62
Your network services will now need to be restarted, to do this issue the following command.
/etc/init.d/network restart
If you have changed the servers primary IP address, gateway or netmask then you will have been disconnected from your SSH client at this point and you will need to log back in.
To add further IP address we need to edit the same file by issuing the following command.
/etc/sysconfig/network-scripts/ifcfg-eth0
For this example we only have one IP address on the server so we need to scroll down to the line GATEWAY=217.39.5.62, and edit the file so it looks like this.
IPADDR=217.39.5.58
PREFEX=28
GATEWAY=217.39.5.62
IPADDR2=217.39.5.59
PREFEX2=28
GATEWAY2=217.39.5.62
We have now added the IP address 217.39.5.62 to the server, restart the network services with the following command so the changes take effect.
/etc/init.d/network restart