码迷,mamicode.com
首页 > 系统相关 > 详细

(转)linux route命令详解

时间:2017-12-20 22:47:00      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:which   inter   needed   处理   das   不用   alived   clust   ble   

原文:https://www.cnblogs.com/lpfuture/p/5857738.html   &&   http://blog.csdn.net/wangyan_hbu/article/details/51013602

为了让设备能访问另一个子网,需要在设备里增加路由到子网络,一般来说都是为了能访问别的子网才设置路由的,比如说,你的主机处于192.168.10.0/24,而你想访问192.168.20.0/24网的主机,当然你知道一个网关IP,例如192.168.10.1(必须和你主机处于同一子网),那么,你可以这样配置路由。

添加路由

route add -net 192.168.20.0 netmask 255.255.255.0 gw 192.168.10.1

查看路由状态

route -n

删除路由

route del -net 192.168.20.0netmask 255.255.255.0

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

 

1.在/etc/rc.local里添加
2.在/etc/sysconfig/network里添加到末尾
3./etc/sysconfig/static-router :
any net x.x.x.x/24 gw y.y.y.y


格式:route 
格式:/sbin/route 
用于打印路由表(display the current routing table)。

在非root用户使用时需要使用完整路径执行route命令。

命令参数

[root@linux ~]# route [-nee]
[root@linux ~]# route add [-net|-host] [网域或主机] netmask [mask] [gw|dev]
[root@linux ~]# route del [-net|-host] [网域或主机] netmask [mask] [gw|dev]
观察的参数:
   -n  :不要使用通讯协定或主机名称,直接使用 IP 或 port number;
   -ee :使用更详细的资讯来显示
增加 (add) 与删除 (del) 路由的相关参数:
   -net    :表示后面接的路由为一个网域;
   -host   :表示后面接的为连接到单部主机的路由;
   netmask :与网域有关,可以设定 netmask 决定网域的大小;
   gw      :gateway 的简写,后续接的是 IP 的数值喔,与 dev 不同;
   dev     :如果只是要指定由那一块网路卡连线出去,则使用这个设定,后面接 eth0 等

格式:route -n
格式:/sbin/route -n
用于打印路由表,加上-n参数就是在输出的信息中不打印主机名而直接打印ip地址。

格式:route add default gw {IP-ADDRESS} {INTERFACE-NAME}
用于设置默认路由,其中,
参数{IP-ADDRESS): 用于指定路由器(网关)的IP地址;
参数{INTERFACE-NAME}: 用于指定接口名称,如eth0。使用/sbin/ifconfig -a可以显示所有接口信息。
例:route add default gw mango

格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
添加到指定网络的路由规则,其中
参数{NETWORK-ADDRESS}: 用于指定网络地址
参数{NETMASK}: 用于指定子网掩码
参数{INTERFACE-NAME}: 用于指定接口名称,如eth0。
例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

格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} reject
设置到指定网络为不可达,避免在连接到这个网络的地址时程序过长时间的等待,直接就知道该网络不可达。
例:route add -net 10.0.0.0 netmask 255.0.0.0 reject

格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} reject
用于删除路由设置。参数指定的方式与route add相似。

 

输出详解

 

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.

范例一

单纯的观察路由状态


[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 时,会出现什么情况?会出现如下的情况: 
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 等软体 )  所架设的多主机时,才会有这个必要~

范例二

