Linux command route detailed explanation

Linux command summary route command

The route command is used to display and manipulate the IP routing table. To implement communication between two different subnets, a router connecting the two networks, or a gateway located in both networks is required. In Linux systems, routing is usually set to solve the following problems: the Linux system is in a local area network, and there is a gateway in the local area network, which can allow the machine to access the Internet , then you need to set the IP address of this machine as the default of the Linux machine. routing. It should be noted that executing the route command directly on the command line to add a route will not be permanently saved. When the network card is restarted or the machine is restarted, the route will be invalid. To save it permanently, there are the following methods:

1

2

3

4

1. Add in /etc/rc.local

2. Add to the end in /etc/sysconfig/network

3./etc/sysconfig/static-router :

any net xxxx / 24 w yyyy

Command parameters

1

2

3

4

5

6

7

8

9

10

11

12

[root@linux ~]# route [-nee]

[root@linux ~]# route add [-net|-host] [domain or host] netmask [mask] [gw|dev]

[root@linux ~]# route del [-net|-host] [domain or host] netmask [mask] [gw|dev]

Observed parameters:

   -n : Do not use protocol or host name, use IP or port number directly;

   -ee : use more detailed information to display

Add (add) and delete (del) related parameters of the route:

   -net : Indicates that the following route is a domain;

   -host : indicates that the following route is connected to a single host;

   netmask : related to the domain, you can set the netmask to determine the size of the domain;

   gw : shorthand for gateway, followed by the value of IP, which is different from dev;

   dev : If you just want to specify which network card to connect to, use this setting, followed by eth0, etc.

Format: route -n
Format: /sbin/route -n
is used to print the routing table, and the -n parameter is used to print the ip address instead of the host name in the output information .

 

Format: route add default gw {IP-ADDRESS}{INTERFACE-NAME}
is used to set the default route, where parameter {IP-ADDRESS): used to specify the IP address of the router (gateway) ; parameter {INTERFACE-NAME}: used to specify the interface name, such as eth0 . Use /sbin/ifconfig -a to display all interface information.

 

Format: route add -net {NETWORK-ADDRESS}netmask {NETMASK} dev {INTERFACE-NAME}
adds to the routing rule of the specified network, where parameter {NETWORK-ADDRESS}: used to specify the network address parameter {NETMASK}: used to specify Subnet mask parameter {INTERFACE-NAME}: used to specify the interface name, such as eth0 .


1

2

例1:route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0

例2:route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

 

Format: route add -net{NETWORK-ADDRESS} netmask {NETMASK} reject
is set to the specified network as unreachable, so as to avoid the program waiting too long when connecting to the address of this network, and directly know that the network is unreachable.

1

例:route add -net 10.0.0.0 netmask 255.0.0.0 reject


Format: route del -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
Format: route del -net {NETWORK-ADDRESS} netmask {NETMASK} reject
is used to delete routing settings. Parameters are specified in a similar way to route add .

Detailed output

The meanings of the routing table fields output by the route command are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

Destination destination

          The destination network or destination host. The destination network or destination host.

 

    Gateway Gateway

          The gateway address or '*' if none set. The gateway address, or an asterisk if none.

 

    Genmask netmask

          The  netmask  for  the  destination net; '255.255.255.255' for a

          host destination and '0.0.0.0' for the default route.

 

     Flags: There are multiple flags in total, and the meanings are as follows:                       

 

         o U (route is up): the route is up;                      

 

         o H (target is a host):目标是一部主机 (IP) 而非网域;                      

 

         o G (use gateway):需要透过外部的主机 (gateway) 来转递封包;                      

 

         o R (reinstate route for dynamic routing):使用动态路由时,恢复路由资讯的旗标;                      

 

         o D (dynamically installed by daemon or redirect):已经由服务或转 port 功能设定为动态路由                      

 

         o M (modified from routing daemon or redirect):路由已经被修改了;                      

 

         o !  (reject route):这个路由将不会被接受(用来抵挡不安全的网域!)

 

         o A (installed by addrconf)

 

  

 

         o C (cache entry)

 

    Metric 距离、跳数。暂无用。

          The 'distance' to the target (usually counted in  hops).  It  is

          not  used  by  recent kernels, but may be needed by routing dae-

          mons.

 

    Ref   不用管,恒为0。

          Number of references to this route. (Not used in the Linux  ker-nel.)

 

    Use    该路由被使用的次数,可以粗略估计通向指定网络地址的网络流量。

          Count  of lookups for the route.  Depending on the use of -F and

          -C this will be either route cache misses (-F) or hits (-C).

 

    Iface 接口,即eth0,eth0等网络接口名

          Interface to which packets for this route will be sent.

