Alternate way to add IP alias in Linux

The traditional way to add IP alias is to create eth0:0, eth0:1, eth0:2 and so on. First lets create one the old way. To add an IP alias to the interface eth0

# ifconfig eth0:0  192.168.1.10/24
# ifconfig eth0:0
eth0:0    Link encap:Ethernet  HWaddr 00:50:56:89:7F:9D
          inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

I prefer using CIDR but you can very well use dotted decimal format.

#ifconfig eth0:0  192.168.1.10 netmask 255.255.255.0
#ifconfig eth0:0
eth0:0    Link encap:Ethernet  HWaddr 00:50:56:89:7F:9D
          inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

If you restart the server or restart the network, the alias IP will go away. To make this persistent, we do the following-

cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0
vi /etc/sysconfig/network-scripts/ifcfg-eth0:0

Replace DEVICE=eth0 with DEVICE=eth0:0. Replace IPADDR and NETMASK with actual IP address and mask. Save the changes and exit. Then restart the network services or bring up eth0:0.

service network restart
#In case of Systemd 
systemctl restart network

The new way
The new way is to simply add IPADDR2 and NETMASK2 to the existing interface configuration file. If you need a second alias, add IPADDR3 and so on. To add an IP alias to eth0, I edit /etc/sysconfig/network-scripts/ifcfg-eth0 and add IPADDR3 and NETMASK2.

# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.10.11
NETMASK=255.255.255.0
TYPE=Ethernet
IPADDR2=192.168.1.10
NETMASK2=255.255.255.0

Then restart the network service. ifconfig will not show the IP address added this way. Use ip command and ip will show alias added in both days.

# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:89:7f:9d brd ff:ff:ff:ff:ff:ff
inet 192.168.10.11/24 brd 192.168.10.255 scope global eth0
inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0

PS: I have not tried this outside of RHEL.

The ip command way
We can also easily add multiple IPs to the same interface using ip command. I am adding three IPs to eth0

# ip addr add 192.168.1.10/24 dev eth0
# ip addr add 192.168.1.11/24 dev eth0
# ip addr add 192.168.1.12/24 dev eth0
# ip a

We can see the new three IPs under eth0

1: lo:  mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:89:7f:9d brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.11/24 brd 192.168.10.255 scope global eth0
    inet 192.168.1.10/24 scope global eth0
    inet 192.168.1.11/24 scope global secondary eth0
    inet 192.168.1.12/24 scope global secondary eth0

Again, this is not persistent. We need to use one of the methods to make it persistent.

Advertisements

One thought on “Alternate way to add IP alias in Linux

  1. Pingback: What’s new in RHEL 7: Networking? – rtfmp

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s