centos 多网卡配置优先级

过程

  1. 查看网卡配置

    [[email protected] ~]# ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host
           valid_lft forever preferred_lft forever
    2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 34:17:eb:f0:18:8f brd ff:ff:ff:ff:ff:ff
        inet 192.168.0.84/24 brd 192.168.0.255 scope global noprefixroute em1
           valid_lft forever preferred_lft forever
        inet6 240e:390:c6a:c3a0:3617:ebff:fef0:188f/64 scope global noprefixroute dynamic
           valid_lft 259182sec preferred_lft 172782sec
        inet6 fe80::3617:ebff:fef0:188f/64 scope link noprefixroute
           valid_lft forever preferred_lft forever
    3: em2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 34:17:eb:f0:18:90 brd ff:ff:ff:ff:ff:ff
        inet 192.169.0.10/23 brd 192.169.1.255 scope global noprefixroute em2
           valid_lft forever preferred_lft forever
        inet6 fe80::3617:ebff:fef0:1890/64 scope link noprefixroute
           valid_lft forever preferred_lft forever
    4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
        link/ether 02:42:b1:66:84:26 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
           valid_lft forever preferred_lft forever
        inet6 fe80::42:b1ff:fe66:8426/64 scope link
           valid_lft forever preferred_lft forever
    12: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
        link/ether ca:d7:f0:2f:42:f0 brd ff:ff:ff:ff:ff:ff link-netnsid 0
        inet6 fe80::c8d7:f0ff:fe2f:42f0/64 scope link
           valid_lft forever preferred_lft forever
    14: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
        link/ether 76:ab:49:a9:e4:a6 brd ff:ff:ff:ff:ff:ff link-netnsid 1
        inet6 fe80::74ab:49ff:fea9:e4a6/64 scope link
           valid_lft forever preferred_lft forever
    

    这里有 em1em2 两张网卡

  2. 查看网卡路由

    [[email protected] ~]# ip route show
    default via 192.168.0.1 dev em1 proto static metric 100
    default via 192.169.0.1 dev em2 proto static metric 101
    172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
    192.168.0.0/24 dev em1 proto kernel scope link src 192.168.0.84 metric 100
    192.169.0.0/23 dev em2 proto kernel scope link src 192.169.0.10 metric 101
    

    可以看到,em1metric 大于 em2metric 值越低,优先级越高。

    Metric 为路由指定所需跃点数的整数值(范围是 1 ~ 9999),它用来在路由表里的多个路由中选择与转发包中的目标地址最为匹配的路由。所选的路由具有最少的跃点数。跃点数能够反映跃点的数量、路径的速度、路径可靠性、路径吞吐量以及管理属性。Metric 的值越小,优先级越高,如果两块网卡的Metric的值相同,就会出现抢占优先级继而网卡冲突,将会有一块网卡无法连接。

  3. 更改优先级

    现在要设置 em2 的优先级比 em1 的高,只需要更改 em2 网卡配置的 metric 值比 em1 的低就可以了

    修改 em2 网卡配置:

    配置文件地址:vim /etc/sysconfig/network-scripts/ifcfg-em2

    添加配置: IPV4_ROUTE_METRIC=90

    TYPE=Ethernet
    BOOTPROTO=static
    DEFROUTE=yes
    PEERDNS=yes
    PEERROUTES=yes
    IPV4_FAILURE_FATAL=no
    IPV4_ROUTE_METRIC=90
    IPV6INIT=yes
    IPV6_AUTOCONF=yes
    IPV6_DEFROUTE=yes
    IPV6_PEERDNS=yes
    IPV6_PEERROUTES=yes
    IPV6_FAILURE_FATAL=no
    NAME=em2
    UUID=23420496-e9ed-466c-a8fc-e99b091f3a00
    DEVICE=em2
    ONBOOT=yes
    IPADDR=192.169.0.10
    NETMASK=255.255.254.0
    GATEWAY=192.169.0.1
    DNS1=114.114.114.114
    
  4. 重启网卡

    # 刷新配置文件
    source /etc/sysconfig/network-scripts/ifcfg-em2
    
    # 关闭 em2 网卡
    ifdown em2
    
    # 开启 em2 网卡
    ifup em2