nmcli命令配置网卡链路聚合
nmcli命令,由软件 NetworkManager 提供的命令
说明:
用nmcli配置之前,不要对接口执行”ip addr”操作,不然会冲突,配置失败。
nmcli主要是解决并发操作ifconfig/ipaddr的问题,主要被gui tool调用。
服务器环境需要自启动接口,还是手动修改在/etc/sysconfig/network-script下的ifc-xxx文件
使用nmcli命令配置的网络配置可以立即生效且系统重启后配置也不会丢失。使用 nmcli 命令时,必须确保 NetworkManager 为运行状态。
介绍:nmcli是NetworkManager的一个命令行工具,它提供了使用命令行配置由NetworkManager管理网络连接的方法(nm 代表 NetworkManager,cli 代表 Command-Line)
一会在Linux系统中,网卡绑定(也称为链路聚合或bonding)是一种将多个网络接口组合成一个逻辑接口的技术。这可以提高带宽、提供冗余和负载均衡
如果虚拟机删除过网卡并重新增加导致网络不通,你需要在nmcli中创建一个新的网络连接,并想知道它的 UUID,可以这样做:
nmcli con add type [connection-type] con-name [connection-name] ifname [interface-name]
注:标红部分将[connection-type]替换为你想要的连接类型(如 ethernet, wifi, bridge),[connection-name] 替换为你想要的连接名称,[interface-name]替换为相应的网络接口名称创建连接后,使用以下命令查看其 UUID:
nmcli con show
显示由NetworkManager识别到的设备及其状态
nmcli device status
# 开启网络连接
nmcli n on
使用nmcli工具启动和停止网络接口,在root权限下执行如下命令(enp1s0为接口名称,需要根据实际情况进行更改)
nmcli connection up id enp1s0 nmcli device disconnect enp1s0
显示 NetworkManager 是否接管网络设置
nmcli networking
查看网络连接状态
nmcli n connectivity
开启网络连接
nmcli n on
关闭网络连接
nmcli n off
再测试网络是否正常:
示例
- 添加一个新的以太网连接配置:
nmcli c a type ethernet con-name eth0 ifname eth0 ipv4.addresses 192.168.8.200/23 ipv4.gateway 192.168.8.8 ipv4.dns "192.168.8.9 192.168.8.8" ipv4.method manual
修改连接常用示例
注意:必须先修改 ipv4.address,然后才能修改 ipv4.method!
nmcli c a type ethernet con-name eth0 ifname eth0 #创建新以太网连接配置 nmcli c m eth0 ipv4.address 192.168.8.200/23 #修改IP地址和子网掩码 nmcli c m eth0 +ipv4.addresses 192.168.8.199/23 #添加一个IP地址和子网掩码 nmcli c m eth0 ipv4.method manual #修改为静态配置,默认是auto nmcli c m eth0 ipv4.gateway 192.168.8.8 #修改默认网关 nmcli c m eth0 ipv4.dns 192.168.8.9 #修改DNS nmcli c m eth0 +ipv4.dns 192.168.8.8 #添加一个DNS nmcli c m eth0 ipv6.method ignored #将IPv6禁用,针对CentOS8 三个可选项:disabled、auto、manual nmcli c m eth0 connection.autoconnect yes #开机启动 nmcli c up eth0 #最后,激活连接以使配置生效
重载配置文件
建议使用 nmcli 命令来设置网络参数,不要直接修改此文件
重载所有连接的配置文件
nmcli c reload nmcli c r
重载某一指定连接的配置文件
nmcli c load ifcfg-enp1s0 nmcli c l ifcfg-enp1s0
创建Linux网卡绑定的步骤
虚拟机先直通两个网口
先使用nmcli命令修改网卡名可以通过以下步骤实现:
修改内核引导文件:首先需要修改GRUB配置文件,以便将网卡命名规则更改为传统的命名方式(如eth0、eth1等)
nano /etc/default/grub
在文件中找到GRUB_CMDLINE_LINUX参数,并在其后添加net.ifnames=0 biosdevname=0完成修改后,执行命令以重新生成GRUB配置并重启系统
grub2-mkconfig -o /boot/grub2/grub.cfg #更新grub
如果是uefi启动,则执行下面的语句更新grub
grub2-mkconfig -o /boot/efi/EFI/openEuler/grub.cfg #更新grub
reboot
查看下有几个网卡配置,全删除了
cd /etc/sysconfig/network-scripts/
ls
nmcli c delete eth0 bond0 enp6s18 enp1s0
1、创建一块bond网卡
nmcli connection add type bond con-name Aggregation ifname Aggregation mode 0 nmcli conn down Aggregation eth0 eth1
mode是网卡绑定模式,这里重点介绍一下
0:balance-rr:轮询模式round-robin
1:active-backup:主备模式
2:balance-xor:根据原目的MAC地址进行XOR Hash负载分担
3:broadcast :从所有成员接口发出,只有冗余机制,没有均衡
4:802.3ad:支持802.3ad协议,和交换机的LACP模式配合
5:balance-tlb:根据成员接口的负载进行发送,在当前接口接收
6:balance-alb:根据成员接口的负载进行发送和接收
其中0、1、4比较常用
2、向Aggregation中添加从属网卡
nmcli con add type bond-slave con-name rr-eth0 ifname eth0 master Aggregation nmcli con add type bond-slave con-name rr-eth1 ifname eth1 master Aggregation
3、设置Aggregation的网络参数
nmcli connection modify Aggregation ipv4.address 192.168.8.125/23 nmcli connection modify Aggregation ipv4.gateway 192.168.8.8 nmcli connection modify Aggregation ipv4.dns 192.168.8.9 nmcli connection modify Aggregation +ipv4.dns 192.168.8.8 nmcli connection modify Aggregation ipv4.method manual nmcli connection modify Aggregation ipv6.address 2000:192:168:8::125/64 nmcli connection modify Aggregation ipv6.gateway 2000:192:168:8::8 nmcli connection modify Aggregation ipv6.dns 2000:192:168:8::9 nmcli connection modify Aggregation +ipv6.dns 2000:192:168:8::8 nmcli connection modify Aggregation ipv6.method manual nmcli connection modify Aggregation connection.autoconnect yes
4、启动网络连接
nmcli connection up eth0 nmcli connection up eth1 nmcli connection up Aggregation
5、验证
cat /proc/net/bonding/Aggregation
ethtool Aggregation | grep -i speed
链路没有提升可能是虚拟接口原因
cat /etc/sysconfig/network-scripts/ifcfg-Aggregation
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?