assign multiple ip addresses to single network interface

How to Assign Multiple IP Addresses to Single Network Interface

Did you know you can assign multiple IP addresses to the same network interface? It is known as IP aliasing. It allows you to setup or host multiple websites as virtual hosts, on a single network interface. In this case, you need separate network adapter for each IP but you can create separate virtual interfaces on a single network card. You can use these steps on most RHEL, Fedora & CentOS distributions.


How to Assign Multiple IP Addresses to Single Network Interface

Here are the steps to assign multiple IP addresses to single network interface. We have default interface for Ethernet device, ifcfg-eth0. Its device network files are located at /etc/sysconfig/network-scripts/. You can view them using ls command.

# sudo ls -l /etc/sysconfig/network-scripts
ifcfg-eth0   ifdown-isdn    ifup-aliases  ifup-plusb     init.ipv6-global
ifcfg-lo     ifdown-post    ifup-bnep     ifup-post      net.hotplug
ifdown       ifdown-ppp     ifup-eth      ifup-ppp       network-functions
ifdown-bnep  ifdown-routes  ifup-ippp     ifup-routes    network-functions-ipv6
ifdown-eth   ifdown-sit     ifup-ipv6     ifup-sit
ifdown-ippp  ifdown-tunnel  ifup-isdn     ifup-tunnel
ifdown-ipv6  ifup           ifup-plip     ifup-wireless

Let us say we want to bind 3 addresses 54.43.32.22, 54.43.32.23, and 54.43.32.24 to NIC (IP address 54.43.32.21). Here is a summary of final configuration desired.

Adapter            IP Address                Type
-------------------------------------------------
eth0              54.43.32.21            Primary
eth0:0            54.43.32.22            Alias 1
eth0:1            54.43.32.23            Alias 2
eth0:2            54.43.32.24            Alias 3

So we will clone the ifcfg-eth0 into 3 copies, one for each alias, with the following command.

# cd /etc/sysconfig/network-scripts/
# cp ifcfg-eth0 ifcfg-eth0:0
# cp ifcfg-eth0 ifcfg-eth0:1
# cp ifcfg-eth0 ifcfg-eth0:2

Now open ifcfg-eth0 in a text editor.

# vi ifcfg-eth0

DEVICE="eth0"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=54.43.32.21
NETMASK=255.255.255.224
GATEWAY=172.16.16.100
HWADDR=00:0C:29:28:FD:4C

We need to replace DEVICE and IPADDR parameters in each of the newly created files with the device name and IP address of alias.

Finally, here is what each of the 3 new files will look like.

ifcfg-eth0:0

DEVICE="eth0:0"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=54.43.32.22
NETMASK=255.255.255.224
GATEWAY=172.16.16.100
HWADDR=00:0C:29:28:FD:4C

ifcfg-eth0:1

DEVICE="eth0:1"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=54.43.32.23
NETMASK=255.255.255.224
GATEWAY=172.16.16.100
HWADDR=00:0C:29:28:FD:4C

ifcfg-eth0:2

DEVICE="eth0:2"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=54.43.32.24
NETMASK=255.255.255.224
GATEWAY=172.16.16.100
HWADDR=00:0C:29:28:FD:4C

Once you have updated the above files, restart network service to apply changes.

# sudo /etc/init.d/network restart

You can verify the setup with ifconfig or ip command.

# ifconfig

You may also ping each of the above 3 new IP address aliases with ping commands, from different machines.

ping 54.43.32.22
ping 54.43.32.23
ping 54.43.32.24

That’s it. Now you can use these IP aliases to host multiple websites & services on a single network interface.

Also read:

How to Find Public IP Address of Server
How to Find Folder Name in Linux
How to Check Open Ports in UFW
How to Debug Shell Script
How to Sort Text File in Python

One thought on “How to Assign Multiple IP Addresses to Single Network Interface

Leave a Reply

Your email address will not be published. Required fields are marked *