Metric can change on several place.
Best working for me is configuration in etc/network/interface
auto lo
iface lo inet loopback
auto new0
iface new0 inet static
address xxx.xxx.xxx.xxx
netmask xxx.xxx.xxx.xxx
up route add default gw aaa.aaa.aaa.aaa metric 10
down route del default gw aaa.aaa.aaa.aaa
auto new1
iface new1 inet static
address yyy.yyy.yyy.yyy
netmask yyy.yyy.yyy.yyy
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.
Edit 1
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 ssh
packets.
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 iptables
.
service iptables save
service iptables restart
To delete this entry from iptables
we can use -D
instead of -A
.
Next, create a new IP route table in /etc/iproute2/rt_table
by just giving an entry
100 sshtable
Write rule for ssh
packets.
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 sshtable
sudo ip route add default via 192.168.1.1 table sshtable
Use ip route show table sshtable
to show all routes at sshtable
.
That’s it..we have done it!!!
Repeat everything for imap
on port 465
or hhtp
on port 80
or https
on port 443
...