命令介绍

~ $route -help
route: invalid option -- 'h'
BusyBox v1.26.2 (2018-12-31 16:58:31 CST) multi-call binary.

Usage: route [{add|del|delete}]

Edit kernel routing tables

	-n	Don't resolve names
	-e	Display other/more information
	-A inet{6}	Select address family
翻译
~ $路线-
路由:无效的选项——'h'
BusyBox v1.26.2多调用二进制。

用法:路线({添加||删除})

编辑内核路由表

-n不解析名称
-e显示其他/更多信息
-A inet{6}选择地址族

一.路由表信息表上的意思

输出项说明
Destination目标网段或者主机
Gateway网关地址,”*” 表示目标是本主机所属的网络,不需要路由
Genmask网络掩码
主机路由的掩码为 255.255.255.255,默认路由的掩码为 0.0.0.0 掩码 255.255.255.255 表明只有精确匹配的目标使用此路由。掩码 0.0.0.0 表示任何目标都可以使用此路由。当以二进制形式撰写掩码时,1 表示重要(必须匹配),而 0 表示不重要(不需要匹配)
Flags标记。一些可能的标记如下:
U — 路由是活动的
H — 目标是一个主机
G — 路由指向网关
R — 恢复动态路由产生的表项
D — 由路由的后台程序动态地安装
M — 由路由的后台程序修改
! — 拒绝路由
Metric路由距离,到达指定网络所需的中转数(linux 内核中没有使用)
Ref路由项正在被使用的进程数
Use通过此路由项的分组数
Iface路由项对应的接口

二.路由表删除

用route -n查询 根据提供的信息删除路由表

cat /proc/net/route用文件的方式显示

~ $cat /proc/net/route 
Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask		MTU	Window	IRTT                                                       
wlan0	00000000	0102A8C0	0003	0	0	0	00000000	0	0	0                                                                              
eth0	00000000	0101A8C0	0003	0	0	0	00000000	0	0	0                                                                               
eth0	0001A8C0	00000000	0001	0	0	0	00FFFFFF	0	0	0                                                                               
wlan0	0002A8C0	00000000	0001	0	0	0	00FFFFFF	0	0	0                                                                       

用route -e的显示方式

~ $route -e
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         192.168.2.1     0.0.0.0         UG        0 0          0 wlan0
default         192.168.1.1     0.0.0.0         UG        0 0          0 eth0
192.168.1.0     *               255.255.255.0   U         0 0          0 eth0
192.168.2.0     *               255.255.255.0   U         0 0          0 wlan0

第一项不是IP的方式显示不好删除
用route -n的显示方式

~ $route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.2.1     0.0.0.0         UG    0      0        0 wlan0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0

删除第一个

~ $route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.2.1     0.0.0.0         UG    0      0        0 wlan0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
~ $route del -net 0.0.0.0 gw 192.168.2.1 netmask 0.0.0.0 dev wlan0
~ $route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0

删除第二个

~ $route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
~ $route del -net 0.0.0.0 gw 192.168.1.1 netmask 0.0.0.0 dev eth0
~ $route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0

删除第三个

~ $route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
~ $route del -net 192.168.1.0 gw 0.0.0.0 netmask 255.255.255.0 dev eth0
~ $route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0

删除第四个

~ $route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
~ $route del -net 192.168.2.0 gw 0.0.0.0 netmask 255.255.255.0 dev wlan0
~ $route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

三.添加路由表

把上面2的del换成add就行,其他的参数根据实际情况设置,如添加wifi的

~ $route add-net 192.168.2.0 gw 0.0.0.0 netmask 255.255.255.0 dev wlan0 metric 0
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