route -n 讲解

我们经常会出现临时添加路由,或者是路由重启路由丢失等导致网络不通的情况,上网查发现很多介绍或者没有实验跟进
导致理解的时候很费劲的情况,可能人家认为是比较简单的事情了,但是很多不尽然,老手也不一定能很快解决这样问题。
所以这里还是以添加的实验结果为主。

route 输出项 说明

Destination 目标网段或者主机
Gateway 网关地址,”*” 表示目标是本主机所属的网络,不需要路由
Genmask 网络掩码,可以设定 netmask 决定网域的大小
Flags 标记。一些可能的标记如下:
  U — 路由是活动的,可以使用
  H — 目标是一个主机,H 该路由是到一个主机,也就是说,
目的地址是一个完整的主机地址。如果没有设置该标志,说明该路由是到一个网络,
而目的地址是一个网络地址:一个网络号,或者网络号与子网号的组合
  G — 路由指向网关
  R — 恢复动态路由产生的表项
  D — 由路由的后台程序动态地安装
  M — 由路由的后台程序修改
  ! — 拒绝路由
Metric 路由距离,到达指定网络所需的中转数(linux 内核中没有使用)
Ref 路由引用次数(linux 内核中没有使用)
Use 此路由项被路由软件查找的次数
Iface 转发的网络设备即虚拟网卡名称

添加网段路由,想要只显示U

 [root@ht23 k8snode]# route add -net 192.168.1.0 netmask 255.255.255.0 dev ens192

[root@ht23 k8snode]# 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 ens192
flags:u
//意思是192.168.1.0网段全部可以通过
//Flags:U

添加主机路由,显示UH

[root@ht23 k8snode]# route add -host 192.168.1.1 dev ens192  //添加成功,不需要指定掩码注意

[root@ht23 k8snode]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.1.1     0.0.0.0         255.255.255.255 UH    0      0        0 ens192

//显示UH,表示主机192.168.1.1这台机器,可以被ens192网卡转发出去.

添加网关路由,显示UG

 [root@etcd2 system]#route add default gw 10.129.55.1

 [root@etcd2 system]#route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.129.55.1
 [root@etcd2 system]#route del default gw 10.129.55.1

[root@etcd2 system]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.129.55.1     0.0.0.0         UG    0      0        0 ens192
//表示去往任何网段的数据包即所有目的地的数据包由网关10.129.55.1通过网卡ens192来转发
//添加了一个网关 ,Flags:UG

 添加网段错误,不能指定ip

错误 -net 只能指定网段,不能指定具体ip
[root@nagios ~]# route add -net 211.211.10.69 netmask 255.255.255.0 dev ens192
route: netmask doesn't match route address
//正确方法,指定网段 [root@nagios ~]# route add -net 211.211.10.0 netmask 255.255.255.0 dev ens192

[root@ht23 k8snode]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface

211.211.10.0    0.0.0.0         255.255.255.0   U     0      0        0 ens192
//显示结果是Flags是U

添加主机路由错误

添加路由和删除路由时报错记录:

//添加路由,出错1,网卡不存在

[root@ht23 k8snode]# route add -host 192.168.1.1 dev eth0
SIOCADDRT: No such device //因为网卡名称不对
[root@ht23 k8snode]# route add -host 192.168.1.1 dev ens192  //添加成功

[root@ht23 k8snode]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.1.1     0.0.0.0         255.255.255.255 UH    0      0        0 ens192

//显示UH,表示主机192.168.1.1这台机器,可以被ens192网卡转发出去.

添加一个主机路由,不能指定子网掩码

[root@ht23 k8snode]# route add -host 10.129.59.11 netmask 255.255.255.0 dev ens192
route: netmask 000000ff doesn't make sense with host route

//正确的方法 
[root@ht23 k8snode]# route add -host 10.129.59.11 dev ens192

 [root@ht23 k8snode]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.129.59.11     0.0.0.0         255.255.255.255 UH    0      0        0 ens192
//显示UH
//如果你重复添加,则提示已存在

[root@ht23 k8snode]# route add -host 192.168.1.1 dev ens192
SIOCADDRT: File exists

//删除该主机路由

[root@ht23 k8snode]# route del -host 192.168.1.1 dev ens192

 你要添加的网关不在你主机所在的网段

SIOCADDRT: No such process 解决方法:
比如你要添加的网关是10.128.51.1 但是本机没有10.128.51段的网卡
[root@ht23 k8snode]# route add 10.128.51.1/32 dev ens192 
然后再 
[root@ht23 k8snode]#route add default gw 10.128.51.1

实例演示:
 [root@ht23 k8snode]# route add 10.128.51.1/32 dev ens192 
[root@ht23 k8snode]# route add default gw 10.128.51.1
[root@ht23 k8snode]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.128.51.1     0.0.0.0         UG    0      0        0 ens19210.128.51.1     0.0.0.0         255.255.255.255 UH    0      0        0 ens192

 

在linux下永久添加静态路由

一、让路由重启服务器后生效:

1,在/etc/rc.local里面添加:

添加路由命令为:直接将在命令行操作的命令复制到该文件中,保存退出即可。

二、两种方式添加静态路由对比:

1、rc.local:

  重启服务器生效;

  重启网络服务,则静态路由失效;

  rc.local是系统启动后最后运行的一个脚本,因此如果有如NFS需要网络才能挂载的服务需求,则该方式不适合;

2、static-routes:

  重启服务器生效;

  重启网络服务生效:

 




posted @ 2022-02-19 18:04  jinzi  阅读(4099)  评论(0编辑  收藏  举报