How to assign alias IP addresses to a network card prior to RHEL8.

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux

Issue

  • How to assign an additional TCP/IP address to a NIC?
  • IP aliases added using the ip addr command aren't persistent after a network restart or server reboot. How to configure permanent or persistent IP aliases?

Resolution

There are two ways to add another IP address to an interface:

  • The old way (aliases) 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.

WARNING: Due to issues described in ifdown of alias removes all IPs off the interface the "new way" is advised.

For RHEL8 and newer refer to How to add and remove an alias IP Address for RHEL8+

The NetworkManager component has always supported the new way of assigning additional IP addresses, and has been updated in RHEL-6.4.z (NetworkManager-0.8.1-61.el6) to support the old way as well.

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 10.8.109.149:

# nmcli con
NAME         UUID                                  TYPE      DEVICE 
System eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  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 10.8.109.229/24 brd 10.8.109.255 scope global dynamic noprefixroute eth0
       valid_lft 39711sec preferred_lft 39711sec
       
# nmcli con mod 'System eth0' +ipv4.addresses 10.8.109.149/24
# 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 10.8.109.229/24 brd 10.8.109.255 scope global dynamic noprefixroute eth0
       valid_lft 43196sec preferred_lft 43196sec
    inet 10.8.109.149/24 brd 10.8.109.255 scope global secondary noprefixroute eth0
       valid_lft forever preferred_lft forever

It is also possible to add additional IP addresses using the graphical user interface.

Note: if not using NetworkManager, you can still edit the config file directly using the options below, (Note these changes don't require NetworkManager and can be applied directly to the ifcfg-* files. (These manual changes should work with RHEL 6.5+)

    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 to 255.
    It can be omitted if there is only one address being configured.

Example:

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 192.168.122.100/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=192.168.122.100
NETMASK2=255.255.255.0

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

# ifdown eth0; ifup eth0

Verifying:

# 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 192.168.122.100/24 brd 192.168.122.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)
Notes:

  • The ONPARENT= option isn't currently supported by NetworkManager.
  • It isn't possible to configure the additional IP address the old way using NetworkManager's graphical interface.

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

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

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

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

# ifup eth0:1

Verifying:

# ifconfig eth0:1
eth0:1    Link encap:Ethernet  HWaddr 52:54:00:XX:XX:XX
          inet addr:192.168.122.100  Bcast:192.168.122.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 192.168.122.100/24 brd 192.168.122.255 scope global eth0:1
    inet6 fe80::5054:ff:fexx:xxxx/64 scope link 
       valid_lft forever preferred_lft forever

Configuring the new way with NetworkManager (prior to 0.8.1-61)
This follows the same steps as described above in Configuring the new way with NetworkManager (0.8.1-61 or newer)

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

  • Earlier NetworkManager versions don't support this method, so it's necessary to disable NetworkManager service as described below.

You will need to stop the NetworkManager service, as it can not control an aliased interface. In order to stop NetworkManager follow 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.122.100
NETMASK=255.255.255.0

3. Start the secondary interface:

# ifup eth0:1

or

# service network restart

Comments

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.