Linux系统的route命令用于显示和操作IP路由表(show?/?manipulate?the?IP?routing?table)。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置永久有效。
In Linux, the route is usually set to solve the following problems: the Linux system has a gateway in a local area network that allows the machine to access the Internet, and the IP address of the machine needs to be set as the default route for the Linux machine. It is important to note that it is executed directly under the command line to add a route, which will not be permanently preserved, when the netcard is re-opened or the machine is restarted; it is possible to add a root command in/etc/rc.local to ensure that the route is permanently effective.
1.命令格式:
1. Command format:
route?[-f]?[-p]?[Command?[Destination]?[mask?Netmask]?[Gateway]?[metric?Metric]]?[if?Interface]]?
2.命令功能:
2. Command function:
Route命令是用于操作基于内核ip路由表,它的主要作用是创建一个静态路由让指定一个主机或者一个网络通过一个网络接口,如eth0。当使用"add"或者"del"参数时,路由表被修改,如果没有参数,则显示路由表当前的内容。
The Route command is used to operate a kernel-based ip route table, the main function of which is to create a static router to specify a host or network through a network interface, e.g. eth0. When using 34; add& #34; or #34; del #34; the parameter, the route chart is modified or, if there are no parameters, the current content of the route chart is shown.
3.命令参数:
3. Command parameters:
-c?显示更多信息
-C-c? Show me more information.
-n?不解析名字
Don't parse names.
-v?显示详细的处理信息
Show me the details of the process.
-F?显示发送信息
-F? Show the sender.
-C?显示路由缓存
-C? Show route caches.
-f?清除所有网关入口的路由表。?
-F? Clear the route sheets for all gateway entrances?
-p?与?add?命令一起使用时使路由具有永久性。
-P-p? To make the route permanent when used with an order?
?
add:添加一条新路由。
Add a new route.
del:删除一条路由。
del: Delete a route.
-net:目标地址是一个网络。
-net: The target address is a network.
-host:目标地址是一个主机。
Target address is a host.
netmask:当添加一个网络路由时,需要使用网络掩码。
netmask: When a network route is added, a network mask is required.
gw:路由数据包通过网关。注意,你指定的网关必须能够达到。
gw: Passes through the gateway through the data pack. Note that the gateway you specify must be accessible.
metric:设置路由跳数。
Metric: Set the number of route jumps.
Command?指定您想运行的命令?(Add/Change/Delete/Print)。?
Command? Specifies the command you want to run? (Add/Change/Delete/Print).
Destination?指定该路由的网络目标。?
Destination? A network target for the route?
mask?Netmask?指定与网络目标相关的网络掩码(也被称作子网掩码)。?
Mask?Netmask? Specifies a network mask (also known as a subnet mask) associated with a network target?
Gateway?指定网络目标定义的地址集和子网掩码可以到达的前进或下一跃点?IP?地址。?
Gateway? Specify the address set and subnet mask that you can get to, or the next jump point? IP? address?
metric?Metric?为路由指定一个整数成本值标(从?1?至?9999),当在路由表(与转发的数据包目标地址最匹配)的多个路由中进行选择时可以使用。?
Metric? Specifies an integer cost marker for routers (from?1? to?9999), which can be used when selecting multiple routes in the route sheets (the most appropriate match to the transmitted data package target address).
if?Interface?为可以访问目标的接口指定接口索引。若要获得一个接口列表和它们相应的接口索引,使用?route?print?命令的显示功能。可以使用十进制或十六进制值进行接口索引。
If? Interface? Specifies the interface index for the interface that allows access to the target. To get a list of interfaces and their corresponding interface indexes, use the display function of the?route?print? command. You can index the interface using decimal or hexadecimal values.
4.使用实例:
4. Examples of use:
实例1:显示当前路由
Example 1: Show current route
命令:
Command:
route
route?-n
输出:
Output:
[root@localhost?~]#?route
Kernel?IP?routing?table
Destination?Gateway?Genmask?Flags?Metric?RefUse?Iface
192.168.120.0?*?255.255.255.0?U?000?eth0
e192.168.0.0?192.168.120.1?255.255.0.0?UG000?eth0
10.0.0.0192.168.120.1?255.0.0.0?UG000?eth0
default?192.168.120.240?0.0.0.0?UG000?eth0
[root@localhost?~]#?route?-n
Kernel?IP?routing?table
Destination?Gateway?Genmask?Flags?Metric?RefUse?Iface
192.168.120.0?0.0.0.0?255.255.255.0?U?000?eth0
192.168.0.0?192.168.120.1?255.255.0.0?UG000?eth0
10.0.0.0192.168.120.1?255.0.0.0?UG000?eth0
0.0.0.0?192.168.120.240?0.0.0.0?UG000?eth0
?
说明:
Note:
第一行表示主机所在网络的地址为192.168.120.0,若数据传送目标是在本局域网内通信,则可直接通过eth0转发数据包;
The first line indicates the address of the host network at 192.168.120.0, which can be transmitted directly through eth0 if the data transfer target is to communicate within the local area network;
第四行表示数据传送目的是访问Internet,则由接口eth0,将数据包发送到网关192.168.120.240
Line 4 indicates that the data transfer is to access the Internet, and the data package is sent to the gateway 192.168.120.240 by interface eth0
其中Flags为路由标志,标记当前网络节点的状态。
Of these, Flags is a route sign that marks the state of the current network node.
Flags标志说明:
The Flags logo explains:
U?Up表示此路由当前为启动状态
U?Up indicates that this path is currently starting state
H?Host,表示此网关为一主机
H? Host, indicate that this gateway is a host.
G?Gateway,表示此网关为一路由器
G? Gateway, indicate that this gateway is a router.
R?Reinstate?Route,使用动态路由重新初始化的路由
R?Reinstate? Route, use a dynamic route to re-initiate.
D?Dynamically,此路由是动态性地写入
D? Dynamically, this route is dynamically written.
M?Modified,此路由是由路由守护程序或导向器动态修改
M?Modified, this route is modified by routed daemon or guidanceer dynamics
!?表示此路由当前为关闭状态
%1 indicates that this path is currently closed
备注:
Remarks:
route?-n?(-n?表示不解析名字,列出速度会比route?快)
(-n? If you don't parse a name, it's faster than roote?)
实例2:添加网关/设置网关
Example 2: Add a gateway/set a gateway
命令:
Command:
route?add?-net?224.0.0.0?netmask?240.0.0.0?dev?eth0
输出:
Output:
[root@localhost?~]#?route?add?-net?224.0.0.0?netmask?240.0.0.0?dev?eth0
[root@localhost?~]#?route
Kernel?IP?routing?table
Destination?Gateway?Genmask?Flags?Metric?RefUse?Iface
192.168.120.0?*?255.255.255.0?U?000?eth0
192.168.0.0?192.168.120.1?255.255.0.0?UG000?eth0
10.0.0.0192.168.120.1?255.0.0.0?UG000?eth0
224.0.0.0?*?240.0.0.0?U?000?eth0
default?192.168.120.240?0.0.0.0?UG000?eth0
[root@localhost?~]#
说明:
Note:
增加一条?到达244.0.0.0的路由
Add a route to 244.0.0.
实例3:屏蔽一条路由
Example 3: Blocking a route
命令:
Command:
route?add?-net?224.0.0.0?netmask?240.0.0.0?reject
输出:
Output:
[root@localhost?~]#?route?add?-net?224.0.0.0?netmask?240.0.0.0?reject
[root@localhost?~]#?route
Kernel?IP?routing?table
Destination?Gateway?Genmask?Flags?Metric?RefUse?Iface
192.168.120.0?*?255.255.255.0?U?000?eth0
192.168.0.0?192.168.120.1?255.255.0.0?UG000?eth0
10.0.0.0192.168.120.1?255.0.0.0?UG000?eth0
224.0.0.0?-?240.0.0.0?!?0-0?-
224.0.0.0?*?240.0.0.0?U?000?eth0
default?192.168.120.240?0.0.0.0?UG000?eth0
?
说明:
Note:
增加一条屏蔽的路由,目的地址为?224.x.x.x?将被拒绝
Add a blocked route to the address? 224.x.x.x?
实例4:删除路由记录
Example 4: Delete route records
命令:
Command:
route?del?-net?224.0.0.0?netmask?240.0.0.0
route?del?-net?224.0.0.0?netmask?240.0.0.0?reject
输出:
Output:
[root@localhost?~]#?route
Kernel?IP?routing?table
Destination?Gateway?Genmask?Flags?Metric?RefUse?Iface
192.168.120.0?*?255.255.255.0?U?000?eth0
192.168.0.0?192.168.120.1?255.255.0.0?UG000?eth0
10.0.0.0192.168.120.1?255.0.0.0?UG000?eth0
224.0.0.0?-?240.0.0.0?!?0-0?-
224.0.0.0?*?240.0.0.0?U?000?eth0
default?192.168.120.240?0.0.0.0?UG000?eth0
[root@localhost?~]#?route?del?-net?224.0.0.0?netmask?240.0.0.0
[root@localhost?~]#?route
Kernel?IP?routing?table
Destination?Gateway?Genmask?Flags?Metric?RefUse?Iface
192.168.120.0?*?255.255.255.0?U?000?eth0
192.168.0.0?192.168.120.1?255.255.0.0?UG000?eth0
10.0.0.0192.168.120.1?255.0.0.0?UG000?eth0
224.0.0.0?-?240.0.0.0?!?0-0?-
default?192.168.120.240?0.0.0.0?UG000?eth0
[root@localhost?~]#?route?del?-net?224.0.0.0?netmask?240.0.0.0?reject
[root@localhost?~]#?route
Kernel?IP?routing?table
Destination?Gateway?Genmask?Flags?Metric?RefUse?Iface
192.168.120.0?*?255.255.255.0?U?000?eth0
192.168.0.0?192.168.120.1?255.255.0.0?UG000?eth0
10.0.0.0192.168.120.1?255.0.0.0?UG000?eth0
default?192.168.120.240?0.0.0.0?UG000?eth0
[root@localhost?~]#?
?
说明:
Note:
实例5:删除和添加设置默认网关
Example 5: Delete and add default gateways
命令:
Command:
route?del?default?gw?192.168.120.240
route?add?default?gw?192.168.120.240
输出:
Output:
[root@localhost?~]#?route?del?default?gw?192.168.120.240
[root@localhost?~]#?route
Kernel?IP?routing?table
Destination?Gateway?Genmask?Flags?Metric?RefUse?Iface
192.168.120.0?*?255.255.255.0?U?000?eth0
192.168.0.0?192.168.120.1?255.255.0.0?UG000?eth0
10.0.0.0192.168.120.1?255.0.0.0?UG000?eth0
[root@localhost?~]#?route?add?default?gw?192.168.120.240
[root@localhost?~]#?route
Kernel?IP?routing?table
Destination?Gateway?Genmask?Flags?Metric?RefUse?Iface
192.168.120.0?*?255.255.255.0?U?000?eth0
192.168.0.0?192.168.120.1?255.255.0.0?UG000?eth0
10.0.0.0192.168.120.1?255.0.0.0?UG000?eth0
default?192.168.120.240?0.0.0.0?UG000?eth0
[root@localhost?~]#?
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论