Skip to main content

linux - How to route only specific subnet (source ip) to a particular interface?


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 1.2.3.4/24 dev eth4


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



Answer



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


ip rule add from / table 
ip route add 1.2.3.4/24 via dev eth4 table

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 . 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.


Comments