It's sometimes necessary to add a second IP address to a server for a new service, such as when configuring a STUN server (which requires two public IP's).

I've sometimes had to do this in situations where a hosting provider has no remaining free IP's in the server's current subnet and have therefore had to allocate an IP in another network. This is what to do when that happens.

Please note that it is almost always better to stick to a single network if you can as each network carries a certain amount of broadcast traffic which reduces your effective data throughput rates.

The trick here is to instantiate a virtual subinterface on the nic. This is done by configuring an interface that looks (to an administrator) very similar to a physical interface with a bit tacked on to the end of it's name, it's followed a colon then a number, e.g. eth1:4 for virtual interface 4 on eth1. Note that the actual virtual interface number used doesn't really matter, as far as I'm aware it's not used for anything other than to differentiate it from other (possible) virtual subinterfaces, although I'm sure strong arguments can be made to increment from 0.

Incidentally, this is quite similar to the method for adding an 802.1q vlan tagged virtual interfaces where a period is used instead of a colon, for example eth0.22 for an interface tagged as vlan 22 on physical port eth0)

This can be achieved on pretty much all linux boxes by the following otherwise standard use of the ifconfig command (substituting the correct IP and netmask of course):

ifconfig eth0:1 netmask up

however to make the change persistent across reboots requires a little more and varies between ditros. I'll cover Debian and RHEL based distros here:

Debian based (inc. Ubuntu)

Edit /etc/network/interfaces and add a eth0:0 or such stanza to the end of the file:

auto eth0:0
iface eth0:0 inet static

You can add as many as you need. Officially you should be able to restart networking to apply the settings (using service networking restart or /etc/init.d/networking restart) but sometimes a full reboot is required, especially if your host is a VPS.

Redhat based (inc. CentOS)

The actual file you need to edit is based on the interface name. Look in /etc/sysconfig/network-scripts/ to identify current interfaces and then create another file in the same folder. For example if you need to add another IP on eth0 you would have to create /etc/sysconfig/network-scripts/ifcfg-eth0:0 (or 0:1) etc.. with the following content:


Finally, running service network restart to activate the change (with the same corollary about needing to reboot some VPS's)

And finally

A word of warning about in band changes to networking settings. Be careful when you change and restart networking settings if you are accessing the box over the network. Double check your settings, it's quite easy for a typo to prevent the network from coming back up and requiring you to do the walk of shame to the console. For remote servers I use a script that backs up network settings and then automagically restores them on next but one boot (most hosting has an ACPI power cycle opton on the control panel).