范例一

单纯的观察路由状态

1

2

3

4

5

6

7

8

9

10

11

12

[root@linux ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0

0.0.0.0         192.168.10.30   0.0.0.0         UG    0      0        0 eth0

[root@linux ~]# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.10.0    *               255.255.255.0   U     0      0        0 eth0

169.254.0.0     *               255.255.0.0     U     0      0        0 eth0

default         server.cluster     0.0.0.0         UG    0      0        0 eth0

此外,观察一下上面的路由排列顺序喔,依序是由小网域(192.168.10.0/24  Class C),逐渐到大网域(169.254.0.0/16 Class B) 最后则是预设路由 (0.0.0.0/0.0.0.0)。然后当我们要判断某个网路封包应该如何传送的时候,该封包会经由这个路由的过程来判断喔!举例来说,我上头仅有三个路由,若我有一个传往 192.168.10.20 的封包要传递,那首先会找 192.168.10.0/24 这个网域的路由,找到了!所以直接由 eth0 传送出去;如果是传送到 Yahoo 的主机呢? Yahoo 的主机 IP  202.43.195.52,我通过判断 

1)不是 192.168.10.0/24
2)
不是 169.254.0.0/16 结果到达 

3)0/0  时, OK !传出去了,透过  eth0  将封包传给  192.168.10.30 那部  gateway  主机啊!所以说,路由是有顺序的。因此当你重复设定多个同样的路由时,例如在你的主机上的两张网路卡设定为相同网域的 IP  时,会出现什么情况?会出现如下的情况:

1

2

3

4

Kernel  IP  routing  table

Destination          Gateway                  Genmask                  Flags  Metric  Ref        Use  Iface

192.168.10.0        0.0.0.0                  255.255.255.0      U          0            0                0  eth0

192.168.10.0        0.0.0.0                  255.255.255.0      U          0            0                0  eth1

也就是说,由于路由是依照顺序来排列与传送的,所以不论封包是由那个介面  (eth0, eth1)  所接收,都会由上述的  eth0  传送出去,所以,在一部主机上面设定两个相同网域的  IP  本身没有什么意义!有点多此一举就是了。除非是类似虚拟主机  (Xen, VMware  等软体 )  所架设的多主机时,才会有这个必要~

范例二

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

[root@linux ~]# route del -net 169.254.0.0 netmask 255.255.0.0 dev eth0

# 上面这个动作可以删除掉 169.254.0.0/16 这个网域!

# 请注意,在删除的时候,需要将路由表上面出现的资讯都写入

# 包括  netmask , dev 等等参数喔!注意注意

[root@linux ~]# route add -net 192.168.100.0 netmask 255.255.255.0 dev eth0

# 透过 route add 来增加一个路由!请注意,这个路由必须要能够与你互通。

 

 

# 举例来说,如果我下达底下的指令就会显示错误:

 route add -net 192.168.200.0 netmask 255.255.255.0 gw 192.168.200.254

# 因为我的环境内仅有 192.168.10.100 这个 IP ,所以不能与 192.168.200.254这个网段直接使用 MAC 互通!这样说,可以理解喔!?

[root@linux ~]# route add default gw 192.168.10.30

# 增加预设路由的方法!请注意,只要有一个预设路由就够了喔!

# 在这个地方如果您随便设定后,记得使用底下的指令重新设定你的网路

  /etc/init.d/network restart

如果是要进行路由的删除与增加,那就得要参考上面的例子了,其实,使用 man route 里面的资料就很丰富了!仔细查阅一下囉!你只要记得,当出现『SIOCADDRT: Network is unreachable』这个错误时,肯定是由于 gw 后面接的 IP 无法直接与您的网域沟通 (Gateway 并不在你的网域内)

WINDOWS下的route命令

简单的的操作如下,

查看路由状态:routeprint

只查看ipv4ipv6)路由状态:route print-4(-6)

添加路由:route add 目的网络 mask 子网掩码网关——重启机器或网卡失效

route add 192.168.20.0 mask 255.255.255.0192.168.10.1

添加永久:route -p add 目的网络 mask子网掩码网关

route -p add 192.168.20.0 mask 255.255.255.0192.168.10.1

删除路由:route delete 目的网络 mask 子网掩码

route delete192.168.20.0 mask255.255.255.0

 

引用:http://www.cnblogs.com/ginvip/p/6539393.html

细节影响成败,感谢 琴酒网络 的总结整理。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476452&siteId=291194637