[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

只查看ipv4(ipv6)路由状态: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 delete 192.168.20.0 mask255.255.255.0

 

考试题一:linux下如何添加路由(百度面试题)

 

以上是原题,老男孩老师翻译成如下3道题。

 

a.如何用命令行方式给linux机器添加一个默认网关,假设网关地址为10.0.0.254?

b. 192.168.1.0网段, 192.168.1.1网关的某一服务器想连入172.16.1.0/24段,该如何添加路由(奇虎360)

c.如果添加一个主机路由?

请分别解答。

解答:route -net 172.16.1.0/24 gw 192.168.1.1

route 命令使用方法:

 

a.缺省网关路由

 

    默认网关就是数据包不匹配任何设定的路由规则,最后流经的地址关口!网关按字面意思就是网络的关口,就相当于我们家里房子的门一样,如果外出就要经过房门,数据包也是一样。

 

本题的答案:

 

route del default gw 10.0.0.254

 

解答实践:

 

[root@oldboy ~]# route -n #==>查看路由表,netstat -rn也可以。

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.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         10.0.0.254      0.0.0.0         UG    0     0        0 eth0

 

#==>这里就是系统的默认网关信息,表示去任何地方(0.0.0.0),都发给10.0.0.254,因为是默认网关,所以,放在了最后一条。路由也是有顺序的,如果不符合任何一条规则就交给默认网关处理。

 

[root@oldboy ~]# route del default gw 10.0.0.254 #==>这个命令是删除默认的网关。

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.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

 

[root@oldboy ~]# route add default gw 10.0.0.254    #==>这个命令是添加默认的网关,也是本题的答案。

 

[root@oldboy ~]# netstat -rn

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

 

10.0.0.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         10.0.0.254      0.0.0.0         UG    0     0        0 eth0 #==>又回来了

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.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         10.0.0.254      0.0.0.0         UG    0     0        0 eth0 #这里就是添加的默认网关记录。

 

特别强调:实际上route add default gw 10.0.0.254 就相当于route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.0.0.254

 

b.网络路由:即去往某一网络或网段的路由

 

    一般多网段之间互相通信,希望建立一条优先路由,而不是通过默认网关时就可以配置网络路由。还是拿房子比喻,你现在不是要出门,而是卧室,卫生间,去卧室就要经过卧室的门,去卫生间也要经过卫生间的门,这里的卧室和卫生间的门就可以认为是去往某一网段的路由,而不是默认路由(即房子的门。)

 

    实际工作中会有需求,两个不同的内部网络之间互访,而不是出网访问,就是上面例子的情况。

 

    本题的答案:

 

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

解答实践:

 

[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

SIOCADDRT: 网络不可达 #==>当连不通地址192.168.1.1时,无法添加路由。

 

[root@oldboy ~]# ifconfig eth0:0 192.168.1.1/24 up #==>添加一个IP别名用于临时测试,如果永久生效最好加双网卡或写入到配置文件。

 

[root@oldboy ~]# ifconfig eth0:0 #==>查看添加的IP别名(网络里把这种多IP的方式称为子接口)

 

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:65:A4:FD 

 

          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0

 

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

 

再来添加去192.168.1.0的数据包,交给192.168.1.1处理。

 

[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

[root@oldboy ~]# netstat -rn   #==>和route -n很像。

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

 

10.0.0.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0

 

192.168.1.0     192.168.1.1     255.255.255.0   UG        0 0          0 eth0 #==>这就是网络路由

 

192.168.1.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         10.0.0.254      0.0.0.0         UG        0 0          0 eth0

 

拓展:其他写法

 

[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0  #==>指定设备而不是地址。

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0       0 eth0

 

192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

 

192.168.1.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0

 

192.168.1.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         10.0.0.254      0.0.0.0         UG    0     0        0 eth0

 

[root@oldboy ~]# route del -net 192.168.1.0/24 dev eth0   

 

[root@oldboy ~]# route add -net 192.168.1.0/24 dev eth0   

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0       0 eth0

 

192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

 

192.168.1.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         10.0.0.254      0.0.0.0         UG    0     0        0 eth0

 

总结:

 

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0

 

route add -net 192.168.1.0/24 dev eth0 

 

route del -net 192.168.1.0/24 dev eth0  

 

特别强调:以上配置在重启网络时都会失效,那么如何让它永久生效呢?

 

如果要是永久生效,有如下几种方法:

方法一:

 

vi /etc/sysconfig/network-scripts/route-eth0  #默认不存在此文件

 

加入如下内容:

 

192.168.1.0/24 via 192.168.1.1

 

提示:写到配置里,重启网络服务和重启系统都会生效!

 

 

 

 

方法二:

 

vi /etc/sysconfig/static-routes  #默认不存在此文件

 

加入如下内容:

 

any net 192.168.1.0/24 gw 192.168.1.1

 

提示:写到配置里,重启网络服务和重启系统都会生效!

 

 

 

 

方法三:

 

vi /etc/rc.local

 

加入如下内容:

 

route add -net 192.168.1.0/24 gw 192.168.1.1

 

PS: 方法一推荐生产环境使用

 

提示:方法三写到/etc/rc.local里只在开机时加载,当手工重启网络后会失效,但是重启系统后会生效!

 

 

如果是配置默认路由网关可以再网卡配置里:

[root@oldboy ~]# grep GATEWAY /etc/sysconfig/network-scripts/ifcfg-eth0

GATEWAY=10.0.0.254

 

c.主机路由:就是去往某个主机地址如何配置路由

 

/sbin/route add -host 192.168.2.13 dev eth2

 

/sbin/route add -host 202.81.11.91 dev lo

 

例如:keepalived或heartbeat高可用服务器对之间的使用单独网卡接心跳线通信就会用到以上主机路由。

route命令拓展:

 

删除一条默认路由:

 

route del default gw 10.0.0.254

 

删除一条静态路由:

 

route del –net 目标网络 netmask

 

如:route del -net 192.168.1.0/24 或route del -net 192.168.1.0 netmask 255.225.255.0

 

删除一条主机路由:

 

route del -host 192.168.1.10 dev eth0

 

有关route命令更详细的内容需要大家执行man route查看帮助,并仔细总结。

 

有关此题,我们谈下多网段生产环境网段划分及路由的解决方案(1000台机器划分网段方案)。我们能感受到route命令不同功能应用案例。

(转)linux route命令详解

标签:which   inter   needed   处理   das   不用   alived   clust   ble   

原文地址:http://www.cnblogs.com/liujiacai/p/8076124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!