Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The function and usage of route command

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the function and usage of route command for you in detail. The sample code in this article is very detailed and has certain reference value. Interested friends can refer to it.

Detailed explanation of route command

The route command of the Linux system is used to display and manipulate the IP routing table (show / manipulate the IP routing table). To achieve communication between two different subnets, you need a router connecting two networks, or a gateway located in both networks. In Linux systems, routing is usually set up to solve the following problems:

The Linux system is in a local area network, where there is a gateway that allows the machine to access Internet, so the IP address of this machine needs to be set to the default route of the Linux machine. It is important to note that the route will not be saved permanently by executing the route command directly under the command line, and the route will become invalid when the network card is restarted or the machine is rebooted; you can add the route command in / etc/rc.local to ensure that the route setting is permanent.

1. Command format:

Route [- f] [- p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]

2. Command function:

The Route command is used to manipulate the kernel-based ip routing table, and its main purpose is to create a static route to specify a host or network through a network interface, such as eth0. When the "add" or "del" parameters are used, the routing table is modified, and if there are no parameters, the current contents of the routing table are displayed.

3. Command parameters:

-c display more information

-n does not resolve the name

-v displays detailed processing information

-F display sending message

-C displays the route cache

-f clears the routing table for all gateway entrances.

-p makes the route permanent when used with the add command.

Add: add a new route.

Del: delete a route.

-net: destination address is a network.

-host: the destination address is a host.

Netmask: when adding a network route, a network mask is required.

Gw: routes packets through the gateway. Note that the gateway you specify must be reachable.

Metric: sets the number of routing hops.

Command specifies the command you want to run (Add/Change/Delete/Print).

Destination specifies the network destination for the route.

Mask Netmask specifies the network mask (also known as the subnet mask) associated with the network destination.

Gateway specifies the forward or next-hop IP address that can be reached by the set of addresses defined by the network destination and the subnet mask.

Metric Metric specifies an integer cost value for the route (from 1 to 9999), which can be used when selecting multiple routes in the routing table that best match the destination address of the forwarded packet.

If Interface specifies the interface index for the interface that can access the target. To get a list of interfaces and their corresponding interface index, use the display function of the route print command. You can use decimal or hexadecimal values for interface indexing.

4. Use an example:

Example 1: show the current route

Command:

Routeroute-n

Output:

[root@localhost ~] # routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 000 eth0e192.168.0.0 192.168.120.1 255.255.0.0 UG 2000 eth010.0.0.0 192.168.120.1 255.0 .0.0 UG 000 eth0default 192.168.120.240 0.0.0.0 UG 000 eth0 [root@localhost ~] # route-nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 0.0.0.0 255.255.255.0 U 000 eth0192 .168.0.0 192.168.120.1 255.255.0.0 UG 00 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 00 0 eth00.0.0.0 192.168.120.240 0.0.0.0 UG 00 0 eth0

Description:

The first line indicates that the address of the host network is 192.168.120.0. If the data transfer target is to communicate in the local local area network, the packet can be forwarded directly through the eth0.

The fourth line indicates that the purpose of the data transfer is to access the Internet, then the interface eth0 sends the packet to the gateway 192.168.120.240

Where Flags is the routing flag, marking the status of the current network node.

The Flags logo says:

U Up indicates that this route is currently started

H Host, indicating that this gateway is a host

G Gateway, indicating that this gateway is a router

R Reinstate Route, routes that are reinitialized with dynamic rout

D Dynamically, this route is dynamically written to

M Modified, this route is dynamically modified by the routing daemon or director

! Indicates that this route is currently turned off

Note:

Route-n (- n means that the name is not parsed, and the listing speed is faster than route)

Example 2: add gateway / set gateway

Command:

Route add-net 224.0.0.0 netmask 240.0.0.0 dev eth0

Output:

[root@localhost ~] # route add-net 224.0.0.0 netmask 240.0.0.0 dev eth0 [root@localhost ~] # routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 000 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 000 eth0224.0.0.0 * 240.0.0.0 U 000 eth0default 192.168.120.240 0.0.0 UG 000 eth0 [root@localhost ~] #

Description:

Add a route to 244.0.0.0

Example 3: block a route

Command:

Route add-net 224.0.0.0 netmask 240.0.0.0 reject

Output:

[root@localhost ~] # route add-net 224.0.0.0 netmask 240.0.0.0 reject [root@localhost ~] # routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 000 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 2000 eth0224.0.0.0-240.0.0.0! 0-0-224.0.0.0 * 240.0.0.0 U 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0

Description:

Add a masked route whose destination address is 224.x.x.x will be rejected

Example 4: delete routing record

Command:

Route del-net 224.0.0.0 netmask 240.0.0.0route del-net 224.0.0.0 netmask 240.0.0.0 reject

Output:

[root@localhost ~] # routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 000 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 2000 eth010.0.0.0 192.168.120.1 255.0 .0.0 UG 000 eth0224.0.0.0-240.0.0.0! 0-0-224.0.0.0 * 240.0.0.0 U 000 eth0default 192.168.120.240 0.0.0.0 UG 000 eth0 [root@localhost ~] # route del-net 224.0.0.0 netmask 240.0.0.0 [root@localhost ~] # routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 000 eth0192.168.0.0 192.168.120.1 255. 255.0.0 UG 000 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 000 eth0224.0.0.0-240.0.0.0! 0-0-default 192.168.120.240 0.0.0.0 UG 000 eth0 [root@localhost ~] # route del-net 224.0.0.0 netmask 240.0.0.0 reject [root@localhost ~] # routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 000 eth0192.168.0.0 192.168.120.1 255. 255.0.0 UG 000 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 000 eth0default 192.168.120.240 0.0.0 UG 000 eth0 [root@localhost ~] #

Description:

Example 5: delete and add default gateways

Command:

Route del default gw 192.168.120.240route add default gw 192.168.120.240

Output:

[root@localhost ~] # route del default gw 192.168.120.240 [root@localhost ~] # routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 000 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 000 eth010.0 .0.0 192.168.120.1 255.0.0.0 UG 0 eth0 [root@localhost ~] # route add default gw 192.168.120.240 [root@localhost] # routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 000 eth0192.168 .0.0 192.168.120.1 255.255.0.0 UG 000 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 000 eth0default 192.168.120.240 0.0.0 UG 000 eth0 [root@localhost ~] #

The above is a brief introduction to the functions and usage of the route command. Of course, the differences in the detailed use of the above command have to be understood by everyone. If you want to know more, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report