Using the route command, you can display and set the network routing table in the Linux kernel. The routes specified by the route command are mainly static routes. To communicate between two different subnets, you need a router connected to both networks, or a gateway located on both networks.

Routing in A Linux system is usually set to solve the following problems: The Linux system is in a LAN, and the LAN has a gateway that allows the machine to access the Internet, so you need to set the IP address of the machine as the default route for the Linux machine. Note that if you run the route command on the cli to add a route, the route will not be stored permanently. The route will become invalid after the NETWORK adapter or the machine restarts. You can add the route command in /etc/rc.local to ensure that the route setting is permanent.

Command syntax

Route (option)(parameter)

Command options

  • -a: sets the address type.
  • -c: prints the routing cache of the Linux core.
  • -v: details mode.
  • -n: Indicates that the DNS reverse lookup is not performed and the NUMERIC IP address is displayed.
  • -e: displays the routing table in netstat format.
  • -net: indicates the routing table to a network.
  • -host: indicates the routing table to a host.

The command parameter

  • Add: adds the specified route record.
  • Del: deletes the specified route record.
  • Target: indicates the destination network or host.
  • Gw: sets the default gateway.
  • MSS: Sets the maximum TCP block length (MSS), in MB.
  • Window: specifies the SIZE of the TCP window for TCP connections through the routing table.
  • Dev: indicates the network interface represented by the route record.

Display current route

> route
Copy the code

Display current routing table (display IP address)

> route -n
Copy the code

With route-n, the DNS reverse lookup is not performed and the IP address in numeric format is displayed. The listing speed is faster than that of route

Fields that

column meaning
Destination Target network or target host. If Destination is default (0.0.0.0), this is the default gateway and all data will be sent to this gateway (in this case, 10.139.128.1).
Gateway Gateway address: 0.0.0.0 indicates that the Destination corresponding to the current record is on the same network segment as the local PC and does not need to pass through the gateway during communication
Genmask Destination specifies the network mask. If Destination is a host, set it to 255.255.255.255; if Destination is a default route, set it to 0.0.0.0
Flags Refer to the following table for the meaning of the tag
Metric Routing distance, the number of transfers required to reach a specified network, is required for large LAN and WAN Settings (not used in the Linux kernel).
Ref Number of route entries referenced (not used in the Linux kernel.)
Use Number of times the route item is searched by the routing software
Iface The name of the network adapter, for example, eth0

Flags meaning

  • The U route is active
  • The H target is a host
  • G passes through the gateway
  • R Restores entries generated by dynamic routes
  • D is installed dynamically by the routing daemon
  • M is modified by the routing daemon
  • ! Refused to routing

The host routing

The Flags field is H for a routing table record that points to a single IP address or host name. In the following example, for the 10.0.0.8 host, gateway 192.168.0.1 route:

> route add -net 10.0.0.8 gateway 192.168.0.1 netmask 255.255.255.255 dev eth0
> route -n 
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.8        192.168.0.1     255.255.255.255 UGH   0      0        0 eth0
Copy the code

Network routing

The network that the host can reach. In the following example, for the 10.0.0.0/24 network, gateway 192.168.0.1 route:

> route add -net 10.0.0.0/24 gateway 192.168.0.1 dev eth0 > route -n Destination gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 192.168.0.1 255.255.255.0 UG 0 0 0 eth0 // or > route add -net 10.0.0.0 gateway 192.168.0.1 dev eth0Copy the code

The default route

When the IP address or network of the destination host is not in the routing table, the packet is sent to the default route (default gateway). The Destination of the default route is default or 0.0.0.0.

> route default gateway 0.0.0.0 UG 100 00 eth0Copy the code

Add hosts on the same LAN

If gw is not specified, the added route record does not use a gateway:

> route add -net 10.10.1.0 netmask 255.255.255.0 dev eth0
> route
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.10.1.0       0.0.0.0         255.255.255.0   U     0      0        0 eth0
Copy the code

Shielding routing

> route add -net 10.10.1.0 netmask 255.255.255.0 reject > Route Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.1.0-255.255.255.0! 0-0Copy the code

Deleting available Routes

> route del -net 10.0.0.0 netmask 255.255.255.0
Copy the code

If multiple routing tables are matched, delete one at a time

Delete and add the default gateway

Linux automatically checks the availability of the default gateway when it is added or removed:

> route add default gw 192.168.1.1
SIOCADDRT: Network is unreachable
> route del default gw 192.168.1.1
SIOCDELRT: No such process
Copy the code

Link :rumenz.com/rumenbiji/l… Wechat official Account: entry station