Assign Virtual IPs to your NIC

Can a network card have multiple IPs assigned to it?
The answer is absolutely yes! Some times, for example when you run several servers on a machine or when you need IP-based Apache virtualhosts, it is useful to bind a server or a virtualhost on its own IP address. I am not going to get into much detail on the pros and cons of virtual IPs, it would be pointless anyway since I am no pro, but here is how it is done.

Assigning a virtual IP to a NIC is a very easy task either you use the system-config-network tool or just do some text file editing. The script ifconfig can also be used to create a virtual network interface, but this would not be permanent since the changes ifconfig makes do not survive a reboot. In this post I’ll stick with the “manual” way…

In Fedora, all information about the network interfaces is kept in the following directories:

  • /etc/sysconfig/network-scripts/
  • /etc/sysconfig/networking/

I assume that the default NIC configuration script is:
/etc/sysconfig/network-scripts/ifcfg-eth0. Mine looks like this:

DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.0.255
HWADDR=00:00:00:00:00:00
IPADDR=192.168.0.1
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes
TYPE=Ethernet
GATEWAY=192.168.0.254

BOOTPROTO: sets the protocol that is used when the device is initialized. Since we use static IPs we set it to static.
HWADDR: is the MAC address of your network card. Do not change it. If this is missing in your configuration file, then don’t add it.
The rest of the options used are self-explanatory.

Make a copy of this in the same directory naming the new file ifcfg-eth0:1

# cp ifcfg-eth0 ifcfg-eth0\:1

eth0:1 is an alias of the eth0 interface. Now, let’s assign a different IP address to eth0:1. Other NIC aliases could be named eth0:2, eth0:3 etc. Fire up your favourite text editor and edit ifcfg-eth0:1. The modifications are shown in bold:

DEVICE=eth0:1
BOOTPROTO=static
BROADCAST=192.168.0.255
HWADDR=00:00:00:00:00:00
IPADDR=192.168.0.101
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes
TYPE=Ethernet
GATEWAY=192.168.0.254

So, its IP address will be 192.168.0.101. Save the file and copy it to /etc/sysconfig/networking/devices/:

# cp ifcfg-eth0\:1 /etc/sysconfig/networking/devices/

Also, copy it to your default network profile or whichever profile you use:

# cp ifcfg-eth0\:1 /etc/sysconfig/networking/profiles/default/

Now, bring up the new interface using the ifup script:

# ifup eth0\:1

Running ifconfig, the new interface should be listed. You can also check it by pinging:

# ping 192.168.0.101

You can now assign a host name on this virtual interface, by updating your local DNS server’s zone files or by adding it to the /etc/hosts files on all your LAN computers.

That’s it.

Assign Virtual IPs to your NIC by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2005 - Some Rights Reserved

George Notaras avatar

About George Notaras

George Notaras is the editor of the G-Loaded Journal, a technical blog about Free and Open-Source Software. George, among other things, is an enthusiast self-taught GNU/Linux system administrator. He has created this web site to share the IT knowledge and experience he has gained over the years with other people. George primarily uses CentOS and Fedora. He has also developed some open-source software projects in his spare time.

3 responses on “Assign Virtual IPs to your NIC

  1. Yann Kabal Permalink →

    That’s nice ! Thx !

  2. Jerczey Permalink →

    Hello!

    Thank you very much for the detailed note!

    I have an issue though,
    I have network manager on my desktop and it would seem that it allows me to have only one active connection at a time, meaning unreachable host for virtual ip!

    for example eth0 is active but I can’t reach eth0:1

    If I use the connection manager to connect to eth0:1 then eth0 is unreachable.

    do you know a fix?