How to assign alias IP addresses to a network card

In order to add an additional IP address to an interface, there are two options:

Aliases, in the old way, generate a new virtual interface named ethX:Y, for example, eth0:1. Each interface has its own IP address. A label is applied to it in ifconfig output as well as ip output.

In the new method, the main interface is given a secondary address. As a result, many IP addresses can be added to a single physical interface rather of requiring a separate interface for each one. In this scenario, the ip tool must be used because the ifconfig tool is too old to see the new IP addresses. Nowadays, this is the preferred method.

In CentOS-6.4 (NetworkManager-0.8.1-61.centos6), the NetworkManager component now supports both the old and new methods of allocating additional IP addresses.

How to check the version being used?

# rpm -q NetworkManager
NetworkManager-0.8.1-61.el6.x86_64

Configuring the new way with NetworkManager (0.8.1-61 or newer)

Using nmcli, e.g. adding 5.178.113.24:

# nmcli con
NAME         UUID                                  TYPE      DEVICE 
System eth0  8fb06bd0-0bb0-7ffb-45f1-d68dd65f3e03  ethernet  eth0   

# ip -4 a show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    inet 5.178.113.24/27 brd 10.8.109.255 scope global dynamic noprefixroute eth0
       valid_lft 39711sec preferred_lft 39711sec

# nmcli con mod 'System eth0' +ipv4.addresses 5.178.113.24/27
# nmcli con up 'System eth0'

# ip -4 a show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    inet 5.178.113.25/27 brd  5.178.113.255 scope global dynamic noprefixroute eth0
       valid_lft 43196sec preferred_lft 43196sec
    inet 5.178.113.24/27 brd 5.178.113.255 scope global secondary noprefixroute eth0
       valid_lft forever preferred_lft forever

If you don’t want to use NetworkManager, you can update the config file manually using the options below. (Note that these modifications do not require NetworkManager and can be made to the ifcfg-* files directly.)

 IPADDRn=
      IP Address
    NETMASKn=
      Subnet mask; just useful for aliases and ippp devices.  For all other
      configurations, use PREFIX instead.

    The "n" is expected to be consecutive positive integers starting from 0.
    It can be omitted if there is only one address being configured.

An example is given below.

Checking the original file:
# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
TYPE=Ethernet
BOOTPROTO=none
IPADDR=192.168.122.2
PREFIX=24
DNS1=192.168.122.1
DOMAIN=lan
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=eth0
UUID=8dc6deb4-4868-46a1-bc3b-0a8fb55fxxxx
ONBOOT=yes
LAST_CONNECT=1380032766

Now adding IP address 172.31.33.1/255.255.255.0 to that file.

# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
TYPE=Ethernet
BOOTPROTO=none
IPADDR=192.168.122.2
PREFIX=24
DNS1=192.168.122.1
DOMAIN=lan
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=eth0
UUID=8dc6deb4-4868-46a1-bc3b-0a8fb55fxxxx
ONBOOT=yes
LAST_CONNECT=1380032766
IPADDR2=172.31.33.1
NETMASK2=255.255.255.0

Then, to make the changes take effect, bring the interface down and up.

# ifdown eth0; ifup eth0

Now verify as follows.

# ip address list dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:XX:XX:XX brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global eth0
    inet 172.31.33.1/24 brd 172.31.33.255 scope global eth0
    inet6 fe80::5054:ff:fexx:xxxx/64 scope link 
       valid_lft forever preferred_lft forever

Configuring the old way with NetworkManager (0.8.1-61 or newer)

This method requires another ifcfg file named as ifcfg-<iface>:<alias>

See an example below of an alias interface with IP address 172.31.33.1/255.255.255.0

# cat /etc/sysconfig/network-scripts/ifcfg-eth0:1 
DEVICE=eth0:1
ONPARENT=yes
IPADDR=172.31.33.1
NETMASK=255.255.255.0

Then bring up the interface to make the changes take effect.

# ifup eth0:1

Now verify as follows.

# ifconfig eth0:1
eth0:1    Link encap:Ethernet  HWaddr 52:54:00:XX:XX:XX
          inet addr:172.31.33.1  Bcast:172.31.33.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

# ip address list dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:XX:XX:XX brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global eth0
    inet 172.31.33.1/24 brd 172.31.33.255 scope global eth0:1
    inet6 fe80::5054:ff:fexx:xxxx/64 scope link 
       valid_lft forever preferred_lft forever

Configuring the old way with NetworkManager (prior to 0.8.1-61)

Because earlier NetworkManager versions do not support this method, the NetworkManager service must be disabled as explained below.

The NetworkManager service must be stopped since it cannot control an aliased interface. To terminate NetworkManager, perform the following steps.

# service NetworkManager stop 
# chkconfig NetworkManager off

Create the alias interface configuration file.
Here is an example for /etc/sysconfig/network-scripts/ifcfg-eth0:1

DEVICE=eth0:1
ONPARENT=yes
IPADDR=192.168.200.2
NETMASK=255.255.255.0

Start the secondary interface as follows.

# ifup eth0:1

or

# service network restart

Leave a Reply