Lnux系统网卡绑定配置详解(2)

2、nmcli命令的使用

(1)作用:地址配置工具

(2)用法:nmcli[ OPTIONS ] OBJECT{ COMMAND | help }(具体用法可以在使用时按Tab键补全)

选项:device

connection

[root@linuxidc network-scripts]#nmcli device show eth1  #查看网卡eth1的详细信息

GENERAL.DEVICE:                        eth1

GENERAL.TYPE:                          ethernet

GENERAL.HWADDR:                        00:0C:29:ED:0F:1B

GENERAL.MTU:                            1500

GENERAL.STATE:                          100 (connected)

GENERAL.CONNECTION:                    Wired connection 1

GENERAL.CON-PATH:                      /org/freedesktop/NetworkManager/ActiveConnection/6

WIRED-PROPERTIES.CARRIER:              on

IP4.ADDRESS[1]:                        10.0.0.3/24

IP4.GATEWAY:                            10.0.0.2

IP4.DNS[1]:                            10.0.0.2

IP4.DOMAIN[1]:                          localdomain

(3)通过nmcli修改网卡配置文件名称

[root@linuxidc network-scripts]#nmcli connection show #查看当前系统有多少套配置文件

NAME          UUID                          TYPE            DEVICE

virbr0      084f00d4-652e-44c8-83b7-70b469f89f31 bridge          virbr0

eth0        d50e8c71-6416-4ab6-97ca-ad2002cd3353  802-3-ethernet  eth0 

Wired connection 1  370c3d11-868f-4efc-95bc-630169b62ef1  802-3-ethernet  eth1

[root@linuxidc network-scripts]#nmcli connection modify Wired\ connection\ 1 connection.id eth1                            #修改网卡配置文件名称

[root@linuxidc network-scripts]#nmcli connection show  #查看修改后的名称

NAME      UUID                                  TYPE            DEVICE

virbr0    084f00d4-652e-44c8-83b7-70b469f89f31  bridge          virbr0

eth0      d50e8c71-6416-4ab6-97ca-ad2002cd3353 802-3-ethernet  eth0 

eth1      370c3d11-868f-4efc-95bc-630169b62ef1 802-3-ethernet  eth1 

(4)通过nmcli命令为网卡在不同的环境创建不同的配置文件

1)创建网卡配置文件

[root@linuxidc network-scripts]#nmcli connection add ifname eth1 type ethernet con-name eth1-home ip4192.168.1.2/24 gw4 192.168.1.1    #通过命令创建一份网卡配置文件

Connection 'eth1-home'(4e73fce1-800e-420d-902f-48b19c8dfc07) successfully added.

[root@linuxidc network-scripts]#nmcli connection show

……

eth1-home  4e73fce1-800e-420d-902f-48b19c8dfc07  802-3-ethernet  --   

[root@linuxidc network-scripts]#cp ifcfg-eth1-home ifcfg-eth1-office  #通过复制创建网卡配置

[root@linuxidc network-scripts]#nmcli connection reload    #加载复制的网卡配置

[root@linuxidc network-scripts]#nmcli connection show      #查看生效后的配置

……

eth1-office  1bbd1be4-e93e-4b29-0ced-eaffbef36326  802-3-ethernet  --   

eth1-home    4e73fce1-800e-420d-902f-48b19c8dfc07  802-3-ethernet  --   

2)使创建的网卡配置文件生效

[root@linuxidc network-scripts]#nmcli connection up eth1-home #使配置eth1-home生效

Connection successfully activated(D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/9)

[root@linuxidc network-scripts]#nmcli connection show  #查看当前生效的配置

NAME        UUID                                  TYPE            DEVICE

…….

eth1-home    4e73fce1-800e-420d-902f-48b19c8dfc07  802-3-ethernet  eth1 

[root@linuxidc network-scripts]#ifconfig eth1  #查看eth1的IP地址

eth1:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

inet 192.168.1.2  netmask 255.255.255.0  broadcast 192.168.1.255

[root@linuxidc network-scripts]#nmcli device status  #查看设备的状态

DEVICE      TYPE    STATE      CONNECTION

virbr0      bridge  connected  virbr0   

eth0        ethernet  connected eth0     

eth1        ethernet  connected eth1-home 

lo          loopback  unmanaged --       

virbr0-nic  tap      unmanaged  --

3)删除网卡配置文件

[root@linuxidc network-scripts]#nmcli connection delete eth1-home

Connection 'eth1-home'(4e73fce1-800e-420d-902f-48b19c8dfc07) successfully deleted.

[root@linuxidc network-scripts]#nmcli connection show  #查看删除后可用的配置文件

(4)通过nmcli网络设置

1)用法:nmcliconnection modify IFACE[+|-]setting.propertyvalue

