How to Assign Multiple IP Addresses to one Network Interface on CentOS

Configuring multiple IP addresses on one network interface is commonly called IP Aliasing. It is very useful when you setup multiple sites on virtual web hosting on a single interface.

There are few ways to setup IP Aliasing. One is using ifconfig like:

[root@burnz ~]# ifconfig eth0:1 192.168.16.19 netmask 255.255.255.248 up
[root@burnz ~]# ifconfig eth0:2 192.168.16.20 netmask 255.255.255.248 up

To verify all ip addresses assigned to eth0 run this command:

[root@burnz ~]# ip addr list dev eth0

Results would be:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:19:f6:d8:c8:86 brd ff:ff:ff:ff:ff:ff
    inet 192.168.166.18/29 brd 192.168.16.23 scope global eth0
    inet 192.168.16.19/29 brd 192.168.16.23 scope global secondary eth0:1
    inet 192.168.16.20/29 brd 192.168.16.23 scope global secondary eth0:2
    inet6 fe80::246:e3gg:feb8:f591/64 scope link
       valid_lft forever preferred_lft forever

You can also use ifconfig command to verify:

[root@burnz ~]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:19:F6:D8:C8:86
          inet addr:192.168.16.18  Bcast:192.168.16.233  Mask:255.255.255.248
          inet6 addr: fe80::246:e3gg:feb8:f591/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11333116 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9978256 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1427419683 (1.3 GiB)  TX bytes:1562204398 (1.4 GiB)

eth0:1    Link encap:Ethernet  HWaddr 00:19:F6:D8:C8:86
          inet addr:192.168.16.19  Bcast:192.168.16.23  Mask:255.255.255.248
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:2    Link encap:Ethernet  HWaddr 00:19:F3:D9:C8:86
          inet addr:192.168.16.20  Bcast:192.168.16.23  Mask:255.255.255.248
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth1      Link encap:Ethernet  HWaddr 00:19:F3:DB:C3:BB 
          inet addr:172.2.8.194  Bcast:172.2.8.223  Mask:255.255.255.224
          inet6 addr: fe80::216:e3ff:feab:f3bb/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:124270 errors:0 dropped:0 overruns:0 frame:0
          TX packets:60627 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:8176024 (7.7 MiB)  TX bytes:5185410 (4.9 MiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:628 errors:0 dropped:0 overruns:0 frame:0
          TX packets:628 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:36416 (35.5 KiB)  TX bytes:36416 (35.5 KiB)

You can also use a manual way by modifying the network config file:

[root@burnz ~]# nano /etc/sysconfig/network-scripts/ifcfg-eth0:1
DEVICE=eth0:1
BOOTPROTO=static
IPADDR=192.168.16.19
NETMASK=255.255.255.248
ONBOOT=yes
$ nano /etc/sysconfig/network-scripts/ifcfg-eth0:2
DEVICE=eth0:2
BOOTPROTO=static
IPADDR=192.168.16.20
NETMASK=255.255.255.248
ONBOOT=yes

Once created you  need to restart the network to activate the IP Aliasing:

[root@burnz ~]# /etc/init.d/network restart

It should be good!