One linux command per day: route command

Keywords: network Linux

The route command of Linux system is used to display and manipulate the IP routing table. To achieve communication between two different subnetworks, a router connecting two networks or a gateway located at both networks is needed. In Linux system, routing is usually set to solve the following problems: the Linux system in a LAN, there is a gateway in the LAN, so that the machine can access the Internet, then it is necessary to set the IP address of the machine as the default route of the Linux machine. It is important to note that routing is added by executing route command directly from the command line and will not be permanently saved. When the network card is restarted or the machine is restarted, the route will be invalid. Route command can be added to / etc/rc.local to ensure that the routing settings are permanently valid.

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. Its main function is to create a static routing so that a specified host or network can pass through a network interface, such as eth0. When using "add" or "del" parameters, the routing table is modified, and if there are no parameters, the current content of the routing table is displayed.

3. Command parameters:

  • - c Display more information
  • - n does not resolve names
  • - v Displays detailed processing information
  • - F Displays Sending Information
  • - C Display Routing Cache
  • - f Clears the routing table for all gateway entries.
  • - p makes routing permanent when used with the add command.
  • add: add a new route.
  • del: Delete a route.
  • - net: The destination address is a network.
  • - host: The target address is a host.
  • netmask: When adding a network routing, you need to use a network mask.
  • gw: Routing packets through gateways. Note that the gateway you specify must be accessible.
  • metric: Set the number of routing hops.

Command specifies the command you want to run (Add/Change/Delete/Print).
Destination specifies the network target for the route.
mask Netmask specifies the network mask associated with the network target (also known as a subnet mask).
Gateway specifies the forward or next hop IP addresses that the network target defines and the subnet mask can reach.
metric Metric specifies an integer cost scale (from 1 to 1999) for routing, which can be used when selecting multiple routes in a 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 get a list of interfaces and their corresponding interface indexes, use the route print command's display function. You can use decimal or hexadecimal values for interface indexing.

4. Use examples:

Example 1: Display the current routing
Order:

route
route -n

Output:

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
e192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
0.0.0.0         192.168.120.240 0.0.0.0         UG    0      0        0 eth0

Explain:

The first line indicates that the address of the host network is 192.168.120.0. If the target of data transmission is to communicate within the local area network, the data packet can be forwarded directly through eth0.
The fourth line indicates that the purpose of data transmission is to access the Internet. Then the data packet is sent to gateway 192.168.120.240 by interface eth0.
Flags is a routing flag, marking the status of the current network node.

Flags logo description:

  • U Up indicates that this route is currently in startup state
  • H Host, indicating that the gateway is a host
  • G Gateway, which means this gateway is a router
  • R Reinstate Route, a route that is reinitialized using dynamic routing
  • D Dynamically, this route is written dynamically
  • M Modified, this routing is dynamically modified by the routing daemon or guide
  • ! indicates that this route is currently closed

Remarks:

Route-n (-n denotes unresolved names and lists faster than route)

Example 2: Add Gateway / Set Gateway
Order:

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 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]#  

Explain:

Add a route to 244.0.0.0

Example 3: Shielding a route
Order:

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 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0

Explain:

Adding a shielded route to the destination address of 224.x.x.x will be rejected

Example 4: Delete routing records
Order:

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

Output:

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# 

Example 5: Delete and add default gateways
Order:

route del default gw 192.168.120.240
route add default gw 192.168.120.240

Output:

[root@localhost ~]# route del default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
[root@localhost ~]# route add default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# 

Posted by pluto on Thu, 28 Mar 2019 02:39:28 -0700