Below is the “classic” way to do it with the current Linux networking stack (iproute2 + iptables/nftables).
Pick the variant that matches what you really mean:
- “Send EVERY packet whose DESTINATION is in 192.0.2.0/24 out of interface eth1”
That’s just normal routing; no policy-routing magic needed.
# Destination network 192.0.2.0/24 should leave via eth1
ip route add 192.0.2.0/24 dev eth1 # no gateway
# …or, if the network is behind a router reachable on eth1:
ip route add 192.0.2.0/24 via 10.5.5.1 dev eth1
That’s it.
Whenever the kernel has to send a packet whose destination is in 192.0.2.0/24 it will pick eth1.
- “Send packets whose SOURCE address is in 10.50.0.0/16 out of interface eth1”
(a.k.a. Source-based / policy routing)
Step-by-step:
- Pick a new routing-table number & name it
(once, system-wide)
echo "100 special" >> /etc/iproute2/rt_tables
- Fill that new table with the routes that should be used
when the rule matches. Example: “anything -> default gateway on eth1”
# default route in the new table
ip route add default via 192.168.2.1 dev eth1 table special
# (add other networks if you want finer control)
- Install a rule that says “if the packet’s source is 10.50.0.0/16,
evaluate table ‘special’ first”.
ip rule add from 10.50.0.0/16 table special
# optional: give it a preference/priority
# ip rule add pref 100 from 10.50.0.0/16 table special
- Check your work:
ip rule # shows the rules in order
ip route show table special
- Persist on boot
• Put the ip route … table special and ip rule … lines into a distro-specific init script
• or use NetworkManager/Netplan/ifupdown “routing-table” directives.
- “Mark packets with iptables/nft and route on the mark”
Useful when you want to match more than just “source” or “dest” (e.g. TCP port, UID, …).
iptables (mangle) + fwmark:
# 1. Mark any packet whose destination is 203.0.113.0/24
iptables -t mangle -A PREROUTING -d 203.0.113.0/24 -j MARK --set-mark 0x10
# 2. Create a table that routes via eth1
echo "200 eth1tbl" >> /etc/iproute2/rt_tables
ip route add default via 192.168.2.1 dev eth1 table eth1tbl
# 3. Tell the kernel to use that table when mark == 0x10
ip rule add fwmark 0x10 table eth1tbl
Same idea with nftables:
table inet mangle {
chain prerouting {
type filter hook prerouting priority mangle;
ip daddr 203.0.113.0/24 meta mark set 0x10
}
}
# then the same ip rule / table as above
- Don’t forget the little things
• Forwarding:
echo 1 > /proc/sys/net/ipv4/ip_forward (or sysctl net.ipv4.ip_forward=1)
• rp_filter:
If you’re doing asymmetric routing, disable reverse-path filtering on the interfaces involved (sysctl net.ipv4.conf.eth1.rp_filter=0).
• Firewalls:
Make sure your firewall allows the packets in/out of the chosen interface.
• Testing:
ip route get <dest> is invaluable to see which interface the kernel will pick for a given destination + source.
TL;DR
• For destination-based decisions, a simple ip route add <net> dev <iface> is enough.
• For source-based or “anything more fancy”, use policy routing: create a new table, add an ip rule that selects it (by from, to, or fwmark), and populate that table with the routes you want.