metric
配置网卡的默认metric
给网卡配置默认的metric值,当添加路由的时候如果没有手动指定 metric 值,则使用默认值,默认值为 -1 ,表示自动配置路由的metric值。metric
数值越大,路由的优先级越低。
没有 NetworkManager 的情况下设置
修改位于/etc/sysconfig/network-scripts/
目录下的ifcfg-<interface>
文件,<interface>
是接口的名称,添加以下内容
METRIC=XXXX
重新启动network
服务生效
service network restart
使用 NetworkManager 配置metric
使用nmcli
,nmtui
,或者GUI
修改路由的Metric
例如,添加一条名为的连接external
的 metric
nmcli connection modify external ipv4.route-metric 600
重新加载连接生效
nmcli connection up external
注意:ipv4.route-metric
参数可以更改所有该连接对应网卡的所有路由的 metric 值,但是。。。对应手动指定了metric 值的路由,是不会修改的。也就是配置网卡的默认metric,-1表示自动配置metric
[root@oracle-linux7-9-5 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 ens33
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
172.16.0.0 0.0.0.0 255.255.255.0 U 105 0 0 ens35
172.16.1.0 0.0.0.0 255.255.255.0 U 600 0 0 ens36
192.168.1.0 172.16.0.1 255.255.255.0 UG 105 0 0 ens35
192.168.1.0 172.16.1.1 255.255.255.0 UG 600 0 0 ens36
[root@oracle-linux7-9-5 ~]# nmcli connection show vpn2 | grep -E 'routes|metric|ens36'
connection.interface-name: ens36
ipv4.routes: { ip = 192.168.1.0/24, nh = 172.16.1.1 }
ipv4.route-metric: 600
ipv4.ignore-auto-routes: no
ipv6.routes: --
ipv6.route-metric: -1
ipv6.ignore-auto-routes: no
GENERAL.DEVICES: ens36
# 可以看到网络连接vpn2,对应的路由条目没有指定metric值,所以该网卡对应的所有路由条目的metric值都是ipv4.route-metric的值
范例:手动修改路由的metric值
[root@oracle-linux7-9-5 ~]# nmcli connection modify vpn2 ipv4.routes '192.168.1.0/24 172.16.1.1 300'
[root@oracle-linux7-9-5 ~]# nmcli connection up vpn2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/16)
[root@oracle-linux7-9-5 ~]# route -n | grep ens36
172.16.1.0 0.0.0.0 255.255.255.0 U 600 0 0 ens36
192.168.1.0 172.16.1.1 255.255.255.0 UG 300 0 0 ens36
修改路由的优先级
没有 NetworkManager 的情况下设置
创建或修改位于/etc/sysconfig/network-scripts/
目录下route-<interface>
文件,<interface>
是网卡接口的名称
文件的格式如下
<network/prefix> via <gateway> <metric "value">
<network/prefix> 指定目标IP或者网段
<gateway> 指定下一跳的地址
<metric "value"> 指定路由的优先级,metric值越高,路由的优先级越低
范例:
# cat /etc/sysconfig/network-scripts/route-<interface>
10.0.0.0/24 via 192.51.100.10 metric 200
10.0.1.0/24 via 192.51.10.20 metric 50
让路由生效
ifup <interface>
使用 NetworkManager 配置metric
-
修改现有连接的metric
nmcli connection modify ens192 ipv4.routes "10.0.0.0/24 192.51.100.10 400"
-
为一条连接添加路由并配置metric
nmcli connection modify external +ipv4.routes "10.0.0.0/24 192.0.2.30 350"
让配置生效
nmcli connection up ens192
大小路由优先级
在路由表中既有大路由,又包含小的路由,例如下面这种
10.0.0.0/16 via 192.51.100.10 metric 200 # 大路由
10.0.0.0/24 via 192.51.10.20 metric 50 # 小路由
实验环境
小路由VPN1,网卡ens35,对应网关172.16.0.1
大路由VPN2,网卡ens36,对应网关172.16.1.1
# 配置小路由
[root@oracle-linux7-9-5 ~]# nmcli connection modify vpn1 ipv4.routes '192.168.1.0/24 172.16.0.1 200'
[root@oracle-linux7-9-5 ~]# nmcli connection show vpn1 | egrep 'metric|routes|ens35'
connection.interface-name: ens35
ipv4.routes: { ip = 192.168.1.0/24, nh = 172.16.0.1, mt = 200 }
ipv4.route-metric: -1
ipv4.ignore-auto-routes: no
ipv6.routes: --
ipv6.route-metric: -1
ipv6.ignore-auto-routes: no
GENERAL.DEVICES: ens35
# 配置大路由
[root@oracle-linux7-9-5 ~]# nmcli connection modify vpn2 ipv4.routes '192.168.0.0/16 172.16.1.1 200'
[root@oracle-linux7-9-5 ~]# nmcli connection up vpn2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/30)
[root@oracle-linux7-9-5 ~]# nmcli connection show vpn2 | egrep 'metric|routes|ens36'
connection.interface-name: ens36
ipv4.routes: { ip = 192.168.0.0/16, nh = 172.16.1.1, mt = 200 }
ipv4.route-metric: -1
ipv4.ignore-auto-routes: no
ipv6.routes: --
ipv6.route-metric: -1
ipv6.ignore-auto-routes: no
GENERAL.DEVICES: ens36
# 最终路由,大路由的网关是172.16.1.1,小路由是172.16.0.1
[root@oracle-linux7-9-5 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 ens33
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
172.16.0.0 0.0.0.0 255.255.255.0 U 113 0 0 ens35
172.16.1.0 0.0.0.0 255.255.255.0 U 114 0 0 ens36
192.168.0.0 172.16.1.1 255.255.0.0 UG 200 0 0 ens36
192.168.1.0 172.16.0.1 255.255.255.0 UG 200 0 0 ens35
# 发现是小的路由优先生效
[root@oracle-linux7-9-5 ~]# traceroute -n 192.168.1.9
traceroute to 192.168.1.9 (192.168.1.9), 30 hops max, 60 byte packets
1 172.16.0.1 0.621 ms 0.569 ms 0.564 ms
2 192.168.1.9 1.071 ms 1.112 ms 1.108 ms
担心是添加路由的顺序导致,小路由在前,路由表优先匹配,删除大小路由重新测试
# 删除大小路由
nmcli connection modify vpn1 ipv4.routes ''
nmcli connection up vpn1
nmcli connection modify vpn2 ipv4.routes ''
nmcli connection up vpn2
# 这次先添加大路由,后添加小路由
nmcli connection modify vpn2 ipv4.routes '192.168.0.0/16 172.16.1.1 200'
nmcli connection up vpn2
nmcli connection modify vpn1 ipv4.routes '192.168.1.0/24 172.16.0.1 200'
nmcli connection up vpn1
# 最终路由表依然是大路由出现在了前面,目前metric相同,开始测试
[root@oracle-linux7-9-5 ~]# traceroute -n 192.168.1.9
traceroute to 192.168.1.9 (192.168.1.9), 30 hops max, 60 byte packets
1 172.16.0.1 0.810 ms 0.764 ms 0.760 ms
2 192.168.1.9 1.223 ms 1.218 ms 1.214 ms
`最终还是走小路由`
降低小路由metric值,也就是提高他的优先级,看看效果
[root@oracle-linux7-9-5 ~]# nmcli connection show vpn1 | egrep 'metric|routes|ens36'
ipv4.routes: { ip = 192.168.0.0/16, nh = 172.16.1.1, mt = 200 }
ipv4.route-metric: -1
ipv4.ignore-auto-routes: no
ipv6.routes: --
ipv6.route-metric: -1
ipv6.ignore-auto-routes: no
# 修改metric值
[root@oracle-linux7-9-5 ~]# nmcli connection modify vpn1 ipv4.routes '192.168.0.0/24 172.16.0.1 1'
[root@oracle-linux7-9-5 ~]# nmcli connection up vpn1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/27)
[root@oracle-linux7-9-5 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 ens33
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
172.16.0.0 0.0.0.0 255.255.255.0 U 111 0 0 ens35
172.16.1.0 0.0.0.0 255.255.255.0 U 110 0 0 ens36
192.168.0.0 172.16.0.1 255.255.255.0 UG 1 0 0 ens35
192.168.0.0 172.16.1.1 255.255.0.0 UG 200 0 0 ens36
# 发现还是大的路由生效
[root@oracle-linux7-9-5 ~]# traceroute -n 192.168.1.9
traceroute to 192.168.1.9 (192.168.1.9), 30 hops max, 60 byte packets
1 172.16.1.1 0.613 ms 0.563 ms 0.534 ms
2 192.168.1.9 0.999 ms 0.955 ms 0.922 ms
小路由优先级更高
删除大路由测试小路由是否生效
[root@oracle-linux7-9-5 ~]# nmcli connection modify vpn2 ipv4.routes ''
[root@oracle-linux7-9-5 ~]# nmcli connection up vpn2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/28)
[root@oracle-linux7-9-5 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 ens33
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
172.16.0.0 0.0.0.0 255.255.255.0 U 119 0 0 ens35
172.16.1.0 0.0.0.0 255.255.255.0 U 120 0 0 ens36
192.168.1.0 172.16.0.1 255.255.255.0 UG 200 0 0 ens35
[root@oracle-linux7-9-5 ~]# traceroute -n 192.168.1.9
traceroute to 192.168.1.9 (192.168.1.9), 30 hops max, 60 byte packets
1 172.16.0.1 0.671 ms 0.620 ms 0.586 ms
2 192.168.1.9 0.792 ms 0.759 ms 0.722 ms
删除小路由测试大路由是否生效
[root@oracle-linux7-9-5 ~]# nmcli connection modify vpn1 ipv4.routes ''
[root@oracle-linux7-9-5 ~]# nmcli connection up vpn1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/41)
[root@oracle-linux7-9-5 ~]# nmcli connection modify vpn2 ipv4.routes ''
[root@oracle-linux7-9-5 ~]# nmcli connection up vpn2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/42)
[root@oracle-linux7-9-5 ~]# nmcli connection modify vpn2 ipv4.routes '192.168.0.0/16 172.16.1.1 200'
[root@oracle-linux7-9-5 ~]# nmcli connection up vpn2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/43)
[root@oracle-linux7-9-5 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 ens33
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
172.16.0.0 0.0.0.0 255.255.255.0 U 121 0 0 ens35
172.16.1.0 0.0.0.0 255.255.255.0 U 122 0 0 ens36
192.168.0.0 172.16.1.1 255.255.0.0 UG 200 0 0 ens36
[root@oracle-linux7-9-5 ~]# traceroute -n 192.168.1.9
traceroute to 192.168.1.9 (192.168.1.9), 30 hops max, 60 byte packets
1 172.16.1.1 0.611 ms 0.559 ms 0.605 ms
2 192.168.1.9 1.247 ms 1.242 ms 1.239 ms
提高大路由的metric,看看效果
[root@oracle-linux7-9-5 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 ens33
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
172.16.0.0 0.0.0.0 255.255.255.0 U 125 0 0 ens35
172.16.1.0 0.0.0.0 255.255.255.0 U 124 0 0 ens36
192.168.0.0 172.16.1.1 255.255.0.0 UG 200 0 0 ens36
192.168.1.0 172.16.0.1 255.255.255.0 UG 200 0 0 ens35
# 提高大路由优先级
nmcli connection modify vpn2 ipv4.routes '192.168.0.0/16 172.16.1.1 1'
nmcli connection up vpn2
[root@oracle-linux7-9-5 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 ens33
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
172.16.0.0 0.0.0.0 255.255.255.0 U 125 0 0 ens35
172.16.1.0 0.0.0.0 255.255.255.0 U 126 0 0 ens36
192.168.0.0 172.16.1.1 255.255.0.0 UG 1 0 0 ens36
192.168.1.0 172.16.0.1 255.255.255.0 UG 200 0 0 ens35
# 测试还是小路由生效
[root@oracle-linux7-9-5 ~]# traceroute -n 192.168.1.9
traceroute to 192.168.1.9 (192.168.1.9), 30 hops max, 60 byte packets
1 172.16.0.1 0.808 ms 0.749 ms 0.718 ms
2 192.168.1.9 1.552 ms 1.547 ms 1.543 ms