Home  >  Article  >  Operation and Maintenance  >  How to add routes in linux

How to add routes in linux

WBOY
WBOYOriginal
2022-01-06 11:39:5724564browse

In Linux, you can use the route command to add routes. This command is used to operate the kernel-based IP routing table. You can create or delete specified routes. The syntax is "route add -net gw" or "route add - host dev".

How to add routes in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to add routing in Linux

The route command of 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 that connects the two networks, or a gateway that is located on both networks at the same time.

In Linux systems, routing is usually set up to solve the following problems: The Linux system is in a LAN, and there is a gateway in the LAN that allows the machine to access the Internet. Then the IP address of the machine needs to be Set as the default route for Linux machines. It should be noted that executing the route command directly on the command line to add a route will not be saved permanently. When the network card is restarted or the machine is restarted, the route will become invalid; you can add the route command in /etc/rc.local to ensure This routing setting is permanent.

1. Command format:

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

2. Command function:

Route command is used to operate the kernel-based ip routing table. Its main function is to create a static route to specify a host or a network through a network interface, such as eth0. When the "add" or "del" parameter is used, the routing table is modified. If there are no parameters, the current contents of the routing table are displayed.

3. Command parameters:

  • -c Display more information

  • -n Do not resolve names

  • -v Display detailed processing information

  • -F Display sending information

  • -C Display routing cache

  • -f clears the routing tables of all gateway entries.

  • -p makes the route persistent when used with the add command.

add: Add a new route.

del: Delete a route.

-net: The target address is a network.

-host: The target address is a host.

netmask: When adding a network route, a network mask needs to be used.

gw: Route packets through the gateway. Note that the gateway you specify must be reachable.

metric: Set the routing hop count.

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

Destination specifies the network destination of this route.

mask Netmask Specifies the network mask (also called a subnet mask) associated with the network target.

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

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

if Interface Specifies the interface index for the interface that can access the target. To obtain a list of interfaces and their corresponding interface indexes, use the display function of the route print command. Interface indexing can be done using decimal or hexadecimal values.

How to add routes in linux

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to add routes in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn