Adding Secondary IP Addresses (CentOS/RHEL)

There are plenty of reasons you would need to add secondary IP addresss (and everyone agrees that SEO is not one of them). Getting a secondary IP address is a simple process if it is done for the right reasons and done correctly. You do NOT need additional NIC cards but you will be creating virtual adapters as the secondary IP will be routing through the primary IP.

Also, this is a great thing to do at home as I've done it to run multiple internal IP addresses on one server to run multiple applications across the same ports (for KISS** sake). Please note that I am doing this is in a virtual testing environment so your settings will definitely be different.

** KISS = Keep It Stupid Simple **

You will need to be the root user and navigate to your /etc/sysconfig/network-scripts

# cd /etc/sysconfig/network-scripts

When getting a list of files in the directory you will see "ifcfg-eth0" (or eth1 if you're doing it for a different adapter)

# ls -l | grep ifcfg-eth
-rw-r--r-- 1 root root   119 Jan 11 19:16 ifcfg-eth0
-rw-r--r-- 1 root root   119 Jan  3 08:45 ifcfg-eth0.bak
-rw-r--r-- 1 root root   119 Feb 24 04:34 ifcfg-eth1
-rw-r--r-- 1 root root   128 Jan 19 18:20 ifcfg-eth1.bak

Now adding the virtual adapters is easy. Basically if the main adapter is called "eth0" you have to call the next (virtual) adapter in a sequential order like so:

  • ifcfg-eth0 (primary adapter, physical)
  • ifcfg-eth0:1 (first virtual adapter to the physical primary adapter)
  • ifcfg-eth0:2 (second virtual adapter to the physical primary adapter)
  • and so on...

That being said, lets go ahead and copy our primary adapter configuration file and name it to be the first virtual adapter for the physical primary:

# cp ifcfg-eth0 ifcfg-eth0:1
# ls -l | grep ifcfg-eth
-rw-r--r-- 1 root root   119 Jan 11 19:16 ifcfg-eth0
-rw-r--r-- 1 root root   119 Feb 24 08:53 ifcfg-eth0:1
-rw-r--r-- 1 root root   119 Jan  3 08:45 ifcfg-eth0.bak
-rw-r--r-- 1 root root   119 Feb 24 04:34 ifcfg-eth1
-rw-r--r-- 1 root root   128 Jan 19 18:20 ifcfg-eth1.bak

Now, we have to configure this virtual adapter to be: a static IP (of course), no hardware address (MAC), configure netmask and of course rename the device.

# vim ifcfg-eth0:1
DEVICE=eth0:1
BOOTPROTO=static
ONBOOT=yes
IPADDR=10.1.1.2
NETMASK=255.255.255.0

There is no need to specify a MAC address as it is a virtual adapter and there is also no need to specify a default gateway as it is already routed through the primary adapter. Basically there are only four things that you will need to change:

  • File name for the adapter itself
  • DEVICE=<device name> (should correspond with the file name)
  • IPADDR=<ip address>
  • NETMASK=<netmask>

Afterwards, just restart the networking service:

# service network restart

That's it; lets check ifconfig to make sure the virtual adapter is there and working:

# ifconfig eth0:1
eth0:1    Link encap:Ethernet  HWaddr 08:00:27:ED:05:B7
inet addr:10.1.1.2  Bcast:10.1.1.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

# ping 10.1.1.2
PING 10.1.1.2 (10.1.1.2) 56(84) bytes of data.
64 bytes from 10.1.1.2: icmp_seq=1 ttl=64 time=0.073 ms
64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=0.042 ms
64 bytes from 10.1.1.2: icmp_seq=3 ttl=64 time=0.029 ms
64 bytes from 10.1.1.2: icmp_seq=4 ttl=64 time=0.029 ms
--- 10.1.1.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.029/0.043/0.073/0.018 ms

Per Steven's comment: a second note. If you're not sure if you've done it right and you do not want to restart the entire network server, you can use the following:

# ifup eth0:1

You may also like...

15 Responses

  1. dom says:

    first page on google lol
    configure secondary ip centos

  2. Steven Klassen says:

    Great walk-through, thanks! If you're not sure you've done it right, an ifup would work too.

  3. Steve Wingate says:

    Not bad. You should consider a career in web hosting, lolz.

  4. me says:

    cat /etc/sysconfig/network-scripts/ifcfg-eth0
    ...
    NETMASK2=255.255.255.0
    IPADDR2=10.250.10.25
    ...

    systemctl restart network

    Its easier and faster

  5. Ankit says:

    It worked for me... Simple and easy.

  6. Bashed says:

    Hoping you can help me out here on something related. I have a KVM host node running a guest VMs. I create them using SolusVM panel, however they don't have an option to add bulk IPs in one shot to a guest VM. I'd like to know how I can add a /27 or such manually via CLI on the host, to a guest VM? I'm running Centos 6 Linux on the Host and Guest VMs.

    • David says:

      Hi Chad! I'm not sure about SolusVM. Is there an open-source or free version I can test with (it looks interesting!)? As for adding the IP addresses to the hosts, you can't have additional DHCP virtual NICs (eth0:1, eth0:2). All virtual NICs have to be configured with static IP addresses.

      As for assigning them, you should be able to assign them without issue and it *should* work. Then again, I cannot be sure as the panel may be controlling other aspects that are unknown (similar to how cPanel integrates itself heavily and will over-write manual configuration changes if not performed through the panel).

      If you are needing to add an entire /27 as virtual NIC's (eth0:1 -> eth0:30) *IN BULK* (or all at once, I assume) you will need to either script this or you will need to manually configure each one, assuming the SolusVM panel does not have the ability to do bulk imports via CSV or some other method.

  7. yaron says:

    BOOTPROTO=static
    This looks like an invalid option, only none, bootp and dhcp are allowed. When using a static IP this should have the value "none".

    https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/s1-networkscripts-interfaces.html

    • David says:

      You could also use "iwantedastaticIP" if you wanted as the script only switches to use dhclient if bootproto =! dhcp or bootp. It will only use Dynamic if you put dhcp or bootp -- anything else in this field will cause it to be configured as a static IP address in the end.

  8. jyoti says:

    can't ssh through virtual adapter's ip. although , the ip of primary adapter is ssh-able. it renders the virtual adapter's ip useless.

    • David says:

      I've seen this before -- you might have a firewall that specifies the destination, interface, or you may have SSH listening specifically on the primary IP address and not all IP addresses (0.0.0.0). Try checking those several things and get back to me!

  9. Thank you this was the best and most accurate description that I have found.

  10. KAMALRAJA SUGUMAR says:

    Thank you. Very simple and useful.

  11. djasa says:

    For the record, there's no such thing as secodary devices and ifconfig command lies in futile effort to emulate BSD kernel networking internals. Since kernel 2.2, which was released in last millenium. To see what kernel sees, use command 'ip'.

    You can specify multiple IP addresses in the ifcfg-* file using IPADDR0/1/2/... + PREFIX0/1/2/... + GATEWAY0/1/2/... configuration keys

  12. Adriaan says:

    This is awesome! Very well explained.

Leave a Reply

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