Networking: Routing Tables in Linux - route

Original address: http://helloandroid.iteye.com/blog/1162748
write picture description here
[toc]

View routing table

# route 
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface 
192.168.0.0     *               255.255.255.0   U     0      0        0 eth0 
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0 
default         192.168.0.1     0.0.0.0         UG    0      0        0 eth0 

Description of output

  • Destination target network segment or host
  • Gateway gateway address, "*" indicates that the target is the network to which the host belongs, no routing is required
  • Genmask netmask
  • Flags flags. Some possible tags are as follows:
  • U — route is active
  • H - the target is a host
  • G — route points to gateway
  • R — restore table entries generated by dynamic routing
  • D — Installed dynamically by routed daemons
  • M — Modified by routed daemons
  • ! — deny routing
  • Metric routing distance, the number of transits required to reach the specified network (not used in the linux kernel)
  • Ref routing item reference times (not used in the linux kernel)
  • Use The number of times this routing item was looked up by routing software
  • Iface The output interface corresponding to this routing table entry

3 routing types

host routing

A host route is a routing record in a routing table that points to a single IP address or hostname. The Flags field of the host route is H. For example, in the example below, the localhost reaches the host with IP address 10.0.0.10 through the router with IP address 192.168.1.1.

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 represents a host can reach. The Flags field of the network route is N. For example, in the example below, localhost forwards packets destined for network 192.19.12 to the router with IP address 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 destination 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 example below, the default route is the router with 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 routes

route command The route command can be used to
set and view the routing table. The command format for setting the kernel routing table is:

# route  [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If] 

Syntax Description

  • add : add a routing rule
  • del : delete a routing rule
  • -net : destination address is a network
  • -host : destination address is a host
  • target : destination network or host
  • netmask : the netmask of the destination address
  • gw : gateway to route packets through
  • dev : the network interface specified for the route

Example of use

Route added to host

# route add -host 192.168.1.2 dev eth0:0 
# route add -host 10.20.30.148 gw 10.20.30.40 

Routes 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 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 

Set up packet forwarding

The default kernel configuration in CentOS already includes routing, but it is not enabled by default at system startup. Enabling the routing function of Linux can be achieved by adjusting the network parameters of the kernel. To configure and adjust kernel parameters you can use the sysctl command. For example: To enable the packet forwarding function of the Linux kernel, use the following command.

# sysctl -w net.ipv4.ip_forward=1 

After this setting, the current system can implement packet forwarding, but it will be invalid when the computer is started next time. The following lines need to be written to the configuration file /etc/sysctl.conf in order for it to be valid the next time the computer is started.

# vi /etc/sysctl.conf 
net.ipv4.ip_forward = 1 

Users can also use the following command to check whether the current system supports packet forwarding.

# sysctl  net.ipv4.ip_forward

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325925447&siteId=291194637