CentOS / RHEL 6 : How to add/remove additional IP addresses to a network interface

There are two ways to add another IP address to an interface. The old way creates a new virtual interface named in the style of ethX:Y where X and Y are numbers, for instance, eth0:1. Each interface has one IP address. It appears in ifconfig output as an ordinary interface and in ip output with a label attached.

The new way adds a secondary address to the main interface. So, instead of having one interface per IP address, it is possible to add many addresses to the real interface. However, ifconfig tool is too old and can’t see the additional IP addresses, so in this case, the ip tool must be used instead. This is the preferred way nowadays.

Add/Remove additional IP manually

1. Use the ip command to display the current ip address configuration of the interface eth0 :

# ip addr show eth0  
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000  
    link/ether 52:54:00:71:98:9d brd ff:ff:ff:ff:ff:ff  
    inet 10.10.122.101/24 brd 10.10.122.255 scope global eth0  
    inet 10.10.122.12/24 scope global secondary eth0  
    inet 10.10.122.11/24 scope global secondary eth0  
    inet 10.10.122.13/24 scope global secondary eth0  
    inet6 fe80::5054:ff:fe71:989d/64 scope link  
       valid_lft forever preferred_lft forever

2. To delete an existing IP

# ip addr del 10.10.122.13/24 dev eth0
# ip addr show eth0  
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000  
    link/ether 52:54:00:71:98:9d brd ff:ff:ff:ff:ff:ff  
    inet 10.10.122.101/24 brd 10.10.122.255 scope global eth0  
    inet 10.10.122.12/24 scope global secondary eth0  
    inet 10.10.122.11/24 scope global secondary eth0  
    inet6 fe80::5054:ff:fe71:989d/64 scope link  
       valid_lft forever preferred_lft forever

3. To add an IP address:

# ip addr add 10.10.122.13/24 dev eth0
# ip addr show eth0  
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000  
    link/ether 52:54:00:71:98:9d brd ff:ff:ff:ff:ff:ff  
    inet 10.10.122.101/24 brd 10.10.122.255 scope global eth0  
    inet 10.10.122.12/24 scope global secondary eth0  
    inet 10.10.122.11/24 scope global secondary eth0  
    inet 10.10.122.13/24 scope global secondary eth0  
    inet6 fe80::5054:ff:fe71:989d/64 scope link  
       valid_lft forever preferred_lft forever
WARNING: The manual method of adding or removing IP address is not persistent and the changes will disappear after a reboot or network service restart

Add/Remove Additional IP persistently

To add or remove additional IP adresses and keep the configuration persistent, we need to Edit the corresponding /etc/sysconfig/network-scripts/ifcfg-eth[x] configuration file and add/remove as many additional IPADDR[n] and PREFIX[n] entries as additional IP addresses are required.

For example the following configuration file:

# cat /etc/sysconfig/network-scripts/ifcfg-eth1  
DEVICE=eth1  
BOOTPROTO=none  
NETMASK=255.255.255.0  
TYPE=Ethernet  
HWADDR=52:54:00:cc:de:0b  
IPADDR=10.10.100.101  
PREFIX=24  
IPADDR2=10.10.128.101  
PREFIX2=24  
IPADDR3=10.10.130.101  
PREFIX3=28

would give the following result:

# ip addr show eth1  
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000  
    link/ether 52:54:00:cc:de:0b brd ff:ff:ff:ff:ff:ff  
    inet 10.10.100.101/24 brd 10.10.100.255 scope global eth1  
    inet 10.10.128.101/24 brd 10.10.128.255 scope global eth1  
    inet 10.10.130.101/28 brd 10.10.130.111 scope global eth1  
    inet6 fe80::5054:ff:fecc:de0b/64 scope link  
       valid_lft forever preferred_lft forever

The following additional entries are possible:

IPADDR: the additional IP address.
PREFIX: the length in bits of the netmask for the additional IP address.
NETMASK: the explicit netmask value for the additional IP address.
BROADCAST: the broadcast address for the additional IP address. This directive is deprecated, as the value is calculated automatically with ipcalc.
Related Post