How to route only specific subnet (source ip) to a particular interface?
125,742
Solution 1
You need to use policy based routing. Something kind of like
ip rule add from <source>/<mask> table <name>
ip route add 1.2.3.4/24 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 1.2.3.4/24
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.
Solution 2
http://wiki.wlug.org.nz/SourceBasedRouting
This site has a nice example of source based routing.
Related videos on Youtube
Author by
Alex
Updated on September 18, 2022Comments
-
Alex over 1 year
How to route only specific subnet (source ip) to a particular interface?
OS: LinuxI know I can do easily by destination IP by using something like
route add 1.2.3.4/24 dev eth4
but I do not see how can route based on source IP.
-
Alex over 12 yearsone 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... alsodefault
can be replaced by a subnet, just like in your example) -
DavidPostill over 3 yearsPlease quote the essential parts of the answer from the reference link(s), as the answer can become invalid if the linked page(s) change.