Two gateways on same network
My machine runs Ubuntu Server 16.04 and it has two NICs, eth0 and eth1, as shown in the network diagram. Its role was initially to route traffic from the 192.168.50.0/24 subnet to the 192.168.1.0/24 subnet where the DSL router is that connects to the internet. So my network configuration is
Output of (eth0) cat /etc/network/interfaces.d/eth0
#auto eth0
#iface eth0 inet dhcp
auto eth0
iface eth0 inet static
address 192.168.1.7
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 192.168.1.1Output of (eth1) cat /etc/network/interfaces.d/eth1
#auto eth0
#iface eth0 inet dhcp
auto eth1
iface eth1 inet static
address 192.168.50.7
netmask 255.255.255.0Now I added another cellular router which also connects to the internet, and it's connected on the 192.168.50.0/24 subnet. I need to route some of the internet traffic through that cellular router, but not all of it (e.g. ssh connections) Any ideas on how to achieve this?
31 Answer
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 80 -j MARK --set-mark 0x1We are marking all packets with destination port 22 as 0x1 .
Now save and restart iptables.
sudo service iptables save
sudo service iptables restartNext, create a new IP route table in /etc/iproute2/rt_tables by just giving an entry
100 sshtableWrite rule for ssh packets.
ip rule add fwmark 0x1 lookup sshtableAdd route at new table sshtable. All other traffic will go through the
default gateway, 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; doneAdd default gateway entry for ssh packets to table sshtable
sudo ip route add default gw 192.168.50.254 table sshtableUse ip route show table sshtable to show all routes at sshtable.
Try.