Linux route

1、简介html

Linux系统的route命令用于显示和操做IP路由表(show / manipulate the IP routing table)。要实现两个不一样的子网之间的通讯,须要一台链接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由一般是为了解决如下问题:该Linux系统在一个局域网中,局域网中有一个网关,可以让机器访问Internet,那么就须要将这台机器的IP地址设置为Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启以后,该路由就失效了;能够在/etc/rc.local中添加route命令来保证该路由设置永久有效。linux

 

2、路由知识安全

1)静态路由网络

静态路由是手动设置的路由。
如ip route 192.168.1.1 255.255.255.255 192.168.1.254,这条静态路由的含义是把目的地为192.168.1.1的IP的数据所有转发至192.168.1.254

2)默认路由this

默认路由是其余路由规则都没有匹配到时,才会选择使用的路由。             
如:ip route 0.0.0.0 0.0.0.0 192.168.1.1,这条默认路由的含义是若是其余路由规则不匹配,就把数据转发至192.168.1.1

3)静态默认路由spa

通常的默认路由都是静态的。

4)动态默认路由命令行

这个和静态的默认路由又有什么区别呢?好比有一个星形拓扑,R1为核心路由器,而剩下的R2,R3为分支,想要让R1告诉分支路由器,当找不到路径到达一个远程网络时候,所有经过核心路由器来转发数据到远程网络。可是当有新的路由器加入到这个网络拓扑的时候,这个路由器也能经过核心路由器发现核心路由器上的默认路由,这时候的默认路由则是动态的(例如:只要是可以动态获得核心路由器的网络的全部的路由器都能有这么一条默认路由,如EIGRP 用D*来表示)

 

3、实例code

1)查看当前路由htm

route –n

2)添加/屏蔽/删除一条路由blog

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
route add -net 224.0.0.0 netmask 240.0.0.0 reject
route del -net 224.0.0.0 netmask 240.0.0.0

3) 添加/删除设置默认网关

route add default gw 192.168.0.1
route del default gw 192.168.0.1

 

4、输出详解

route命令输出的路由表字段含义以下:
    Destination 目标
          The destination network or destination host. 目标网络或目标主机。

    Gateway 网关
          The gateway address or '*' if none set. 网关地址,若是没有就显示星号。

    Genmask 网络掩码
          The  netmask  for  the  destination net; '255.255.255.255' for a
           host destination and '0.0.0.0' for the default route.

     Flags:总共有多个旗标,表明的意义以下:                        

         o U (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.

 

5、常见错误

1)route: netmask doesn't match route address

linux-rd4x:/ # route add -net 8.8.8.8 netmask 255.255.255.0 dev eth0  
route: netmask doesn't match route address

提示:

随意添加一条路由信息时,报错,通过尝试,Destination IP必须和掩码相对应,
即IP:8.8.8.8 & 255.255.255.0(逻辑与操做) 才是真正的DestinationIP,不然会报错。

2)SIOCADDRT: 没有那个进程(No Such Process)

route add -net 192.168.1.0 netmask 255.255.255.0                
SIOCADDRT: 没有那个设备

提示:

在添加一个新的网段时,它的出口地址应该是当前ip routing table表中的当前有的地址,不然会出现这个错误:SIOCADDRT: 没有那个进程。

 

相关文章
相关标签/搜索