As shown in the following figure, there are two networks, network segment 0 and network segment 2, router 1 forms 0 network segment, and router 2 separates 2 network segments from router
If you want to communicate directly between different network segments, you need to add a route, such as the Linux route add command:
route [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]
add: Add a routing rule
del : Deletes a routing rule
-net : The destination address is a network
-host : The destination address is a host
target: The destination network or host
netmask : The netmask of the destination address
GW: The gateway through which the packet is routed
dev : The network interface specified for the route
1. Add host routes
If you want the 192.168.2.10 host to ping the 192.168.0.8 host, you need to go through Router 2 and add a route to 192.168.0.8 on the 192.168.2.10 host, and the following command is added:
route add -host 192.168.0.8 gw 192.168.2.1 dev eth0
The meaning of this command is that hosts accessing 192.168.0.8 messages are forwarded from port 192.168.2.1. Run the route command to view the added routes
Destination | Target network or target host. If the destination is default(0.0.0.0), it means that this is the default gateway and all data is sent to this gateway (10.139.128.1 here) |
Gateway | Gateway address, 0.0.0.0 indicates that the destination corresponding to the current record is in the same network segment as the local machine, and does not need to go through the gateway for communication (the communication between two hosts in the same LAN does not need to go through the gateway) |
Genmask | The netmask of the Destination field needs to be set to 255.255.255.255 when the destination is the host, and 0.0.0.0 when the default route is the destination |
Flags | sign
|
Metric | The routing distance, the number of transfers required to reach a given network, is required for large LAN and WAN setups (not used in the Linux kernel.) ) |
Ref | Number of route item references (not used in the Linux kernel.) ) |
Use | The number of times this route item was looked up by routing software |
Iface | NIC name, e.g. eth0; Which network interface is required to get to this network segment. That is, eth0 is the network card |
The first route information means that the information about the 192.168.0.8 access host is forwarded from the 192.168.2.1 gateway.
To delete this route, simply execute: route del 192.168.0.8
2. Add a network route
The first way to add a host route can only access one directory host, if 192.168.2.10 wants to access all the hosts in the 0 network segment, it is obviously very troublesome to add the 0 network segment hosts again, by adding the network route, as long as you add a 0 network route on the 192.168.0.10 host, the add command is as follows:
route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth0
This target is the network, so you need to set the subnet mask and use the route command to check the added network routes
This route indicates that all messages on CIDR block 0 are forwarded from the 192.168.2.1 gateway
删除网络路由:route del -net 192.168.0.0/24 gw 192.168.2.1
3. Add a default route
如果2网段主机想访问其他所以网段的网络,只需要添加默认路由即可:route add default gw 192.168.2.1 dev eth0
The default route means that all information accessing non-2 CIDR blocks is forwarded from 192.168.2.1
删除默认路由:route del default
Link: https://www.cnblogs.com/YYFaGe/p/16599675.html
(The copyright belongs to the original author, invaded and deleted)
Pay attention to the good of the industry: IT operation and maintenance base camp, and get the 60 G "Network Engineering System Gift Package"