How to add virtual ip address, subnet mask, gateway in Linux

Run command to display ip address

$admin@adminpc:~$ ifconfig

admin@adminpc:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:78:9d:7a
          inet addr:192.168.0.102  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe78:9d7a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3141 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1736 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3934535 (3.9 MB)  TX bytes:177104 (177.1 KB)
          Interrupt:19 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:44 errors:0 dropped:0 overruns:0 frame:0
          TX packets:44 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3590 (3.5 KB)  TX bytes:3590 (3.5 KB)

Currently i am having one interface with eth0 interface which ip address is  192.168.0.102 . Now i have requirement to add one more virtual ip address in system. IP address can be same subnet or different depend on requirement.

Now run command to add virtual ip address

In Ubuntu system
$ sudo ifconfig eth0:1 192.168.0.103

In RHEL/Centos system
# ifconfig eth0:1 192.168.0.103

Now you can see the output of ifconfig command, a new ip address is address virtually, ip address added temporally, you can set this ip address permanent by create networking file.

Add ip address, subnet mask together

$ sudo ifconfig eth0:1 192.168.0.103 netmask 255.255.255.0

or,
# ifconfig eth0:1 192.168.0.103 netmask 255.255.255.0