Search code examples
azurenetwork-programmingnic

Access internet with second (or multiple) NIC in Azure


I have a VM in Azure with Ubuntu 19.04 and 4 NICs, each one has a public IP attached to it, as an example, this is the second NIC:

enter image description here

As you can see, it has public IP 191.234.186.19.

My objective is to be able to access the internet from each NIC with a different IP address, but inside the VM I'm only able to access it with the first network eth0.

Looking at # route -n I get this output:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.1        0.0.0.0         UG    100    0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth2
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth3
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
168.63.129.16   10.0.0.1        255.255.255.255 UGH   100    0        0 eth0
169.254.169.254 10.0.0.1        255.255.255.255 UGH   100    0        0 eth0

I tried to add routes for the other NICs with the following commands:

sudo ip route add default via 10.0.0.1 dev eth1 metric 101
sudo ip route add default via 10.0.0.1 dev eth2 metric 102
sudo ip route add default via 10.0.0.1 dev eth3 metric 103

But again I still can't connect to the internet with these NICs, only eth0.

Am I missing something? Should I need to change/configure anything else to make this work?

For completeness, I'm using this command to test the connectivity:

curl --interface eth1 api.ipify.org?format=json -w "\n"

And my VM is the same as the one in this article: article


Solution

  • I suspect you might need to 1) define new routing tables for your 2nd, 3rd and 4th interface, 2) add default routes to those tables 3) and define rules to ensure return packets always get routed back into the same interface the originating packets were sent from.

    There are a few examples out there for your reference (for example this and this).

    I'll just point out a couple of points that you might find helpful when setting this up for your specific case: iproute allows you to maintain multiple routeing tables. To instruct the OS to utilise different routing tables you need to define rules that in most simplistic form will enforce the use of routing table corresponding to the interface that initiated the connection.