Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a Linux webserver with assigned to eth0 and assigned to eth1. I must not change this. Here's the routing so far: dev eth1 proto kernel scope link src dev eth0 proto kernel scope link src via dev eth1
default via dev eth0

The default route leads to the Internet which the machine needs access to for downloading stuff. Now there are also packets (requests) coming in from the Internet via How do I have to set up routing tables and rules in order to answer those requests? iptables is not possible here.

Update: Seems like this works, but it doesn't look elegant to me:

ip r a dev eth1 table 10
ip r a via t 10
ip r a default via t 10
ip rule add from t 10
share|improve this question
What doesn't look elegant about it? You create a second table, and select it based on source address. – derobert Jan 4 '13 at 18:43
I would also add selection according to specified output interface (e.g. ip rule add oif eth1 t 10). – BatchyX Feb 3 '13 at 20:35

This is called simple source-based routing, and it's covered in the LARTC HOWTO:


If you find the copy&paste of the contents of the original routing table (sans its default gateway) inelegant, you don't have to do that, you can use a trivial script:

ip route flush table 10
ip route show table main | grep -v ^default | while read ROUTE; do ip route add table 10 $ROUTE; done
ip route add table 10 default via
ip rule add from table 10
ip route flush cache
share|improve this answer

I think you can't.

When applications (web servers) reply, these are new packets for the kernel and it will just follow a routing table. You can't have multiple default gateways and choose one depending on what interface a packet arrived that triggered the application to send this response.

Please, just use one default gateway. Don't do NAT on multiple subnets like this. Your network design is faulty here. Really.

And if you do, then don't expect that port forwarding can do magic here.

share|improve this answer
"You can't have multiple default gateways and choose one depending on what interface a packet arrived that triggered the application to send this response." Sure you can. Linux routing can consider source address. And even without that, if OP didn't have the "no iptables" restriction, connection tracking lets you do it. And this isn't necessarily a faulty network design, depends on what the restrictions are that led to it. – derobert Jan 4 '13 at 18:25
@ downvoter: instead of downvoting, please post an answer. – gertvdijk Feb 3 '13 at 21:32
@gertvdijk: The solution is in the question already. – BatchyX Feb 10 '13 at 21:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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