Linux route command Detailed and use the sample (view and manipulate the IP routing table)

Linux system route command is used to display and operate the IP routing table (show / manipulate the IP routing table ).
To enable communication between two different subnets, it requires a router to connect to a network or both networks at the same time in a gateway to implement.

  In the Linux system, set the route usually to address the following issues:
the Linux system in a local area network, local area network has a gateway that allows the machine to access Internet, you will need these IP address of the machine as the default Linux machines routing.
It should be noted that the implementation of route commands directly from the command line to add routes, not saved permanently, after reboot or restart the machine card, the route becomes ineffective;
you can add route commands in /etc/rc.local to ensure the permanent set route.

  1. Format:

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

  2. Function:

    route command is used to operate the kernel-based ip routing table, its main role is to create a static route to specify a host or make a network via a network interface, such as eth0.
When "add" or "del" parameter, routing tables are modified, if not, the command displays the current contents of the routing table.

  3. Command parameters:

    -c Show more information

    -n do not resolve names

    -v display detailed processing information

    -F transmission information display

    Display routing cache -C

    -f Clears all gateway routing table entry.

    So that when used in conjunction with a permanent route -p add command.

  add: to add new routes.
del: delete a route.
-net: destination address is a network.
-host: The destination address is a host.
netmask: When you add a network route, you need the network mask.
gw: routing packets through the gateway.
Note that you must be able to reach a specific gateway. metric: Set routing hops.

Command Specifies the command you want to run (Add / Change / Delete / Print ).
Destination Specifies the network destination of the route.
mask Netmask Specifies the network mask associated with the network of the target (also referred to as a subnet mask).

  Gateway object definition specifies the network address and subnet mask set can reach forward or next-hop IP address.

  a specified cost metric Metric integer value denoted route (from 1 to 9999), when the selected (best matches the packet's destination address forwarding) of the plurality of routes in the routing table may be used.

  if Interface to access to the target's interface specifies the interface index. To obtain a list of interfaces and their corresponding interface indexes, use the route print command display. You can use either decimal or hexadecimal values ​​interface index.

4. Example:
  Example 1: Display current routing

    route or route -n

root@ubuntu:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 59.188.252.1 0.0.0.0 UG 100 0 0 br0
10.22.0.0 0.0.0.0 255.255.255.0 U 0 0 0 br0
59.188.252.0 0.0.0.0 255.255.255.0 U 0 0 0 br0
172.22.0.0 0.0.0.0 255.255.255.0 U 0 0 0 br1
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

  Description:  

  The first row represents the network where the host address 192.168.120.0, if the data transfer destination is a communication within the LAN, the packet may be forwarded directly through the eth0;  

  The fourth line represents data transfer destination is to visit Internet, by the interface eth0, the packet is sent to the gateway 192.168.120.240

    Wherein Flags routing flag, the flag state of the current network node.

    Flags Flag Description:

      U Up indicate that this route is currently active state H Host, it indicates that this is a host gateway   

      G Gateway, represented by the gateway is a router R Reinstate Route, using dynamic routing routes reinitialization

      D Dynamically, the route is written dynamics M Modified, the route is dynamically modified by the routing daemon or guide

      ! Indicates that the route is currently closed state

  Remarks:

    route -n (-n said they did not resolve the name list will be faster than the route)

  Example 2: Add the Gateway / gateway provided

    Command: route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

  Example 3: shielding a route

    Command: route add -net 224.0.0.0 netmask 240.0.0.0 reject

  [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
  说明:

    Route add the destination address of a mask will be rejected 224.xxx

  Example 4: Delete route record

    Command:
      route del -net 224.0.0.0 240.0.0.0 Netmask
      route del -net 224.0.0.0 240.0.0.0 Netmask Reject
  Example 5: Set the default gateway to add and delete

  命令:
    route del default gw 192.168.120.240
    route add default gw 192.168.120.240

  [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

Guess you like

Origin www.cnblogs.com/ryanace1988/p/11082976.html