• Linux使用route命令显示和操作IP路由表


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

    常用参数

    格式:route

    格式:/sbin/route

    用于打印路由表(display the current routing table)。

    在非root用户使用时需要使用完整路径执行route命令。TP-Link 620G在Fedora上安装网卡驱动指南

    格式:route -n

    格式:/sbin/route -n

    用于打印路由表,加上-n参数就是在输出的信息中不打印主机名而直接打印ip地址。像netstat命令也有此参数。

    格式:route add default gw {IP-ADDRESS} {INTERFACE-NAME}

    用于设置默认路由(adds a default route, which will be used if no other route matches),其中,

    参数{IP-ADDRESS): 用于指定路由器(网关)的IP地址(Specify router IP address);
    参数{INTERFACE-NAME}: 用于指定接口名称,如eth0(Specify interface name such as eth0)。使用/sbin/ifconfig -a可以显示所有接口信息。

    man route 写道
    route add default gw mango-gw
    adds a default route (which will be used if no other route matches). All packets using this route will
    be gatewayed through "mango-gw". The device which will actually be used for that route depends on how we
    can reach "mango-gw" - the static route to "mango-gw" will have to be set up before.
     

    格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}

    添加到指定网络的路由规则,其中

    参数{NETWORK-ADDRESS}: 用于指定网络地址

    参数{NETMASK}: 用于指定子网掩码

    参数{INTERFACE-NAME}: 用于指定接口名称,如eth0。

    man route 写道
    route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0
    adds a route to the network 192.56.76.x via "eth0". The Class C netmask modifier is not really necessary
    here because 192.* is a Class C IP address. The word "dev" can be omitted here.

    route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
    This is an obscure one documented so people know how to do it. This sets all of the class D (multicast)
    IP routes to go via "eth0". This is the correct normal configuration line with a multicasting kernel.
     

    格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} reject

    设置到指定网络为不可达,避免在连接到这个网络的地址时程序过长时间的等待,直接就知道该网络不可达。

    man route 写道
    route add -net 10.0.0.0 netmask 255.0.0.0 reject
    This installs a rejecting route for the private network "10.x.x.x."

    格式: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  Possible flags include 标志,常用的是U和G。
                  U (route is up) 路由启用
                  H (target is a host) 目标是主机
                  G (use gateway) 使用网关
                  R (reinstate route for dynamic routing)
                  D (dynamically installed by daemon or redirect)
                  M (modified from routing daemon or redirect)
                  A (installed by addrconf)
                  C (cache entry)
                  !  (reject route)

           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用户)

    [root@jfht ~]# route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    211.103.28.0    *               255.255.255.224 U     0      0        0 eth0
    192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
    169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
    default         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
    [root@jfht ~]# /sbin/route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    211.103.28.0    *               255.255.255.224 U     0      0        0 eth0
    192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
    169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
    default         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
    [root@jfht ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    211.103.28.0    0.0.0.0         255.255.255.224 U     0      0        0 eth0
    192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
    169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
    0.0.0.0         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
    [root@jfht ~]# /sbin/route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    211.103.28.0    0.0.0.0         255.255.255.224 U     0      0        0 eth0
    192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
    169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
    0.0.0.0         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
    [root@jfht ~]#

    示例二 打印当前路由表(非root用户)

    [web@hnweb1 ~]$ route
    -bash: route: command not found
    [web@hnweb1 ~]$ /sbin/route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    10.66.10.0      *               255.255.255.128 U     0      0        0 eth0
    192.130.12.0    10.66.10.1      255.255.255.0   UG    0      0        0 eth0
    10.0.0.0        *               255.255.255.0   U     0      0        0 eth1
    10.66.0.0       10.66.10.1      255.255.0.0     UG    0      0        0 eth0
    134.161.0.0     10.66.10.1      255.255.0.0     UG    0      0        0 eth0
    10.20.0.0       10.66.10.1      255.255.0.0     UG    0      0        0 eth0
    169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
    172.224.0.0     10.66.10.1      255.255.0.0     UG    0      0        0 eth0
    default         10.66.10.22     0.0.0.0         UG    0      0        0 eth0
    [web@hnweb1 ~]$ route -n
    -bash: route: command not found
    [web@hnweb1 ~]$ /sbin/route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    10.66.10.0      0.0.0.0         255.255.255.128 U     0      0        0 eth0
    192.130.12.0    10.66.10.1      255.255.255.0   UG    0      0        0 eth0
    10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
    10.66.0.0       10.66.10.1      255.255.0.0     UG    0      0        0 eth0
    134.161.0.0     10.66.10.1      255.255.0.0     UG    0      0        0 eth0
    10.20.0.0       10.66.10.1      255.255.0.0     UG    0      0        0 eth0
    169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
    172.224.0.0     10.66.10.1      255.255.0.0     UG    0      0        0 eth0
    0.0.0.0         10.66.10.22     0.0.0.0         UG    0      0        0 eth0
    [web@hnweb1 ~]$

  • 相关阅读:
    移动布局---1. 移动端布局基础
    1. CSS新特性之选择器
    1. H5新增语义化标签
    POJ 3281
    poj 1986
    POJ 3728
    poj 2763
    poj 2749
    uva 11294
    LA 3713
  • 原文地址:https://www.cnblogs.com/sky7034/p/2107596.html
Copyright © 2020-2023  润新知