Showing posts with label secondary ip. Show all posts
Showing posts with label secondary ip. Show all posts

Thursday, November 3, 2016

Set Multiple IP Addresses on the same NIC (Network Interface Card) on Linux

At times we need multiple IP addresses. But what if we have only a single network interface card (NIC)? Well, we can set up multiple IP addresses on the same NIC. Follow along to see how.

Let's say I have an Ethernet interface enp0s3 with the IP address 10.0.2.15. Now I want to add another IP address to the interface. Let's say the new IP address is 10.0.2.18.

Before Adding the Secondary IP Address:

sudo ip addr show enp0s3
2: enp0s3:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:45:4e:87 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 86402sec preferred_lft 86402sec
    inet6 fe80::a00:27ff:fe45:4e87/64 scope link 
       valid_lft forever preferred_lft forever

To add another IP address, use the following command,

sudo ip addr add dev enp0s3 10.0.2.18/24

Here 10.0.2.18 is the new IP address and /24 is the subnet mask. enp0s3 is the interface that we want the IP address to be added to.


After Adding the Secondary IP Address:

sudo ip addr show enp0s3
2: enp0s3:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:45:4e:87 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 86219sec preferred_lft 86219sec
    inet 10.0.2.18/24 scope global secondary enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe45:4e87/64 scope link 
       valid_lft forever preferred_lft forever

From the output, you can see that the new IP address has been added. Now let's try to ping to the IP addresses.

Checking Connectivity:

ping 10.0.2.15
PING 10.0.2.15 (10.0.2.15) 56(84) bytes of data.
64 bytes from 10.0.2.15: icmp_seq=1 ttl=64 time=0.098 ms
64 bytes from 10.0.2.15: icmp_seq=2 ttl=64 time=0.132 ms
64 bytes from 10.0.2.15: icmp_seq=3 ttl=64 time=0.065 ms
64 bytes from 10.0.2.15: icmp_seq=4 ttl=64 time=0.239 ms
64 bytes from 10.0.2.15: icmp_seq=5 ttl=64 time=0.054 ms
64 bytes from 10.0.2.15: icmp_seq=6 ttl=64 time=0.053 ms
64 bytes from 10.0.2.15: icmp_seq=7 ttl=64 time=0.121 ms
64 bytes from 10.0.2.15: icmp_seq=8 ttl=64 time=0.124 ms
^C
--- 10.0.2.15 ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7007ms
rtt min/avg/max/mdev = 0.053/0.110/0.239/0.058 ms
ping 10.0.2.18
PING 10.0.2.18 (10.0.2.18) 56(84) bytes of data.
64 bytes from 10.0.2.18: icmp_seq=1 ttl=64 time=0.117 ms
64 bytes from 10.0.2.18: icmp_seq=2 ttl=64 time=0.113 ms
64 bytes from 10.0.2.18: icmp_seq=3 ttl=64 time=0.111 ms
64 bytes from 10.0.2.18: icmp_seq=4 ttl=64 time=0.134 ms
64 bytes from 10.0.2.18: icmp_seq=5 ttl=64 time=0.113 ms
^C
--- 10.0.2.18 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.111/0.117/0.134/0.014 ms

Both the IP addresses can be pinged. Everything is working as expected.