route (显示或修改IP 路由表)

瑞兹 发表于 2020-11-25 09:06
浏览次数:
在手机上阅读

在类似Unix的操作系统上,route命令显示或修改IP 路由表。

查看英文版

目录

1 route 运行系统环境

2 route 描述

3 route 语法

4 route 例子

route 运行系统环境

Linux

route 描述

在计算机网络中,路由器是负责转发网络流量的设备。当数据报到达路由器时,路由器必须确定将其路由到目的地的最佳方法。

在Linux,BSD和其他类似Unix的系统上,route命令用于查看和更改内核路由表。在不同的系统上,命令语法不同。在这里,当涉及特定的命令语法时,我们将讨论Linux版本。

在命令行中运行route,不带任何选项将显示路线表条目:

route
内核IP路由表
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.2     0.0.0.0         UG    1024   0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

这向我们展示了系统当前的配置方式。如果一个数据包进入系统,并且目的地在192.168.1.0192.168.1.255范围内,则它将转发到网关*,它是0.0.0.0 —一个特殊的地址,表示无效或不存在的目的地。因此,在这种情况下,我们的系统将不会路由这些数据包。

如果目的地不在此IP地址范围内,则将其转发到默认网关(在本例中为192.168.1.2),该系统将确定如何将流量转发到目的地。

In computer networking, a router is a device responsible for forwarding network traffic. When datagrams arrive at a router, the router must determine the best way to route them to their destination.

On Linux, BSD, and other Unix-like systems, the route command is used to view and make changes to the kernel routing table. The command syntax is different on different systems; here, when it comes to specific command syntax, we'll be discussing the Linux version.

Running route at the command line without any options displays the routing table entries:

route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.2     0.0.0.0         UG    1024   0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

This shows us how the system is currently configured. If a packet comes into the system and has a destination in the range 192.168.1.0 through 192.168.1.255, then it is forwarded to the gateway *, which is 0.0.0.0 — a special address which represents an invalid or non-existant destination. So, in this case, our system will not route these packets.

If the destination is not in this IP address range, it is forwarded to the default gateway (in this case, 192.168.1.2, and that system will determine how to forward the traffic on to the next step towards its destination.

查看英文版

查看中文版

route 语法

route [-CFvnee]
route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] 
      [metric N] i [mss M] [window W] [irtt m] [reject] [mod] [dyn] 
      [reinstate] [[dev] If]
route [-v] [-A family] del [-net|-host] target [gw Gw] [netmask Nm] 
      [metric N] [[dev] If]
route [-V] [--version] [-h] [--help]

技术说明

route操作内核的IP路由表。它的主要用途是在使用ifconfig程序配置接口后,通过接口设置到特定主机或网络的静态 路由。

使用adddel选项时,route修改路由表。如果没有这些选项,route将显示路由表的当前内容。

选件

-A family 使用指定的地址族(例如“ inet ”;使用“ route --help ”获得完整列表)。
-F 在内核的FIB(转发信息库)路由表上运行。这是默认值。
-C 在内核的路由缓存上运行。
-v 选择详细操作。
-n 显示数字地址,而不是尝试确定符号主机名。如果您要确定到名称服务器的路由消失的原因,这很有用。
-e 使用netstat -format显示路由表。-ee将使用路由表中的所有参数生成很长的一行。
del 删除路线。
add 添加新路线。
target 目标网络或主机。您可以用点分十进制或主机/网络名称提供IP地址。
-net 目标是网络。
-host 目标是主机。
netmask NM 添加网络路由时,要使用的网络掩码。
gw GW 通过网关路由数据包。注意:必须首先可以访问指定的网关。这通常意味着您必须事先设置到网关的静态路由。如果您指定本地 接口之一的地址,它将用于确定数据包应路由到的接口。这是BSD风格的兼容性技巧。
metric M 在路由表中的度量字段(由路由用于设置守护程序),以中号
MSS M 将通过此路由的连接的TCP最大段大小(MSS)设置为M个字节。缺省值为设备MTU减去标题,或在发生路径mtu发现时使用较低的MTU。当路径mtu发现不起作用时(通常是由于配置错误的防火墙阻止了所需的ICMP碎片),此设置可用于在另一端强制使用较小的TCP数据包。
window W 将通过此路由的连接的TCP窗口大小设置为W 个字节。仅在AX.25网络上使用,并且驱动程序无法处理背靠背帧。
irtt m 设置初始往返时间(irtt TCP连接)在此路由, 毫秒(1 - 12000)。仅在AX.25网络上使用。如果省略,则使用RFC 1122默认值300ms
reject 安装阻塞路由,这将强制路由查找失败。例如,这用于在使用默认路由之前屏蔽网络。这不是用于防火墙。
moddynreinstate 安装动态或修改后的路线。这些标志用于诊断目的,通常仅由路由守护程序设置。
dev If 强制将路由与指定设备相关联,否则内核将尝试自行确定设备(通过检查现有路由和设备规格以及添加路由的位置)。在大多数普通网络中,您将不需要此功能。

