View Linux kernel route table
Use the following route command to view the Linux kernel route table.
# routeDestination Gateway Genmask Flags Metric Ref Use Iface192.168.0.0 * 255.255.255.0 U 0 0 0 eth0169.254.0.0 * 255.255.0.0 U 0 0 0 eth0default 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
Route command output description
Three route types
Host Routing
Host Routing is a route record pointing to a single IP address or host name in the routing selection table. The Flags field of the host route is H. For example, in the following example, the local host uses the vro192.168.of IP address 192.168.1.1 to reach the host with IP address 10.0.0.10.
Destination Gateway Genmask Flags Metric Ref Use Iface----------- ------- ------- ----- ------ --- --- -----10.0.0.10 192.168.1.1 255.255.255.255 UH 0 0 0 eth0
Network Routing
A network route is a network that can be accessed by a host. The Flags field of the network route is N. For example, in the following example, the local host forwards packets sent to 192.19.12 to the vro whose IP address is 192.168.1.1.
Destination Gateway Genmask Flags Metric Ref Use Iface----------- ------- ------- ----- ----- --- --- -----192.19.12 192.168.1.1 255.255.255.0 UN 0 0 0 eth0
Default route
When the host cannot find the IP address or network route of the target host in the routing table, the packet is sent to the default route (Default Gateway. The Flags field of the default route is G. For example, in the following example, the default route is the vro with the IP address 192.168.1.1.
Destination Gateway Genmask Flags Metric Ref Use Iface----------- ------- ------- ----- ------ --- --- -----default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
Configure Static Routing
Route command
You can use the route command to set and view route tables. The command format of the kernel route table is as follows:
# Route [add | del] [-net |-host] target [netmask Nm] [gw Gw] [[dev] If]
Where:
* Add: add a routing rule.
* Del: delete a routing rule.
* Net: the destination address is a network.
* Host: the destination address is a host.
* Target: target network or host
* Netmask: network mask of the destination address
* Gw: Gateway used to route data packets
* Dev: the network interface specified for the route
Route command example
Route entry added to the host
# Route add-host 192.168.1.2 dev eth0: 0
# Route add-host 10.20.30.148 gw 10.20.30.40
Route entry added to the network
# Route add-net 10.20.30.40 netmask 255.255.255.248 eth0
# Route add-net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
# Route add-net 192.168.1.0/24 eth1
Add default route
# Route add default gw 192.168.1.1
Delete A route
# Route del-host 192.168.1.2 dev eth0: 0
# Route del-host 10.20.30.148 gw 10.20.30.40
# Route del-net 10.20.30.40 netmask 255.255.255.248 eth0
# Route del-net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
# Route del-net 192.168.1.0/24 eth1
# Route del default gw 192.168.1.1
Configure packet forwarding
The default Kernel configuration in CentOS already contains the routing function, but it is not enabled by default when the system is started. To enable Linux routing, you can adjust the network parameters of the kernel. To configure and adjust kernel parameters, run the sysctl command. For example, to enable the packet forwarding function of the Linux kernel, run the following command.
# sysctl -w net.ipv4.ip_forward=1
After this setting, the current system will be able to forward packets, but the next time you start the computer, it will become invalid. To enable the system to remain valid the next time you start the computer, write the following lines to the configuration file/etc/sysctl. conf.
# vi /etc/sysctl.confnet.ipv4.ip_forward = 1
You can also run the following command to check whether the current system supports packet forwarding.
# sysctl net.ipv4.ip_forward