CentOS secondary IP (IP Alias) How-to

Submitted by Shoel on Wed, 01/20/2010 - 18:48

This how-to will show you how to add a additional IP addresses to a single NIC (Network Interface Card) with the use of aliases.

You will find your interface configurations under /etc/sysconfig/network-scripts/

Each interface is represented by a file named ifcfg-ethX - where X represents the interface's unique number/id. As an example, the first/default interface would have a corresponding configuration file called ifcfg-eth0.

In order to create an alias for the interface, you need to create a file in the format ifcfg-ethX:Y - where X is the interface number and Y represents the alias number. The first alias would be numbered 0, the next alias 1, and so forth. For the first alias on the first interface, you would create a file called ifcfg-eth0:0.

To add a secondary IP address via an alias to eth0, we open ifcfg-eth0:0 in a text editor (I will use my favorite, nano, for this example - you are of course free to use whatever editor you prefer):

nano -w /etc/sysconfig/network-scripts/ifcfg-eth0:0

And add the following configuration:

DEVICE=eth0:0
BOOTPROTO=none
ONPARENT=yes
IPADDR=XXX.XXX.XXX.XXX
GATEWAY=XXX.XXX.XXX.1
NETMASK=255.255.255.0

-- This assumes that your IP addresses are in the same vlan/range as your primary interface, and you will need to replace the IPADDR, GATEWAY and in some cases the NETMASK with the appropriate values. If in doubt, use ifcfg-eth0 as a guide, or ask someone to help you. --

In order to apply the new IP address, you can either do a full network restart (which will interrupt all services temporarily):

service network restart

Or you can simply use ifup to bring up the alias "interface":

ifup eth0:0

To verify that everything is OK, issue the ifconfig command to list active interfaces:

# ifconfig

eth0      Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX
          inet addr:XXX.XXX.XXX.XX  Bcast:XXX.XXX.XXX.255  Mask:255.255.255.0
          inet6 addr: XXX::XXX:XXXX:XXXX:XXXX/XX Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1672376185 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1830099250 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1022777289 (975.3 MiB)  TX bytes:111054671 (105.9 MiB)

eth0:0    Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX
          inet addr:XXX.XXX.XXX.XX  Bcast:XXX.XXX.XXX.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

(...)

That should be it! If you have any issues, feel free to comment or get in touch.

Copyright © 2013 - CentOSForge.com