Monday, July 28, 2014

Two ways of setting Multiple IP Addresses on one interface.

I want to talk about secondary IP addresses on interface in Ubuntu.

Threre is two ways of assigning multiple IP addresses on one interface.
Most of people use the first one:


First method of setting multiple IP Addresses on interface:

 

With root privileges, edit /etc/network/interfaces:
auto eth0
iface eth0 inet static
       address 172.16.100.1
       netmask 255.255.0.0
       gateway 172.16.0.1
       dns-nameservers 8.8.8.8

auto eth0:0
iface eth0:0 inet static
       address 172.16.100.2
       netmask 255.255.0.0

auto eth0:1
iface eth0:1 inet static
       address 172.16.100.3
       netmask 255.255.0.0

 
Save and restart network service.

You can create up-to 254 aliases on one interface (eth0:X)


The second way to set multiple IP Addresses is to use IP command:


$ip addr add 172.16.100.1/16 dev eth0
$ip addr add 172.16.100.2/16 dev eth0
$ip addr add 172.16.100.3/16 dev eth0
$ip addr add 172.16.100.4/16 dev eth0 label eth0:0     #you can label it 'label eth0:0'
$ip addr show dev eth0

2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 28:d2:44:33:d8:a3 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.204/16 brd 172.16.255.255 scope global eth0   << this is the primary IP Address
       valid_lft forever preferred_lft forever
    inet 172.16.100.1/16 scope global secondary eth0            << this is secondary
       valid_lft forever preferred_lft forever
    inet 172.16.100.2/16 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet 172.16.100.3/16 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet 172.16.100.4/16 scope global secondary eth0:0          << this with label 'eth0:0'
       valid_lft forever preferred_lft forever
    inet6 fe80::2ad2:44ff:fe33:a459/64 scope link 
       valid_lft forever preferred_lft forever



As you can see, in 'ifconfig' output, there is no addresses without label. Only one labeled 'eth0:0'
$ifconfig

eth0      Link encap:Ethernet  HWaddr 28:d2:44:33:a4:59  
          inet addr:172.16.1.204  Bcast:172.16.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:48726 errors:0 dropped:0 overruns:0 frame:0
          TX packets:30127 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:40392826 (40.3 MB)  TX bytes:4955907 (4.9 MB)

eth0:0    Link encap:Ethernet  HWaddr 28:d2:44:33:a4:59           << only labelled is shown. 
          inet addr:172.16.100.4  Bcast:0.0.0.0  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


Want more?
Get it. it's free: 
Linux Advanced Routing & Traffic Control HOWTO


No comments:

Post a Comment