I want to decide on application level which gateway to use to connect to the internet.

It can't be done by simple subnet rules, because it is really the application which should be able to decide which gateway to use.

E.g. I want to use curl to download something and I want to specify somehow which gateway to use.

It is a Linux box having only one network card, IP address There are two or more gateways (different ISPs): e.g. and

Is it possible to set up routing rules with iptables and iproute2 to manage that? How do I tell an application which route to use?

I tried this:
I added in /etc/iproute2/rt_tables: 100 out2
ip rule add prio 200 from lookup out2
ip route add dev eth0 src table out2
ip route add default via dev eth0 src table out2
Still "telnet -b www.google.com 80" returns "network is unreachable"

  • Maybe using owner-matching with --uid-owner in iptables rules?
    – pintpint
    Jan 6, 2012 at 16:31

2 Answers 2


I can think of one way to do it which is as follows.

  1. Define two IP addresses on your NIC like & This should be easy and straight-forward. This can be done using ifconfig.
  2. Configure source based routing such that each source IP is routed to one of the default gateways. This link can help you.
  3. Finally, you can tell your application to use bind one of the addresses or This way you can choose one of the gateways depending on the source IP selected. To test this, you can use telnet with -b source_ip option.

Here are the commands I used previously to enable source-based routing:

$ sudo ip rule add from tab 1 priority 500
$ sudo ip route add default via dev eth0 tab 1
$ sudo ip route flush cache

If you have the default gateway set to, then this should work for you. The packets sent from should be sent to default gateway, and packets sent from should be sent to 2nd gateway as instructed above.

  • @pintpint: It is hard to read such information from a comment. You can edit your question instead. It will be better and easier to read. Did you forget to add the port 80 at the end of telnet command?
    – Khaled
    Jan 9, 2012 at 13:29
  • Sorry, I added it to my original question now...
    – pintpint
    Jan 9, 2012 at 13:32
  • Does the described setup really work for you? I think one problem might be that I have two IP addresses but only one NIC. Routing would be easier if I had two NICs. But that would not solve the problem because it is not scalable.
    – pintpint
    Jan 10, 2012 at 9:53
  • @pintpint: I think the tricky part is related to the source-based routing. I tried point 2 before and it worked for me. The other points should be easy.
    – Khaled
    Jan 10, 2012 at 9:58
  • What do you mean by "point 2"?
    – pintpint
    Jan 10, 2012 at 10:01

Another option. A lot of CLI utilitise use environment variables to do this type of configuration, http_proxy may be relevant to your situation, http://www.cyberciti.biz/faq/linux-unix-set-proxy-environment-variable/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.