Linux 网络层 路由 route 命令

route 管理路由表


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

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

一、查看路由信息


$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.55.1    0.0.0.0         UG    600    0        0 wlp3s0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 wlp3s0
192.168.55.0    0.0.0.0         255.255.255.0   U     600    0        0 wlp3s0



[root@master ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eth0
10.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 calidad8634cbdd
10.233.70.20    0.0.0.0         255.255.255.255 UH    0      0        0 caliac4b211f131
10.233.70.22    0.0.0.0         255.255.255.255 UH    0      0        0 cali88530cc7bcc
10.233.70.23    0.0.0.0         255.255.255.255 UH    0      0        0 cali7bd93055467
10.233.70.26    0.0.0.0         255.255.255.255 UH    0      0        0 calidda57e68112
10.233.70.27    0.0.0.0         255.255.255.255 UH    0      0        0 caliad466b050ae
10.233.90.0     192.168.0.3     255.255.255.0   UG    0      0        0 tunl0
10.233.96.0     192.168.0.4     255.255.255.0   UG    0      0        0 tunl0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eth0

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.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.1
root@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 eth3
root@text:~# route -n
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.179.2   0.0.0.0         UG    100    0        0 ens33
10.244.0.0      0.0.0.0         255.255.255.0   U     0      0        0 cni0
10.244.1.0      10.244.1.0      255.255.255.0   UG    0      0        0 flannel.1
10.244.2.0      10.244.2.0      255.255.255.0   UG    0      0        0 flannel.1
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.179.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@k8s-master ~]# ping www.baidu.com
PING www.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 ms
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=2 ttl=128 time=72.7 ms
^C
--- www.wshifen.com ping statistics ---
3 packets transmitted, 2 received, 33% packet loss, time 2007ms
rtt 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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.244.0.0      0.0.0.0         255.255.255.0   U     0      0        0 cni0
10.244.1.0      10.244.1.0      255.255.255.0   UG    0      0        0 flannel.1
10.244.2.0      10.244.2.0      255.255.255.0   UG    0      0        0 flannel.1
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.179.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@k8s-master ~]# ping www.baidu.com
ping: www.baidu.com: Name or service not known

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值