Multiple ip addresses on one physical NIC using virtual NICs in centos

Multiple ip addresses on one physical NIC.

I have a gigabyte brix running esxi to host a few virtualised servers. I have one server for my web development. I have multiple apps that all require running on port 80. I have one physical network port. Fortunately, you can have multiple ip addresses on one physical NIC.

One approach to get all these apps would be to run them on different ports
– but this means you need to remember the port they are running on – this would be fine if the
setup was for one user, but in an environment where there are multiple users / teams then its better

Another approach (if you have the hardware resources) would be to run multiple virtual servers, each with its own app
– eg one for gitlab, one for a LAMP stack, one for a nodejs stack, one for CI
– this approach quickly uses up hardware though

The setup that I chose uses one virtual server for my LAMP stack, my nodejs stack and my CI apps (gitlab, gitlabCI, jenins, travis, etc) and uses multiple ip addresses on one physical nic.
This means that I have one virtual server to spin up for all my dev work. There are a few drawbacks to this setup, but it allows me to have a single image with everything on it, and I can migrate the apps to their own images later if needed

To get all the apps running on port 80, I need a load of ip addresses allocated to the virtual server.
There are a few ways to do this again, you could add multiple virtual hardware nic cards (eth0, eth1, eth2, eth3, etc) using vmware / virtualbox, and assign each its own IP address,
or you can add virtual nics inside your linux os (eth0:1, eth0:2, eth0:3) and assign a single ip address to each ‘virtual nic’.

I opted for the second method (eth0:1, eth0:2) as I hadnt tried that method before and it works well for my needs, so I wrote this post incase I need it in the future, or incase someone else needed the info

Multiple ip addresses on one physical NIC


cd /etc/sysconfig/network-scripts

In here you will have files that start with ifcfg- eg ifcfg-eth0 to find which file your nic card uses, you can use ifconfig


cp ifcfg-(your nic) ifcfg-(your nic):0

Then edit that new file


vi ifcfg-(your nic):0

for my setup, I just configured static IP addresses, but you could use dhcp, or anything else – just make sure that the DEVICE= line matches the filename, eg DEVICE=eth0:0 and that if you are using a static IP, you make sure that the ip address isnt in use anywhere else

You can repeat this process multiple times:


cp ifcfg-(your nic) ifcfg:1
cp ifcfg-(your nic) ifcfg:2
cp ifcfg-(your nic) ifcfg:3

Once finished, run


service network restart

And ping the ip address from another machine.
You can now assign different services to different ip addresses / nic’s all running on the same machine, and all running on port 80

Sources:
Linuxconfig

Leave a Reply