How to route only specific subnet (source ip) to a particular interface?
OS: Linux

I know I can do easily by destination IP by using something like

route add dev eth4

but I do not see how can route based on source IP.

  • This is called policy based routing, and is implementation dependent. What device/OS are you wanting to do this on? – Paul Jan 10 '12 at 2:38
  • @Paul: the OS is Linux, I edited the question – Alex Jan 10 '12 at 3:33
up vote 24 down vote accepted

You need to use policy based routing. Something kind of like

ip rule add from <source>/<mask> table <name>
ip route add via <router> dev eth4 table <name>

<name> is either table name specified in /etc/iproute2/rt_tables or you can use numeric id ...

This pretty much says, that all traffic from will be routed using routing table <name>. IIRC it doesen't use the default table after going through this, so if you need other routes (ie. default gateway), you need to add them to the table as well.

  • I was about to add my own answer after some research and testing and Fox, you beat me to it with exactly what I found :) – Alex Jan 12 '12 at 2:19
  • 1
    one correction (but not sure if your one will also work. for the second command: ip route add default dev eth4 table <name> (table directive should go at the end I think, just like in the first command... also default can be replaced by a subnet, just like in your example) – Alex Jan 12 '12 at 2:23

This site has a nice example of source based routing.

  • yes, nice and detailed article, a little bit complex for my simple need – Alex Jan 12 '12 at 2:21

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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