2)常用参数

Nmcli  修改参数

 

对应网卡配置文件选项

 

ipv4.method  manual

 

BOOTPROTO=none

 

ipv4.method  auto

 

BOOTPROTO=dhcp

 

ipv4.addresses  “192.0.2.1/24 192.0.2.254”

 

IPADDR0=192.0.2.1

PREFIX0=24

GATEWAY0=192.0.2.254

 

ipv4.dns  8.8.8.8

 

DNS0=8.8.8.8

 

ipv4.dns-search  example.com

 

DOMAIN=example.com

 

ipv4.ignore-auto-dns  true

 

PEERDNS=no

 

connection.autoconnectyes

 

ONBOOT=yes

 

connection.id  eth0

 

NAME=eth0

 

connection.interface-name  eth0

 

DEVICE=eth0

 

802-3-ethernet.mac-address  . . .

 

HWADDR=  . . .

 

#将网卡配置文件中的ONBOOT设置为no

[root@linuxidc network-scripts]# nmcliconnection modify eth1-home connection.autoconnect no

#设置dns

[root@linuxidc network-scripts]#nmcli connection modify eth1-home ipv4.dns 114.114.114.114

#为网卡再添加一个IP地址

[root@linuxidc network-scripts]#nmcli connection modify eth1-home +ipv4.addresses 192.168.1.100/24

[root@linuxidc network-scripts]#cat ifcfg-eth1-home | grep -E "IPADDR|DNS"

DNS1=114.114.114.114

IPADDR=192.168.1.2

IPADDR1=192.168.1.100

[root@linuxidc network-scripts]#nmcli connection modify eth1-home -ipv4.addresses 192.168.1.100/24    #删除设置的一个IP地址

3、通过nmcli命令实现bonding

(1)添加bonding接口

[root@linuxidc network-scripts]#nmcli connection add type bond con-name bond0 ifname bond0 mode active-backup  #创建bonding接口,模式为主从模式

Connection 'bond0'(da41db3d-b705-4eca-8e0a-06e7c59a95eb) successfully added.

[root@linuxidc network-scripts]#nmcli connection show

……

bond0  da41db3d-b705-4eca-8e0a-06e7c59a95eb  bond            bond0 

(2)为bonding接口添加从属接口

[root@linuxidc network-scripts]#nmcli connection add type bond-slave ifname eth0 master bond0 #将eth0接口添加到bond0的从属接口,添加后会生成新的配置文件bond-slave-eth0

Connection 'bond-slave-eth0'(681e4abb-f538-4756-b0bd-cd5a51eb0cf6) successfully added.

[root@linuxidc network-scripts]#nmcli connection add type bond-slave ifname eth1 master bond0 #将eth1接口添加到bond0的从属接口,添加后会生成新的配置文件bond-slave-eth1

Connection 'bond-slave-eth1'(0a1708cc-e9fc-48d6-b6c2-6349b14d06e0) successfully added.

(3)启动绑定

[root@linuxidc network-scripts]#nmcli connection up bond-slave-eth0 #启动绑定从属接口0

Connection successfully activated(D-Bus active path: /org/freedesktop/NetworkManager/Active

Connection/13)

[root@linuxidc network-scripts]#nmcli connection up bond-slave-eth1 #启动绑定从属接口1

Connection successfully activated(D-Bus active path: /org/freedesktop/NetworkManager/Active

Connection/14)

[root@linuxidc network-scripts]#nmcli connection up bond0  #启动绑定

Connection successfully activated(master waiting for slaves) (D-Bus active path: /org/freedesktop/

NetworkManager/ActiveConnection/15)

(4)查看bonding状态及信息

[root@linuxidc network-scripts]#cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver:v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance(active-backup)

Primary Slave: None

Currently Active Slave: eth0

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0

……

(5)修改bongding的工作模式

[root@linuxidc network-scripts]#cat ifcfg-bond0

…….

BONDING_OPTS=mode=broadcast    #将主从模式修改广播模式

……

[root@linuxidc network-scripts]#nmcli connection up bond0  #使修改后的配置生效

Connection successfully activated(master waiting for slaves) (D-Bus active path:/org/freedesktop/NetworkManager/ActiveConnection/18)

[root@linuxidc network-scripts]#cat /proc/net/bonding/bond0  #查看状态

Ethernet Channel Bonding Driver:v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance(broadcast)

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0

(6)删除bonding

[root@linuxidc network-scripts]#nmcli connection delete bond0  #删除bonding配置文件

[root@linuxidc ~]# nmcliconnection delete bond-slave-eth0  #删除bonding从属接口配置文件

[root@linuxidc ~]# nmcliconnection delete bond-slave-eth1

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/13930.html