metric
配置網(wǎng)卡的默認metric
給網(wǎng)卡配置默認的metric值,當添加路由的時候如果沒有手動指定 metric 值累铅,則使用默認值窗轩,默認值為 -1 检痰,表示自動配置路由的metric值愤惰。metric
數(shù)值越大苇经,路由的優(yōu)先級越低。
沒有 NetworkManager 的情況下設置
修改位于/etc/sysconfig/network-scripts/
目錄下的ifcfg-<interface>
文件宦言,<interface>
是接口的名稱扇单,添加以下內(nèi)容
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
參數(shù)可以更改所有該連接對應網(wǎng)卡的所有路由的 metric 值,但是响疚。鄙信。。對應手動指定了metric 值的路由忿晕,是不會修改的装诡。也就是配置網(wǎng)卡的默認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
# 可以看到網(wǎng)絡連接vpn2践盼,對應的路由條目沒有指定metric值鸦采,所以該網(wǎng)卡對應的所有路由條目的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
修改路由的優(yōu)先級
沒有 NetworkManager 的情況下設置
創(chuàng)建或修改位于/etc/sysconfig/network-scripts/
目錄下route-<interface>
文件,<interface>
是網(wǎng)卡接口的名稱
文件的格式如下
<network/prefix> via <gateway> <metric "value">
<network/prefix> 指定目標IP或者網(wǎng)段
<gateway> 指定下一跳的地址
<metric "value"> 指定路由的優(yōu)先級咕幻,metric值越高渔伯,路由的優(yōu)先級越低
范例:
# 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
-
修改現(xiàn)有連接的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
大小路由優(yōu)先級
在路由表中既有大路由,又包含小的路由肄程,例如下面這種
10.0.0.0/16 via 192.51.100.10 metric 200 # 大路由
10.0.0.0/24 via 192.51.10.20 metric 50 # 小路由
實驗環(huán)境
小路由VPN1锣吼,網(wǎng)卡ens35,對應網(wǎng)關172.16.0.1
大路由VPN2蓝厌,網(wǎng)卡ens36玄叠,對應網(wǎng)關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
# 最終路由,大路由的網(wǎng)關是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
# 發(fā)現(xiàn)是小的路由優(yōu)先生效
[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
擔心是添加路由的順序?qū)е轮畹洌÷酚稍谇埃酚杀韮?yōu)先匹配崎苗,刪除大小路由重新測試
# 刪除大小路由
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
# 最終路由表依然是大路由出現(xiàn)在了前面,目前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值肌蜻,也就是提高他的優(yōu)先級,看看效果
[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
# 發(fā)現(xiàn)還是大的路由生效
[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
小路由優(yōu)先級更高
刪除大路由測試小路由是否生效
[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
# 提高大路由優(yōu)先級
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