Title: how to add virtual ip address to linux
Description: how to add virtual ip address to linux Linux
Tags: how, to, add, virtual, ip, address, to, linux
Info: This Post Has Been Viewed 15142 Times SinceFri Jan 09, 2009 3:11 pm Author wallpaperama With 13 Replies #6967

how to add virtual ip address to linux

on this post i will teach you how you can add an additional ip interface address to your linux server. we will be adding ip 10.0.0.2

lets say we already have binded our ip of 10.0.0.1 in our NIC which is ETH0 on this step by step tutorial guide i will show you how you can add anothre ip address to the same interface but it will be a virtual interface. this will be very easy using ssh. so login to your server with root acess or as a super user.

NOTE: i am using a red hat fedora core system for this example, i think is should work on centos but not sure about other distributions like ubuntu or debian

the first command is going to make a copy of the current interface:
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0


now that we have a copy of our new virtual interface, you need to edit the file:
nano /etc/sysconfig/network-scripts/ifcfg-eth0:0
NOTE: i am using nano as my text editor, im sure you can use whatever your favorite text editor is. like VI for example

ok, when you first open ifcfg-eth0:0 it will probably look like this:

NETMASK=255.255.255.0
DEVICE=eth0
ONBOOT=yes
TYPE=Ethernet
ETHTOOL_OPTS="duplex full speed 100 autoneg off wol g"

IPADDR=10.0.0.01


now, change the following lines/fields to match the new virtual ip:

DEVICE=eth0:0
IPADDR=10.0.0.02


On my sample here, i only change DEVICE and IPADDR

now that our new virtual interface it configured, its time to make the changes work, how do we do that.. we do that by restarting our network. so send this unix shell command to restart our network in our server

service network restart


or another way to restart your network is with this command:
/etc/init.d/network restart


ok, if you are going through ssh, wait like 10 seconds the if the changes took affect and there were no problems, it should work ok. if you get disconnected, try to connect to your ssh session again.

now that the network has been restarted, you can test it by pinging your new virtual ip
ping 10.0.0.2


after you ping you should get successful ping like this:
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.018 ms
64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=0.021 ms
64 bytes from 10.0.0.2: icmp_seq=4 ttl=64 time=0.019 ms


thats it,

Comments (13)
View Top Comments
Leave Your Comments...
#1
1
Manjunath:
3 years ago
#90695
Manjunath Tue Jan 20, 2009 1:03 am
thanks, my problem of virtual ip is solved................
#2
harsh:
2 years ago
#109496
harsh Wed Jul 15, 2009 8:11 pm
you chk the details from whatismyipaddress /
#3
anrudh65:
2 years ago
#109497
anrudh65 Wed Jul 15, 2009 8:13 pm
15-6967-04.jpg
i chk this post. i searched for this linux ip address details.but it is better if it is in a detailed information. i normally use the website to chk the ip address from ip-details /
#4
Raja:
1 year ago
#127771
Raja Wed Feb 10, 2010 12:14 am
**************Thanking you*************


#5
1
rony:
1 year ago
#130562
rony Thu Mar 25, 2010 12:51 am
You can add virtual ip in debian derivative by like this :
#vi /etc/network/interfaces

eth0
iface eth0 inet static
address 192.168.0.5
netmask 255.255.255.0
network x.x.x.x
broadcast x.x.x.x
gateway x.x.x.x

auto eth0:1
iface eth0:1 inet static
address 192.168.0.50
netmask 255.255.255.0
network x.x.x.x
broadcast x.x.x.x
gateway x.x.x.x
#6
nillgun:
1 year ago
#139656
nillgun Tue Jul 20, 2010 11:13 pm
wow (;
thank man,
I do complite.................
#7
saravana:
10 months ago
#153609
saravana Tue Mar 01, 2011 9:55 pm
hi
but how i can do by giving comments?
just like
ifconfig -a eth0:1 <ip address>
..i used this..but when i restrat the services .virtual ip could disappear,,
how i can resolve
#8
Morlack:
8 months ago
#157448
Morlack Thu May 26, 2011 4:02 am
- at - saravana : I suppose ifconfig -a has temporary effect (like ifconfig eth0 down, when u restart network eth0 will be up again). If you want a permanent config u must modify the network-scripts files.
#9
Allan:
7 months ago
#157795
Allan Mon May 30, 2011 7:07 am
Verry good ... thanks friend...
#10
Denish:
5 months ago
#165260
Denish Tue Aug 23, 2011 7:53 am
You have created virtual interface not the virtual IP .The heading is not correct.Please correct it
#11
Ken:
1 month ago
#172205
Ken Tue Dec 20, 2011 4:45 pm
As mentioned above, if you add an IP or gateway at the command line like:

ifconfig -a eth0:1 <ip address>

route add default gateway <ip addres> eth1

it will not survive a reboot, you need to configure it in the cfg files mentioned or 'network' file for the gateway to survive a reboot.


Also "virtual IP" sounds funny but is technically correct although it sounds funny. It is a VIP (virtual IP) off an existing interface.

#12
Maik:
3 minutes ago
#174771
Maik Wed Jan 25, 2012 8:58 pm
This is NOT a virtual IP address (read: one address shared by two hosts) it's a virtual NIC...
Leave Your Comments...
Share
| More