如果dev If是命令行上的最后一个选项,则可以省略单词dev,因为它是默认值。否则,路由修饰符(metric - netmask - gw - dev)的顺序无关紧要。
route [-CFvnee]
route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] 
      [metric N] i [mss M] [window W] [irtt m] [reject] [mod] [dyn] 
      [reinstate] [[dev] If]
route [-v] [-A family] del [-net|-host] target [gw Gw] [netmask Nm] 
      [metric N] [[dev] If]
route [-V] [--version] [-h] [--help]

Technical description

route manipulates the kernel's IP routing tables. Its primary use is to set up static routes to specific hosts or networks via an interface after it was configured with the ifconfig program.

When the add or del options are used, route modifies the routing tables. Without these options, route displays the current contents of the routing tables.

Options

-A family use the specified address family (eg 'inet'; use 'route --help' for a full list).
-F operate on the kernel's FIB (Forwarding Information Base) routing table. This is the default.
-C operate on the kernel's routing cache.
-v select verbose operation.
-n show numerical addresses instead of trying to determine symbolic hostnames. This is useful if you are trying to determine why the route to your nameserver has vanished.
-e use netstat-format for displaying the routing table. -ee will generate a very long line with all parameters from the routing table.
del delete a route.
add add a new route.
target the destination network or host. You can provide IP addresses in dotted decimal or host/network names.
-net the target is a network.
-host the target is a host.
netmask NM when adding a network route, the netmask to be used.
gw GW route packets via a gateway. NOTE: The specified gateway must be reachable first. This usually means that you have to set up a static route to the gateway beforehand. If you specify the address of one of your local interfaces, it will be used to decide about the interface to which the packets should be routed. This is a BSD-style compatibility hack.
metric M set the metric field in the routing table (used by routing daemons) to M.
mss M set the TCP Maximum Segment Size (MSS) for connections over this route to M bytes. The default is the device MTU minus headers, or a lower MTU when path mtu discovery occurred. This setting can be used to force smaller TCP packets on the other end when path mtu discovery does not work (usually because of misconfigured firewalls that block ICMP Fragmentation Needed)
window W set the TCP window size for connections over this route to W bytes. This is only used on AX.25 networks and with drivers unable to handle back to back frames.
irtt m set the initial round trip time (irtt) for TCP connections over this route to m milliseconds (1-12000). This is only used on AX.25 networks. If omitted the RFC 1122 default of 300ms is used.
reject install a blocking route, which will force a route lookup to fail. This is for example used to mask out networks before using the default route. This is NOT for firewalling.
moddynreinstate install a dynamic or modified route. These flags are for diagnostic purposes, and are generally only set by routing daemons.
dev If force the route to be associated with the specified device, as the kernel will otherwise try to determine the device on its own (by checking already existing routes and device specifications, and where the route is added to). In most normal networks you won't need this.

If dev If is the last option on the command line, the word dev may be omitted, as it's the default. Otherwise, the order of the route modifiers (metric - netmask - gw - dev) doesn't matter.

查看英文版

查看中文版

route 例子

route -n

显示绑定到服务器的所有IP的路由表。

route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0

通过“ eth0”添加到网络192.56.76.x的路由这里实际上并不需要C类网络掩码修饰符,因为> 192. *是C类IP地址。这里可以省略单词“ dev ”。

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

此命令将所有D类(多播)IP路由都设置为通过“ eth0 ”。这是多播内核的正确常规配置。

route -n

Shows routing table for all IPs bound to the server.

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 command sets all of the class D (multicast) IP routes to go via "eth0". This is the correct normal configuration for a multicasting kernel.

查看英文版

查看中文版

其他命令行

renice | replace | rm | rmdir | rn | rpcinfo | rename | rcp | readlink | rehash | rsh | rlogin | rmmod |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条