4

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

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

Now 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?

Network diagram

Panos
  • 299

1 Answers1

4

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 0x1

We are marking all packets with destination port 22 as 0x1 .

Now save and restart iptables.

sudo service iptables save
sudo service iptables restart

Next, create a new IP route table in /etc/iproute2/rt_tables by just giving an entry

100 sshtable

Write rule for ssh packets.

ip rule add fwmark 0x1 lookup sshtable

Add 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; done

Add default gateway entry for ssh packets to table sshtable

sudo ip route add default gw 192.168.50.254 table sshtable

Use ip route show table sshtable to show all routes at sshtable.

Try.

2707974
  • 10,758