Metric can change on several place.
Best working for me is configuration in
iface lo inet loopback
iface new0 inet static
up route add default gw aaa.aaa.aaa.aaa metric 10
down route del default gw aaa.aaa.aaa.aaa
iface new1 inet static
up route add default gw bbb.bbb.bbb.bbb
down route del default gw bbb.bbb.bbb.bbb
On this way when interface
new0 go up will bring route to gateway
aaa.aaa.aaa.aaa with metric 10. Interface
new1 bring up route to
bbb.bbb.bbb.bbb with lower metric and traffic will be routed on that interface.
If you do
sudo ifdown new1 on unplug cable from card route will be deleted and route to
aaa.aaa.aaa.aaa will be new best route for traffic.
When again brin int
new1 up route will again be added and
bbb.bbb.bbb.bbb will again be your best route.
Route priority is not the same like route based on destination port.
But I give you example to route traffic for ssh,
port 22 and you can implement for port you need for
Let’s start from the iptable. Here we shall take
ssh as the service for routing. Use
mangle table of iptable for modifying the
sudo iptables -t mangle -A OUTPUT -p tcp –dport 22 -j MARK –set-mark 0×1
We are marking all packets with destination port
22 as ’0×1′ .
Now save and restart
service iptables save
service iptables restart
To delete this entry from
iptables we can use
-D instead of
Next, create a new IP route table in
/etc/iproute2/rt_table by just giving an entry
Write rule for
ip rule add fwmark 0×1 lookup sshtable
Add route at new table
sshtable. Here we shall use ’192.168.1.1′ as the gateway for
ssh. Address ’192.168.1.1′, in this example will be gateway address for
new2 All other traffic will go through the
default gateway aka
new1, which can be seen by IP route show command.
We copy all entries except default gateway entry from main table.
sudo ip route show table main | grep -Ev ^default | while read ROUTE ; do ip route add table sshtable $ROUTE; done
Add default gateway entry for
ssh packets to table
sudo ip route add default via 192.168.1.1 table sshtable
ip route show table sshtable to show all routes at
That’s it..we have done it!!!
Repeat everything for
imap on port
hhtp on port
https on port