5

This is probably a silly question.
I'm doing some http requests using wget from the command line, and I want those connections to be made through one specific IP of the 4 IPs my server has.
Those http requests go to one specific range of IPs so I only want those to be routed differently.
The 4 interfaces in my server are eth0, eth0:0, eth0:1, eth0:2.

I tried with the following command:
route add -net 192.164.10.0/24 dev eth0:0

But when I see the routing table it says:

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.164.10.0    0.0.0.0         255.255.255.0   U         0 0          0 eth0

The interface is set to eth0 not eth0:0 as my command says.

What am I doing wrong?

GetFree
  • 1,460
  • 7
  • 23
  • 37

3 Answers3

9

wget --bind-address=192.0.2.116 http://file from man wget

Chris S
  • 77,337
  • 11
  • 120
  • 212
  • Ok, but that's a `wget` specific solution. How can I make the routing work? – GetFree Apr 06 '13 at 03:53
  • 2
    We aren't exactly talking about _routing_, because it's all about pseudo interfaces. You are using one interface and all requests go the same route - regardless of the source IP address which is defined in application code. Unless specifically designated, client TCP sockets are bound to an interface's default address. – Lukas Apr 06 '13 at 04:00
  • You know it's a long day when you see a mod's answer auto-flagged for the LQP queue. – Andrew B Apr 06 '13 at 04:19
  • I thought you could use the virtual interfaces like any other. Apparently you cannot. – GetFree Apr 06 '13 at 07:28
3

If it's not wget, then there's this nifty little wrapper that you use LD_PRELOAD to insert before you run any command.

You can use that to specifically bind, say, a browser to a VLAN interface (speaking from experience).

Tom O'Connor
  • 27,440
  • 10
  • 72
  • 148
0

You could try to setup PBR (policy base routing) if you need more flexible routing (instead of --bind-address)

IP1='xxx.xxx.xxx.62'
IF1='eth0'
GW1='xxx.xxx.xxx.61'
P1_NET='xxx.xxx.xxx.60/30'

IP2='yyy.yyy.yyy.10'
IF2='eth0:0'
GW2='yyy.yyy.yyy.9'
P2_NET='yyy.yyy.yyy.8/30'

IP3='zzz.zzz.zzz.239'
IF3='eth0:1'
GW3='zzz.zzz.zzz.254'
P3_NET='zzz.zzz.zzz.0/24'

/sbin/ip route add $P1_NET dev $IF1 src $IP1 table ISP1
/sbin/ip route add default via $GW1 table ISP1

/sbin/ip route add $P2_NET dev $IF2 src $IP2 table ISP2
/sbin/ip route add default via $GW2 table ISP2

/sbin/ip route add $P3_NET dev $IF3 src $IP3 table ISP3
/sbin/ip route add default via $GW3 table ISP3

/sbin/ip rule add from $IP1 table ISP1
/sbin/ip rule add from $IP2 table ISP2
/sbin/ip rule add from $IP3 table ISP3

/sbin/ip ru add fwmark 1 lookup ISP1 prio 100
/sbin/ip ru add fwmark 2 lookup ISP2 prio 200
/sbin/ip ru add fwmark 3 lookup ISP3 prio 300

/sbin/ip route add default via $GW1
/sbin/ip route flush cache

And then use something like following

iptables -t mangle -I OUTPUT -p tcp --dport 80 -j MARK --set-mark 1
ALex_hha
  • 7,025
  • 1
  • 23
  • 39