Showing posts with label show. Show all posts
Showing posts with label show. 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.



Find the IP Address of a Specific Network Interface on Linux

To Find the IP Address of a specific network interface on Linux, use the following command,

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 85650sec preferred_lft 85650sec
    inet6 fe80::a00:27ff:fe45:4e87/64 scope link 
       valid_lft forever preferred_lft forever

Here enp0s3 is the network interface that I want to see the address of.

The output shows that, enp0s3 has an ipv4 address of 10.0.2.15 and an ipv6 address of fe80::a00:27ff:fe45:4e87



Find the IP Addresses of the Available Network Interfaces of your Linux System

To find the ip addresses of the available network interfaces on Linux, use the following command,

sudo ip addr show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
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 86403sec preferred_lft 86403sec
    inet6 fe80::a00:27ff:fe45:4e87/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0:  mtu 1500 qdisc noqueue state DOWN 
    link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic:  mtu 1500 qdisc pfifo_fast state DOWN qlen 500
    link/ether 52:54:00:bb:2e:64 brd ff:ff:ff:ff:ff:ff

You can see that, the result shows the network interfaces with numbers 1,2,3,4 and so on. I have 4 network interfaces on my Linux system.

You can see lines that start with inet or inet6. These lines represent ipv4 and ipv6 addresses respectively.

The first network interface which is a loopback interface has an ipv4 address of 127.0.0.1 and an ipv6 address of ::1

The second network interface enp0s3 has an ipv4 address of 10.0.2.15 and an ipv6 address of fe80::a00:27ff:fe45:4e87

The third network interface virbr0 has an ipv4 address of 192.168.122.1. It doesn't have an ipv6 address set.

The fourth network interface virbr0-nic doesn't have any ip address set at the moment.



Find the Hardware Address or MAC address of the Available Interfaces of your Linux System

To find the hardware address or MAC address of the available network interfaces on Linux, use the following command,

sudo ip link show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 08:00:27:45:4e:87 brd ff:ff:ff:ff:ff:ff
3: virbr0:  mtu 1500 qdisc noqueue state DOWN mode DEFAULT 
    link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
4: virbr0-nic:  mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 500
    link/ether 52:54:00:bb:2e:64 brd ff:ff:ff:ff:ff:ff

You can see that, the result shows the network interfaces with numbers 1,2,3,4 and so on. I have 4 network interfaces on my Linux system. The first one is lo, which is a loopback interface with the hardware address 00:00:00:00:00:00. The second one is enp0s3, which is the ethernet interface with the hardware address 08:00:27:45:4e:87. The third one, virbr0, is a bridge interface with the MAC address 00:00:00:00:00:00. The fourth one is virbr0-nic, which is another bridge interface with the MAC address 52:54:00:bb:2e:64