ubuntu配置網(wǎng)卡的metric

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

使用nmclinmtui 奠旺,或者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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蒋搜,一起剝皮案震驚了整個濱河市篡撵,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌豆挽,老刑警劉巖育谬,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異帮哈,居然都是意外死亡膛檀,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進店門娘侍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來咖刃,“玉大人,你說我怎么就攤上這事憾筏『垦睿” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵氧腰,是天一觀的道長枫浙。 經(jīng)常有香客問我,道長容贝,這世上最難降的妖魔是什么自脯? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮斤富,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘锻狗。我一直安慰自己满力,他們只是感情好,可當我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布轻纪。 她就那樣靜靜地躺著油额,像睡著了一般。 火紅的嫁衣襯著肌膚如雪刻帚。 梳的紋絲不亂的頭發(fā)上潦嘶,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天,我揣著相機與錄音崇众,去河邊找鬼掂僵。 笑死,一個胖子當著我的面吹牛顷歌,可吹牛的內(nèi)容都是我干的锰蓬。 我是一名探鬼主播,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼眯漩,長吁一口氣:“原來是場噩夢啊……” “哼芹扭!你這毒婦竟也來了麻顶?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤舱卡,失蹤者是張志新(化名)和其女友劉穎辅肾,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體轮锥,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡矫钓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了交胚。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片份汗。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蝴簇,靈堂內(nèi)的尸體忽然破棺而出杯活,到底是詐尸還是另有隱情,我是刑警寧澤熬词,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布旁钧,位于F島的核電站,受9級特大地震影響互拾,放射性物質(zhì)發(fā)生泄漏歪今。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一颜矿、第九天 我趴在偏房一處隱蔽的房頂上張望寄猩。 院中可真熱鬧,春花似錦骑疆、人聲如沸田篇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽泊柬。三九已至,卻和暖如春诈火,著一層夾襖步出監(jiān)牢的瞬間兽赁,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工冷守, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留刀崖,地道東北人。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓教沾,卻偏偏與公主長得像蒲跨,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子授翻,可洞房花燭夜當晚...
    茶點故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容