router
The working principle of the router is to realize the path selection of the data packet based on the routing table in the router.
When the router receives a packet, it will read the destination IP address of the packet and match the rules in the routing table according to the destination IP address.
A single router will not be responsible for the entire transmission of the data packet, but is only responsible for forwarding the data packet to the next router to complete the task.
Routing classification
1. Host routing The path to a single host in the network
2. Network routing The path to reach a certain network segment
3. Default route The destination network is 0.0.0.0 Send to the nearest route connected to the Internet
The composition of the routing table
1. target 2.netmask 3.interface 4.gateway
explain:
Destination: The destination path to which the packet is sent
netmask: subnet mask to determine the network ID of the network segment
interface: The exit on this router, similar to specifying which network card on the computer to use
gateway:
1. Direct connection The gateway of the direct connection network does not need to be configured. The direct connection network indicates the network segment directly connected to the interface of the router.
2. Non-direct connection The interface address of the next router adjacent to this router
Routing table configuration
The directly connected network in the router does not need to be manually configured, the system will automatically generate the routing record only for the non-directly connected network.
The routing table not only exists in the router, but also exists in the computer.
When the interfaces are the same and the gateways are the same, these can be set as default routes (configuration as default routes is conditional, that is, the router has only one exit to all non-directly connected network segments)
Usually, we configure the default gateway on the computer to generate a default routing record. If there is a default route in the routing table of the computer host but no default gateway is configured, you can also access the Internet.
However, if there is only a default gateway and no default route, the computer will also not be able to access the Internet.
Routing Operation Commands
add route
route add -net/-host 8.8.8.8 netmask 255.255.255.0 gw 8.8.8.1
route add default gw 8.8.8.1
delete route
route del default gw 8.8.8.1
route del -net/-host 8.8.8.8 netmask 255.255.255.0
When configuring routing table records, all gw must be in the same network segment as the IP on any network card of the current host, otherwise this routing record cannot be added successfully.
Host communication process steps (host A accesses B)
1. Host A first determines whether host B and A are in the same network segment through IP and netmask operations
2. If in the same network segment, obtain B's MAC address through arp protocol and B's IP
3. If it is not in the same network segment, obtain the MAC address of the gateway through the arp protocol and the IP of the gateway
4. The gateway forwards the packet to the next router through B's IP and its own routing table until it goes to the router on the same network segment as B
5. The last router obtains B's MAC address through the arp protocol and B's IP
3. When the data packet is transmitted between routers, only the source MAC address and the destination MAC address are constantly changing.
4. A host without a route cannot communicate with other hosts in the network.
5. In order for two hosts to be able to communicate, there are two levels that need to be connected to each other.
1. Physical layer Both hosts are connected to the Internet with network cables, and the two hosts can be on the same network segment (connected to the same end of a router), or even connected to the same switch.
Even if two hosts are connected to the same switch, if the two hosts are artificially configured with IPs on different network segments, then the two cannot be pinged.
2. Network layer
1. You can set the IP of two hosts to the same network segment
2. You can add the routing records to the network segment of the other host to the routing tables of the two hosts respectively.
6. If the two network cards are physically connected, but the configured IP addresses are not in the same network segment, if the two hosts are to be connected, a routing record without a gateway can be configured.
In this case, there is no need to add a router to communicate across these two network segments, only a routing record needs to be added to the host
route add default dev ens37
route add default dev eth1
7. The user configures the IP gateway for the host. The netmask is essentially to configure routing, and routing is the key to network host communication.
Linux configured as a router
cat /proc/sys/net/ipv4/ip_forward is changed to 1 to enable linux's packet forwarding (routing) function. If it is not enabled, the system will discard packets that are not targeted by itself by default.