开启左侧

飞牛OS双网卡指定wifi上网优先

发表于:2025-2-27 22:33:17 系统攻略 495

<br /> <br />

对于有双网卡使用速度较快的网卡上网的方法,例如如一个有线网卡100M,一个无线网卡1000M,想指定wifi上网优先。

思路就是更改路由的metric值,因为metric越小越优先,使用定时任务查询并修改网卡的metric值。

提示:该方法对于docker上网,只能对使用bridge或者host网卡的容器有效,使用macvlan网卡如果parent绑定的是有线网卡该方法无法更改为无线网卡上网。

先ssh进入root权限,这里默认有线网卡和无线网卡都已联网,且为同一网段。

然后route -n查看路由表,其中enp2s0-ovs是有线网卡,wlp3s0是无线网卡。

root@fn02:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.31.1    0.0.0.0         UG    0      0        0 enp2s0-ovs
0.0.0.0         192.168.31.1    0.0.0.0         UG    0      0        0 wlp3s0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-990d3794a7fe
192.168.31.0    0.0.0.0         255.255.255.0   U     0      0        0 enp2s0-ovs
192.168.31.0    0.0.0.0         255.255.255.0   U     0      0        0 wlp3s0

如果无线网卡的metric值不为0,则进入目录/etc/NetworkManager/system-connections

cd /etc/NetworkManager/system-connections

ll命令查询,其中有线网卡连接的配置文件 enp2s0-ovs.nmconnection ,因为ipv4的metric值不知道为什么会被定时覆盖,所以这里并没有编辑修改它ipv4的metric值。

root@fn02:/etc/NetworkManager/system-connections# ll
total 20
drwxr-xr-x 2 root root 4096 Feb 26 21:24  .
drwxr-xr-x 7 root root 4096 Feb 25 19:56  ..
-rw------- 1 root root  242 Feb 26 21:24  enp2s0-ovs.nmconnection
-rw------- 1 root root  340 Feb 25 20:00  hellowordwifi_5G.nmconnection
-rw------- 1 root root  282 Feb 25 20:00 'Wired connection 2.nmconnection'

选择编辑wifi的配置文件,我的配置文件名为hellowordwifi_5G.nmconnection

vi hellowordwifi_5G.nmconnection

找到ipv4,增加或者修改route-metric=0

[ipv4]
method=auto
route-metric=0

这样wifi的路由metric就被改为0了,可以重启NetManager看下:

systemctl restart NetworkManager
route -n

然后就是定时任务修改有线网卡enp2s0-ovs的metric值,我这里修改的是enp2s0-ovs的两条路由,见下图划线的地方,因为我的子网是192.168.31.0/24,网关是192.168.31.1:

捕获.PNG

编写脚本:

vi /usr/local/bin/wifi_first.sh

代码如下,大家复制的时候记得修改一下参数,网卡名:enp2s0-ovs、子网:192.168.31.0/24,网关:192.168.31.1

#!/bin/bash

# 1.网卡enp2s0-ovs修改gw 192.168.31.1的路由metric
# 获取所有匹配的 metric 值并统计行数
current_metrics=$(sudo route -n | grep "192.168.31.1" | grep "enp2s0-ovs" | awk '{print $5}')
line_count=$(echo "$current_metrics" | tr ' ' '\n'| wc -l)

if [ "$line_count" -eq 0 ]; then
    sudo route add default gw 192.168.31.1 dev enp2s0-ovs metric 204
elif [ "$line_count" -eq 1 ]; then
    # 只有一行时,直接执行条件判断
    echo "$current_metrics" | while read current_metric; do
        if [ "$current_metric" -eq "0" ]; then
            echo "发现 metric 为 0,执行路由更新操作"
            sudo route del default gw 192.168.31.1 dev enp2s0-ovs
            sudo route add default gw 192.168.31.1 dev enp2s0-ovs metric 204
        else
            echo "当前 metric 为 $current_metric,无需更新"
        fi
    done
else
    # 行数大于 1 时,执行 2 次删除命令
    sudo route del default gw 192.168.31.1 dev enp2s0-ovs
    sudo route del default gw 192.168.31.1 dev enp2s0-ovs
    sudo route add default gw 192.168.31.1 dev enp2s0-ovs metric 204
fi
# 2.网卡enp2s0-ovs 修改net 192.168.31.0 netmask 255.255.255.0的路由metric
# 获取所有匹配的 metric 值并统计行数
current_metrics=$(sudo route -n | grep "192.168.31.0" | grep "enp2s0-ovs" | awk '{print $5}')
line_count=$(echo "$current_metrics" | tr ' ' '\n'| wc -l)

if [ "$line_count" -eq 0 ]; then
    sudo route add -net 192.168.31.0 netmask 255.255.255.0 metric 205 dev enp2s0-ovs
elif [ "$line_count" -eq 1 ]; then
    # 只有一行时,直接执行条件判断
    echo "$current_metrics" | while read current_metric; do
        if [ "$current_metric" -eq "0" ]; then
            echo "发现 metric 为 0,执行路由更新操作"
            sudo route del -net 192.168.31.0 netmask 255.255.255.0 metric 0 dev enp2s0-ovs
            sudo route add -net 192.168.31.0 netmask 255.255.255.0 metric 205 dev enp2s0-ovs
        else
            echo "当前 metric 为 $current_metric,无需更新"
        fi
    done
else
    # 行数大于 1 时,执行2次删除命令
    sudo route del -net 192.168.31.0 netmask 255.255.255.0 metric 0 dev enp2s0-ovs
    sudo route del -net 192.168.31.0 netmask 255.255.255.0 metric 0 dev enp2s0-ovs
    sudo route add -net 192.168.31.0 netmask 255.255.255.0 metric 205 dev enp2s0-ovs
fi

增加执行权限,执行并查看route

chmod +x /usr/local/bin/wifi_first.sh
sh /usr/local/bin/wifi_first.sh
route -n

命令执行后路由,204和205为修改之后的metric值:

root@fn02:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.31.1    0.0.0.0         UG    0      0        0 wlp3s0
0.0.0.0         192.168.31.1    0.0.0.0         UG    204    0        0 enp2s0-ovs
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-990d3794a7fe
192.168.31.0    0.0.0.0         255.255.255.0   U     0      0        0 wlp3s0
192.168.31.0    0.0.0.0         255.255.255.0   U     205    0        0 enp2s0-ovs

然后就是加到系统定时任务:

vi /etc/cron.d/ch_metric

写入如下定时任务,每分钟执行一次:

*/1 * * * * root /bin/bash  /usr/local/bin/wifi_first.sh 

完毕。

脚本wifi_first.sh代码分两段,第一段是0.0.0.0的路由(上外网),第二段是子网192.168.31.0的路由(内网),可以按需求删除或保留。

收藏
送赞 1
分享

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

发表回复

评论列表(2)

好方法,谢谢分享
2025-2-27 22:55:35 回复
期待官方在系统设置里面直接集成这个功能
2025-3-1 09:48:38 回复