linux网络运维命令--route
简介
- route - show / manipulate the IP routing table
- Route操作内核的IP路由表。它的主要用途是在使用ifconfig(8)程序配置后,通过接口建立到特定主机或网络的静态路由。
- 当使用add或del选项时,route会修改路由表。如果没有这些选项,route将显示路由表的当前内容。
- 要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置永久有效。
下载安装
yum install net-tools
使用用法
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]
-c 显示更多信息
-n 不解析名字
-v 显示详细的处理信息
-F 显示发送信息
-C 显示路由缓存
-f 清除所有网关入口的路由表。
-p 与 add 命令一起使用时使路由具有永久性
add : 添加一条新路由
del : 删除一条路由
-net : 目标地址是一个网络
-host : 目标地址是一个主机
netmask : 当添加一个网络路由时,需要使用网络掩码
gw : 路由数据包通过网关。注意,你指定的网关必须能够达到
metric : 设置路由跳数
输出
[root@master ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.209.2 0.0.0.0 UG 100 0 0 ens33
10.10.10.0 - 255.255.255.0 ! 0 - 0 -
192.168.209.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
输出说明:
内核路由表的输出按以下列组织
Destination : 目标网络或目标主机。
Gateway : 网关地址或'*',如果没有设置。
Genmask : 目的网络的子网掩码;'255.255.255.255'表示主机目的地址,'0.0.0.0'表示默认路由。
Flags :
U(路线向上)
H(目标为主机)
G(使用网关)
R(恢复动态路由路由)
D(通过守护进程或重定向动态安装)
M(来自路由守护进程或重定向)
A(通过addrconf安装)
C(缓存条目)
! (拒绝)
Metric : 到目标的“距离”(通常以跳数计算)。最近的内核不使用它,但是路由守护进程可能需要它
Ref : 此路由的引用数。(Linux内核中不使用。)
Use : 对路由的查找计数。根据-F和-C的使用,这将是路由缓存未命中(-F)或命中(-C)
Iface : 该路由的报文将被发送到的接口
示例
查询路由
[root@master ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.209.2 0.0.0.0 UG 100 0 0 ens33
192.168.209.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
添加路由
[root@master ~]# route add -net 10.10.10.0 gw 192.168.209.2 netmask 255.255.255.0 dev ens33
[root@master ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.209.2 0.0.0.0 UG 100 0 0 ens33
10.10.10.0 192.168.209.2 255.255.255.0 UG 0 0 0 ens33
192.168.209.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
删除路由
[root@master ~]# route del -net 10.10.10.0 gw 192.168.209.2 netmask 255.255.255.0 dev ens33
[root@master ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.209.2 0.0.0.0 UG 100 0 0 ens33
192.168.209.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
添加阻断路由与删除
[root@master ~]# route add -net 10.10.10.0 netmask 255.255.255.0 reject
[root@master ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.209.2 0.0.0.0 UG 100 0 0 ens33
10.10.10.0 - 255.255.255.0 ! 0 - 0 -
192.168.209.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
[root@master ~]# route del -net 10.10.10.0 netmask 255.255.255.0 reject
[root@master ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.209.2 0.0.0.0 UG 100 0 0 ens33
192.168.209.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现