Linux command network command route

1. Introduction to route command

  The route command is a network management tool in Linux systems, used to display and operate the IP routing table. It can be used to view the routing table information of the current system, add, delete or modify routing table entries, and display detailed information in the routing table. The route command can help users diagnose and solve network connection problems, as well as perform network planning and optimization.

2. Route command usage example

1. Check the command version

  The route command also belongs to the command in the net-tools tool set. If the Linux system does not have this command, we can install it by installing the net-tools software package.

[root@s152 ~]# route --version
net-tools 2.10-alpha
+NEW_ADDRT +RTF_IRTT +RTF_REJECT +I18N +SELINUX
AF: (inet) +UNIX +INET +INET6 +IPX +AX25 +NETROM +X25 +ATALK +ECONET +ROSE -BLUETOOTH
HW: +ETHER +ARC +SLIP +PPP +TUNNEL -TR +AX25 +NETROM +X25 +FR +ROSE +ASH +SIT +FDDI +HIPPI +HDLC/LAPB +EUI64

2. Get command help

  You can get command help through the –help or -h parameter, and the route command can view, update, modify, or delete the system routing table.
Insert image description here

3. View operating system routing

  Use the -n parameter to view routing details. Of course, you can also view it without parameters. -n means not to resolve the host name.

[root@s152 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

4. Add a route

  Use the add parameter to add a route. You can add a network segment or a host route.

[root@s152 ~]# route add -net 192.168.122.0/24 gw 192.168.0.166
[root@s152 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.122.0 192.168.0.166 255.255.255.0 UG 0 0 0 eth0

5. Delete a route

  Use the del parameter to delete a segment or a host route.

[root@s152 ~]# route del -net 192.168.122.0/24
[root@s152 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

5. Add a host route

[root@s152 ~]# route add -host 192.168.122.1 gw 192.168.0.166
[root@s152 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.122.1 192.168.0.166 255.255.255.255 UGH 0 0 0 eth0
[root@s152 ~]# ping 192.168.122.1
PING 192.168.122.1 (192.168.122.1) 56(84) bytes of data.
64 bytes from 192.168.122.1: icmp_seq=1 ttl=64 time=0.795 ms

6. Add a prohibited access route

  Use the reject parameter to indicate rejecting the route, which is used for access security control and prohibits hosts from accessing hosts that are clearly unsafe or do not have permission to access. After adding, check the routing table. The status is an exclamation mark, indicating that access is prohibited and the ordinary routing policy takes precedence.
Insert image description here

7. Delete the default route

  Use del default gw to delete the default gateway.

[root@s166 ~]# route del default gw 192.168.0.1
[root@s166 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

8. Add default route

  Use add default gw to add a default gateway.

[root@s166 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@s166 ~]# route add default gw 192.168.0.1
[root@s166 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

3. Route command usage syntax and parameter description

1. Use grammar

View route usage: #route [-nNvee] [-FC] []
Update route usage: #route [-v] [-FC] {add|del|flush } …

2. Parameter description

Parameter options Parameter Description
-n Use the IP address directly without DNS resolving the host
- of Show more detailed routing information
add Add routing information
of the Delete routing information
target Specify the target network or host. Can use IP address or host/network name
-net A route to a network, followed by a network number address
-host A route to a host, followed by a host address
netmask NM Specify the network mask for the added route, NM represents the mask address, such as 255.255.255.0
gw GW Specify a gateway for any packets destined for the destination network/host
dev lf Specify which network device goes out, followed by the network device name, such as etho, etc.

3. Flags routing label information

Flagsmark Tag description
U(route is up) Indicates that this route is currently in the activated state
H(target is a host) The destination route is a host (IP) not a network
R(reinstate route for dynamic routing): When using dynamic routing, restore routing information identification
G(use gateway) Indicates that data needs to be transferred through an external host (gateway)
M(modified from routing daemon or redirect) Indicates that the route has been modified
D(dynamically installed by daemon or redirect) The service has been set as dynamic routing
!(reject route ) This route will not be accepted (used to protect against insecure networks)

4. Static route permanent configuration method

  The routes we configure using the route command are temporarily effective and will become invalid after the network card is restarted or the system is restarted. If we need to configure permanent static routes, we can use the following methods.

  • method one:

vi /etc/sysconfig/network-scripts/route-eth0 #<==This file does not exist by default
Add the following content
192.168 .1.0/24 via 10.0.0.254

  • Method two:

vi /etc/sysconfig/static-routes #<==This file does not exist by default
Add the following content
any net 192.168.1.0 /24 gw 10.0.0.254

  • Method three:

vi /etc/rc.local
Add the following content
route add -net 192.168.1.0/24 gw 10.0.0.254

Guess you like

Origin blog.csdn.net/carefree2005/article/details/133709139