zoukankan      html  css  js  c++  java
  • Linux route

    一、简介

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

     

    二、路由知识

    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)默认路由

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

    3)静态默认路由

    一般的默认路由都是静态的。

    4)动态默认路由

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

     

    三、实例

    1)查看当前路由

    route –n

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

    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

     

    四、输出详解

    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.

     

    五、常见错误

    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: 没有那个进程。

     

  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/274914765qq/p/5246863.html
Copyright © 2011-2022 走看看