I’m beginning understanding network concepts and tools. I setup three VM’s with the following configuration:
A: 192.168.1.3 B: 192.168.1.1 192.168.2.1 C: 192.168.2.2
In A, I ping 192.168.1.1 and it pongs. I think A has no idea where 192.168.2.1 is located. So, when I ping 192.168.2.1, it doesn’t know to what interface it should be routed. My first question is that when there’s only one interface, why it wonders? 😀
Anyway, for route to take occur, in A I add the following:sudo route add default enp0s3
I may use the following commands instead:
sudo route add default gw 192.168.1.1 sudo route add default gw 192.168.1.3
- Am I right in the commands?
- The last command also works! Why?! Is it correct?! How?!
With one of these commands A may ping 192.168.2.1 too. Then, I go to ping C from A. I expect the ping packet when received by B, it knows how to route it and route it to C via 192.168.2.x interface, but it doesn’t.
- Why? Should I do something in B?
- What should I do in B for the ping to get response from C?
Answer
In order to achieve what you would like to have you need to do:
On A, you need to configure the route to 192.168.2.X, that is via 192.168.1.1:
ip route add 192.168.2.0/24 via 192.168.1.1
Note: you can specify a device name with dev DEVICE_NAME
or you can omit it in this case.
On B, you need to make sure that packets can be forwarded e.g. ip_forwaring is enabled:
sysctl -w net.ipv4.ip_forward=1
or
echo 1 > /proc/sys/net/ipv4/ip_forward
In addition, you need to allow the traffic between the two networks in the firewall. So something like the following command should to the job:
iptables -I FORWARD -s 192.168.1.0/24 -d 192.168.2.0/24 -j ACCEPT
iptables -I FORWARD -s 192.168.2.0/24 -d 192.168.1.0/24 -j ACCEPT
B has two interfaces (192.168.1.1 and 192.168.2.1) and it already knows where to find both subnets. So in this case you don’t need to add additional routes on it.
On C, same as on A, you need to tell 192.168.2.2 how to reach 192.168.1.X, that is via 192.168.2.1:
ip route add 192.168.1.0/24 via 192.168.2.1
And finally, you have to make all these changes permanent, because if you now reboot these nodes, the configuration which you have applied will be gone. How to achieve this depends on the Linux distribution you are using and is not in the scope of your current question 🙂
Attribution
Source : Link , Question Author : hamidi , Answer Author : basekat