Linux 网络层 路由 route 命令

网友投稿 1285 2022-11-30

Linux 网络层 路由 route 命令

Linux 网络层 路由 route 命令

route 管理路由表

要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为 Linux机器的默认路由。

很多情况下,人们把网关就叫做路由器。其实不完全准确,而另一种比喻更加恰当:路由器是一台设备,它有五个网口或者网卡,相当于有五只手,分别连着五个局域网。每只手的 IP 地址都和局域网的 IP 地址相同的网段,每只手都是它握住的那个局域网的网关。

一、查看路由信息

$ route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.55.1 0.0.0.0 UG 600 0 0 wlp3s0169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 wlp3s0192.168.55.0 0.0.0.0 255.255.255.0 U 600 0 0 wlp3s0[root@master ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 eth010.233.70.0 0.0.0.0 255.255.255.0 U 0 0 0 *10.233.70.18 0.0.0.0 255.255.255.255 UH 0 0 0 calidad8634cbdd10.233.70.20 0.0.0.0 255.255.255.255 UH 0 0 0 caliac4b211f13110.233.70.22 0.0.0.0 255.255.255.255 UH 0 0 0 cali88530cc7bcc10.233.70.23 0.0.0.0 255.255.255.255 UH 0 0 0 cali7bd9305546710.233.70.26 0.0.0.0 255.255.255.255 UH 0 0 0 calidda57e6811210.233.70.27 0.0.0.0 255.255.255.255 UH 0 0 0 caliad466b050ae10.233.90.0 192.168.0.3 255.255.255.0 UG 0 0 0 tunl010.233.96.0 192.168.0.4 255.255.255.0 UG 0 0 0 tunl0172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

显示信息说明

标题

说明

Destination

目标网段或者主机

Gateway

网关地址,​*​ 表示目标是本主机所属的网络,不需要路由

Genmask

网络掩码

Flags

标记;

​​U​: 路由是活动的;

​​H​: 目标是一个主机;

​​G​: 路由指向网关;

​R​​: 恢复动态路由产生的表项;

​D​​: 由路由的后台程序动态地安装;

​M​​: 由路由的后台程序修改;

​!​​: 拒绝路由;

Metric

路由距离,到达指定网络所需的中转数

Ref

路由项引用次数\

Use

此路由项被路由软件查找的次数

Iface

该路由表项对应的输出接口

对于0.0.0.0有一点不了解。这是查到的对于0.0.0.0的解释:

If used in a routing table, it identifies the default gateway; a route to 0.0.0.0 is the default one, i.e. the one used when there is not any more specific route available to a destination address.

问题一:Destination中这个0.0.0.0代表的是啥?是匹配所有的目标ip吗,如果这样的话,后面两条记录不就失去意义了吗?

答:0.0.0.0代表的是匹配所有目标地址,但注意默认网关的描述​​the one used when there is not any more specific route available to a destination address​​​,一般路由匹配要符合​​掩码最长匹配​​原则,而默认路由的掩码是最短的,它只有在没有其他匹配条目的时候才会被选择。

[root@master ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 eth0$ route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.55.1 0.0.0.0 UG 600 0 0 wlp3s0

问题二:第二条和第三条记录Gateway中0.0.0.0表示的default gateway又是哪一个网关?而第一条记录查处的Gateway 192.168.55.1这个网关地址就是家里的路由器吗?

这两条记录中的gateway并没有意义,​​Flags​​​那一列中有​​G​​​时才会使用​​Gateway​​​。这两条路由并没有这样的标志,由于它们是本地的,匹配这些条目的数据包会直接通过​​Iface​​列中的网卡发送出去。

[root@master ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

查看参数说明

参数

说明

-c

显示更多信息

-n

解析名字

-v

显示详细的处理信息

-F

显示发送信息

-C

显示路由缓存

二、配置静态路由

命令

route [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]

参数说明

参数

说明

add

添加一条路由规则

del

删除一条路由规则

-net

目的地址是一个网络

-host

目的地址是一个主机

target

目的网络或主机

netmask

目的地址的网络掩码

gw

路由数据包通过的网关

dev

为路由指定的网络接口

示例:

添加默认网关 为10.0.0.1root@text:~# route add default gw 10.0.0.1删除默认网关root@text:~# route del default gw 10.0.0.1添加到主机的路由root@text:~# route add -host 192.168.10.2 dev eth3root@text:~# route -nDestination Gateway Genmask Flags Metric Ref Use Iface192.168.10.2 * 255.255.255.255 UH 0 0 0 eth3删除到主机的路由root@text:~# route del -host 192.168.10.2 dev eth3添加到网络的路由root@text:~# route add -net 192.168.55.0 netmask 255.255.255.0 eth3删除到网络的路由root@text:~# route del -net 192.168.55.0 netmask 255.255.255.0 eth3

[root@k8s-master ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.179.2 0.0.0.0 UG 100 0 0 ens3310.244.0.0 0.0.0.0 255.255.255.0 U 0 0 0 cni010.244.1.0 10.244.1.0 255.255.255.0 UG 0 0 0 flannel.110.244.2.0 10.244.2.0 255.255.255.0 UG 0 0 0 flannel.1172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0192.168.179.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33[root@k8s-master ~]# ping baidu.comPING wshifen.com (103.235.46.39) 56(84) bytes of data.64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=1 ttl=128 time=71.8 ms64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=2 ttl=128 time=72.7 ms^C--- wshifen.com ping statistics ---3 packets transmitted, 2 received, 33% packet loss, time 2007msrtt min/avg/max/mdev = 71.877/72.297/72.718/0.499 ms[root@k8s-master ~]# route del default gw 192.168.179.2[root@k8s-master ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface10.244.0.0 0.0.0.0 255.255.255.0 U 0 0 0 cni010.244.1.0 10.244.1.0 255.255.255.0 UG 0 0 0 flannel.110.244.2.0 10.244.2.0 255.255.255.0 UG 0 0 0 flannel.1172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0192.168.179.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33[root@k8s-master ~]# ping baidu.comping: baidu.com: Name or service not known

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:K8S NetworkPolicy网络策略介绍与实战
下一篇:sonarqube api接口
相关文章

 发表评论

暂时没有评论,来抢沙发吧~