ROUTING , SWITCH & GATEWAY
The important components of computer network are — ROUTER , SWITCH & GATEWAY.
SWITCHING:-
Enables the communication only with Network.
Here in below diagram, A computer can connect to B computer via a switch. To connect to switch we need interface on each hosts (eth0).
Computer A can receive a packet from Computer B, and similarly can send the packet to host B. All this is possible within a same network only.
ROUTER:-
How does a computer or host in one network reach the computer or hosts in another network. This is where ROUTER comes into play. A router helps 2 network connect together.
It’s is just another device or a server with many network ports.
In this below image- The router connects to 2 separate networks, hence it gets 2 IP assigned. 192.168.1.1 on A side and 192.168.2.1 on B side. Now, this router can enable communication between this 2 network. A settings needs to be done at each hosts to connect to router. This is where Gateway configuration is required.
GATEWAY:-
When a system A sends packets to system B how does it knows where the router is in the network. This is where we configured a system with a Gateway or a Route.
Suppose if a network is a room, then a gateway is door to outside world to other network or internet. A system need to know where the door is to go through that.
To see existing routing configuration in System:
Run the command-
- Route — it displays kernel routing table.
Below image is output of Route command, currently there is no routing configuration. So, in this case the System A cannot reach System B. It can only reach system within same network.
To configured gateway on System A to reach a Systems on another network (192.168.2.0) we need to add routing details.
Run the command- ip route add 192.168.2.0/24 via 192.168.1.1
After the command is executed, running route command will show routing details. With this configuration, System A can now reach to any system on a network 192.168.2.0 ( System B). The same setup needs to be configured on all system.
Similarly, for System B to connect to system A, we need to perform similar configuration.
We can add any other network in same way- below is just an example
ip route add 170.214.180.0/24 via 192.168.2.1
ip route add 214.125.50.0/24 via 192.168.2.1
ip route add 14.55.50.0/24 via 192.168.2.1
So, instead of adding all network individually for the each of these network, we can also setup a single configuration for routing,
ip route add default via 192.168.2.1
The default or 0.0.0.0 (any IP destination) mean same thing. So, both the below lines mean one and same thing.
In the above image under gateway column- 0.0.0.0 means you don’t need a gateway. In this above example for System B to access any devices in network -192.168.2.0 it doesn’t need gateway as it’s on own network.