每天一个Linux命令(50):route
来源:互联网 发布:小公司网络组建 编辑:程序博客网 时间:2024/06/05 21:09
route用于显示和操作IP路由表。要实现两个不同子网之间的通信,需要一台连接两个网络的路由器或者同时位于两个子网的网关来实现。
在Linux系统中,设置路由通常是为了解决以下问题:Linux系统在一个局域网中,局域网有一个网关,可以让系统访问Internet,那么就需要将这台机器的IP地址设置为Linux系统的默认路由。要注意的是,直接在命令行下执行route
命令添加路由,系统不会永久保存,当网卡重启或者系统重启之后,该路由就失效了。可以在/etc/rc.local中添加route
命令,保证该路由设置永久有效。
1. 命令格式
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric] [if Interface]
2. 命令功能
用于操作基于内核IP的路由表,它的主要作用是创建一个静态路由,让指定的一个主机或者一个网络地址通过一个网络接口,如eth0。
3. 命令参数
-c,显示更多信息
-n,不解析名字
-v,显示详细的处理信息
-F,显示发送信息
-C,显示路由缓存
-f,清除所有网关入口的路由表
-p,与add命令一起使用时,路由具有永久性
add,添加一条新路由
del,删除一条路由
-net,目标IP地址是一个网络
-host,目标IP地址是一台主机
netmask,当添加一个网络路由时,需要使用网络掩码
gw,路由数据包通过网关。注意,你指定的网关必须能够到达
metric,设置路由跳数
Command,指定你想运行的命令 (Add/Change/Delete/Print)
Destination,指定该路由的网络目标
mask Netmask,指定与网络目标相关的网络掩码(也被称作子网掩码)
Gateway,指定网络目标定义的地址集和子网掩码可以到达的前个或下个跃点的 IP地址
metric Metric,为路由指定一个整数成本值标(从 1 至 9999),当在路由表(与转发的数据包目标地址最匹配)的多个路由中进行选择时可以使用
if Interface,为可以访问目标的接口指定接口索引。若要获得一个接口列表和它们相应的接口索引,使用route print
命令的显示功能。可以使用十进制或十六进制值进行接口索引
4. 使用实例
例一:显示当前路由
命令:
routeroute -n
# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0e192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth00.0.0.0 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
说明:192.168.120.0代表主机的IP地址,若数据传送的目标是在本局域网内,则可直接通过eth0转发数据包。第六行,由接口eth0将数据包发送到网关192.168.120.240,表示数据传送目的是访问Internet。
其中Flags为路由标志,用于标记当前网络节点的状态。
U,Up,表示此路由当前为启动状态
H,Host,表示此网关为一主机
G,Gateway,表示此网关为一路由器
R,Reinstate Route,使用动态路由重新初始化的路由
D,Dynamically,此路由是动态性地写入
M,Modified,此路由是由路由守护程序或导向器动态修改
!,表示当前此路由为关闭状态
备注:route -n
,-n表示不解析名字,列出速度比route
快。
例二:添加/设置路由
命令: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 dev eth0//在eth0上,增加一条到达244.0.0.0的路由# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0224.0.0.0 * 240.0.0.0 U 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
例三:屏蔽一条路由
命令:route add -net 224.0.0.0 netmask 240.0.0.0 reject
说明:增加一条屏蔽的路由,目的地址224.x.x.x将被拒绝。
# route add -net 224.0.0.0 netmask 240.0.0.0 reject# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0224.0.0.0 - 240.0.0.0 ! 0 - 0 -224.0.0.0 * 240.0.0.0 U 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
例四:删除一条路由记录
命令:
route del -net 224.0.0.0 netmask 240.0.0.0route del -net 224.0.0.0 netmask 240.0.0.0 reject
# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0224.0.0.0 - 240.0.0.0 ! 0 - 0 -224.0.0.0 * 240.0.0.0 U 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0# route del -net 224.0.0.0 netmask 240.0.0.0# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0224.0.0.0 - 240.0.0.0 ! 0 - 0 -default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0# route del -net 224.0.0.0 netmask 240.0.0.0 reject# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
例五:删除/添加默认网关
命令:
route del default gw 192.168.120.240route add default gw 192.168.120.240
# route del default gw 192.168.120.240# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0# route add default gw 192.168.120.240# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
- 每天一个Linux命令(50):route
- 每天一个linux命令:route命令
- 每天一个linux命令(53):route命令
- 每天一个linux命令(53):route命令
- 每天一个linux命令(53):route命令
- 每天一个linux命令(53):route命令
- 每天一个linux命令(53):route命令
- 每天一个linux命令(18):route命令
- 每天一个linux命令(53):route命令
- 每天一个linux命令(53):route命令
- 每天一个linux命令(53):route命令 【转】
- 每天一个linux命令(41):route命令
- 每天一个Linux命令(53):route命令
- 每天一个linux命令(53):route命令
- 每天一个linux命令(53):route命令
- 每天一个linux命令
- 每天一个linux命令
- 每天一个linux命令
- LeetCode笔记:386. Lexicographical Numbers
- CentOS 7 安装GitLab
- Linux下打补丁
- Java程序员必须掌握的8大排序算法
- Java虚拟机类加载机制
- 每天一个Linux命令(50):route
- 轻量级操作系统FreeRTOS的内存管理机制(一)
- IP地址的CString型和DWORD型转换
- 青涩的时光写给未来的自己——程序员不能Low
- SICP 1.3
- JS读取/创建本地文件及目录文件夹的方法
- session.invalidate();详细解释
- 【LeetCode】 328. Odd Even Linked List C语言
- Java类加载器总结