Ubuntu 双网卡配置优先级

Ubuntu 双网卡配置优先级

Ubuntu的网卡配置跟CentOS不太一样。

更多请参考:Linux 双网卡配置优先级

根据业务需要,有时候服务器需要两张网卡,一张用于访问外网,另一种用于连接内网。

如果在安装系统是就对两张网卡进行配置了的话,就比较方便,这里不再讨论。

下面是后期添加的一张网卡ens37,其中ens33用于连接内网,ens37用于连接外网。

话不多说,直入正题。

启用网卡

查看所有网卡信息:

ifconfig -a

启用网卡,ens37是我用于连接外网的网卡。

sudo ifconfig ens37 up

此时网卡已启用。但是可能会由于网卡路由优先级的问题,还不能访问外网。

查看路由信息:ip route show

可以看到两张网卡的metric (跃点数),ens33的跃点数默认值是100,比ens37的值小,所以会通过ens33连接外网(但是业务需求是ens33连接的是内网而不是外网),因此不能访问外网。

修改网卡路由优先级

这里为了让ens37的路由优先级高于ens33,需要将ens37的跃点数改成小于ens33的跃点数(默认100)。我们这里将ens37的跃点数改成90

:Ubuntu使用netplan配置网卡

sudo vim /etc/netplan/50-cloud-init.yaml 
# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        ens33:
            dhcp4: true
        ens37:
            dhcp4: true
            dhcp4-overrides:
                route-metric: 90
    version: 2

这个配置文件看起来很清楚,ens37是要访问外网的网卡,跃点数设置成了90,而ens33默认跃点数100

然后执行命令:

sudo netplan apply

查看路由信息:ip route show

在这里插入图片描述

这时就可以通过ens37来访问外网了。

猜你喜欢

转载自blog.csdn.net/weixin_44129085/article/details/109121911