Why does curl not work with non-default network interface in CentOS 6?

1

curl can use non-default network interface with its --interface option theoretically. However, in practise it doesn't work. I have 2 network interfaces eth0 and eth1. Each one is connected to Internet via its own router and uses DHCP for autoconfiguration. Lets say eth0 IP is ip0 and eth1 IP is ip1. So, I set eth0 as default connection via /etc/sysconfig/network-scripts and run following command:

curl --url "http://ip-api.com/json"

I get JSON reply where I see that actual external IP is ip0. Now I set eth1 instead of eth0 as default interface, and the same command returns me ip1.

Now I set eth0 as default IP again and run following command:

curl --url "http://ip-api.com/json" --interface "eth0"

No problems, it returns ip0.

And finally:

curl --url "http://ip-api.com/json" --interface "eth1"

results in following error:

curl: (7) couldn't connect to host

As we can see from previous test (where eth1 was default interface), eth1 has not issues. It is very reliable wired connection actually, so the issue is not related to any network stability problems.

My routing table follows:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.85.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.182.0   0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth2
169.254.0.0     0.0.0.0         255.255.0.0     U     1004   0        0 eth2
0.0.0.0         192.168.182.2   0.0.0.0         UG    0      0        0 eth0
0.0.0.0         192.168.85.1    0.0.0.0         UG    1      0        0 eth1

Can anyone explain why curl doesn't work with non-default interface?

Vitalii

Posted 2017-06-23T06:51:12.840

Reputation: 115

Please show you routing table – Gerald Schneider – 2017-06-23T06:53:47.267

Answers

4

The --interface options is used to figure determine what address on the system will be used as the source IP. It doesn't magically change anything about routing.

A typical system will only have a single default gateway. Naively connecting two ports two different networks using only DHCP simply is not going to give you a working multi-homed system. One of the interfaces will work to connect to the internet and one will only work for the subnet. Trying to bind like you have done is going to just send packets through the wrong router.

On Linux it is possible to get this working, but it requires multiple route tables, and rules to define which table to use. See this howto.

http://lartc.org/howto/lartc.rpdb.html

Zoredache

Posted 2017-06-23T06:51:12.840

Reputation: 18 453

I can confirm that above commands started to work as expected after I added 2 custom routing tables (one per network interface) and specific correct default gateway for each custom table. – Vitalii – 2017-06-23T11:12:40.187