I have a computer that has connected to two routers via their internal network. I have forwarded port 80 to my computer on both routers.
RouterA:80 -> 10.0.0.10:80 works. RouterB:80 -> 192.168.1.10:80 works.
So routers' port forwarding config is correct.
However when I connect to both network only RouterA's port forwarding works. How can I make RouterB's port forwarding work? Please take look at my computer network config.
network: ethernets: enp7s0: addresses: - 10.0.0.10/24 gateway4: 10.0.0.1 nameservers: addresses: - 10.0.0.1 enp6s0: addresses: - 192.168.1.10/24 gateway4: 192.168.1.1 nameservers: {} version: 2I don't need to use enp6s0's gateway but it seems like it needed for port forwarding.
Thanks to @raj my updated network config.
NOTE: This is not exactly what I asked but if you have different IP range accessing your routers it would work. Fortunately that was my case.
enp7s0: addresses: - 10.0.0.10/24 routes: - to: 0.0.0.0/0 #default route via: 10.0.0.1 metric: 100 nameservers: addresses: - 10.0.0.1 enp6s0: addresses: - 192.168.1.10/24 routes: - to: A.B.C.D/24 # IP range of network that would access Router B via: 192.168.1.1 metric: 10 nameservers: {} 1 Answer
Generally, the networking doesn't work very good with two default gateways defined. Your computer is using only one gateway by default (probably 10.0.0.1 in this case) and will use the second one only when the first one is unreachable.
So port forwarding on RouterB probably is working, that is, packets sent to RouterB:80 probably reach interface 192.168.1.10 on your machine. But the reply packets are sent always to gateway 10.0.0.1 and probably don't get back to destination (this is so called "asymmetric routing" which is generally troublesome).
If you plan to connect to RouterA:80 and RouterB:80 from the same range of IP addresses, I don't see a simple solution to your problem. Maybe it is possible to tweak iptables somehow so that replies to packets received via interface enp6s0 always go out via 192.168.1.1, but I don't know how to do it (if possible at all).
If the IP addresses will be different, you should not define 192.168.1.1 as a default gateway, but instead add to routing table a route to particular network(s) via 192.168.1.1. That takes precedence over the default gateway, so packets to specified network(s) will be sent via 192.168.1.1 and not via 10.0.0.1.
1