RHEL7: nmcli网络连接管理

team;

看网卡设备

[root@rhel7-0 network-scripts]# nmcli dev status
DEVICE  TYPE      STATE         CONNECTION 
ens3    ethernet  connected     ens3       
ens6    ethernet  disconnected  --         
ens7    ethernet  disconnected  --         
lo      loopback  unmanaged     --   
[root@rhel7-0 ~]# nmcli device show ens3
GENERAL.DEVICE:                         ens3
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         52:54:00:28:5D:1A
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     ens3
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         ip = 192.168.122.179/24, gw = 192.168.122.1
IP4.DNS[1]:                             192.168.122.1
IP6.ADDRESS[1]:                         ip = fe80::5054:ff:fe28:5d1a/64, gw = ::

看网络连接的详细信息

[root@rhel7-0 ~]# nmcli con show ens3
connection.id:                          ens3
connection.uuid:                        c078b66a-de02-4165-8026-a7b6f8fe8048
connection.interface-name:              ens3
connection.type:                        802-3-ethernet
connection.autoconnect:                 yes

创建网络连接

[root@rhel7-0 ~]# nmcli connection add con-name ens6 type ethernet ifname ens6 
Connection 'ens6' (3cf83be9-7038-4061-87f9-2f58cf065240) successfully added.
[root@rhel7-0 ~]# 
[root@rhel7-0 ~]# nmcli connection add con-name ens7 type ethernet ifname ens7 
Connection 'ens7' (3e3776e3-fe5f-4f77-8f61-c47428eaf4d6) successfully added.


自动连接

[root@rhel7-0 ~]# nmcli connection modify ens6 connection.autoconnect no
[root@rhel7-0 ~]# nmcli connection modify ens6 connection.autoconnect yes

断开/启用连接

[root@rhel7-0 ~]# nmcli connection down ens6 

[root@rhel7-0 ~]# nmcli connection show 
NAME  UUID                                  TYPE            DEVICE 
ens7  3e3776e3-fe5f-4f77-8f61-c47428eaf4d6  802-3-ethernet  ens7   
ens6  3cf83be9-7038-4061-87f9-2f58cf065240  802-3-ethernet  --     
ens3  c078b66a-de02-4165-8026-a7b6f8fe8048  802-3-ethernet  ens3   

[root@rhel7-0 ~]# nmcli device status 
DEVICE  TYPE      STATE         CONNECTION 
ens3    ethernet  connected     ens3       
ens7    ethernet  connected     ens7       
ens6    ethernet  disconnected  --         
lo      loopback  unmanaged     --   

[root@rhel7-0 ~]# nmcli connection up ens6 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)

指定DNS Server

[root@rhel7-0 ~]# nmcli connection modify ens6 ipv4.dns 8.8.8.8
[root@rhel7-0 ~]# nmcli connection show ens6 | grep -i dns
ipv4.dns:                               8.8.8.8
ipv4.dns-search:                        
ipv4.ignore-auto-dns:                   no
ipv6.dns:                               
ipv6.dns-search:                        
ipv6.ignore-auto-dns:                   no
IP4.DNS[1]:                             192.168.122.1


[root@rhel7-0 ~]# nmcli connection modify ens6 ipv4.dns 8.8.4.4
[root@rhel7-0 ~]# nmcli connection show ens6 | grep -i dns
ipv4.dns:                               8.8.4.4
ipv4.dns-search:                        
ipv4.ignore-auto-dns:                   no
ipv6.dns:                               
ipv6.dns-search:                        
ipv6.ignore-auto-dns:                   no
IP4.DNS[1]:                             192.168.122.1

[root@rhel7-0 ~]# nmcli connection modify ens6 +ipv4.dns 8.8.8.8
[root@rhel7-0 ~]# nmcli connection show ens6 | grep -i dns
ipv4.dns:                               8.8.4.4, 8.8.8.8
ipv4.dns-search:                        
ipv4.ignore-auto-dns:                   no
ipv6.dns:                               
ipv6.dns-search:                        
ipv6.ignore-auto-dns:                   no
IP4.DNS[1]:                             192.168.122.1


修改IP地址

nmcli connection modify ens6 ipv4.addresses IPAddr/prefix Gateway

[root@rhel7-0 ~]# nmcli connection modify ens6 ipv4.addresses  192.168.122.233/24 192.168.122.1
[root@rhel7-0 ~]# nmcli connection modify ens6 ipv4.method static
[root@rhel7-0 ~]# nmcli connection down ens6
[root@rhel7-0 ~]# nmcli connection up ens6
[root@rhel7-0 ~]# nmcli connection show ens6 | grep -i address
802-3-ethernet.mac-address:             --
802-3-ethernet.cloned-mac-address:      --
802-3-ethernet.mac-address-blacklist:   
ipv4.addresses:                         { ip = 192.168.122.233/24, gw = 192.168.122.1 }
ipv6.addresses:                         
IP4.ADDRESS[1]:                         ip = 192.168.122.233/24, gw = 192.168.122.1
IP6.ADDRESS[1]:                         ip = fe80::5054:ff:fe7e:2642/64, gw = ::

nmcli命令总结

命令 作用
nmcli dev status 列出设备(状态)
nmcli con show 显示当前连接信息
nmcli con up "连接名" 启动一个连接
nmcli con down "连接名" 关闭一个连接;如果这个连接设置了autoconnect,那么关闭后这个连接会重启
nmcli dev dis 设备 关闭这个网卡设备,就算有autoconnect也不会重启
nmcli net off 关闭所有受NetworkManager管理的网卡
nmcli con add con-name ens6 type ethernet ifname ens6 增加一个连接
nmcli con mod "连接名" 修改一个连接
nmcli con del "连接名" 删除一个连接

更多信息请参考nmcli, nmcli-examples, nm-settings man pages.


TEAM

[root@rhel7-0 ~]# nmcli connection add type team con-name team0 ifname team0 config '{"runner":{"name":"roundrobin"}}'
Connection 'team0' (ea6ff298-8c4a-4b16-82bc-ba0aeb558a41) successfully added.

[root@rhel7-0 ~]# nmcli connection add type team-slave con-name team-slave0 ifname ens6 master team0
Connection 'team-slave0' (fc87d972-7b3b-491a-8f5d-6bcb66c7d85f) successfully added.
[root@rhel7-0 ~]# nmcli connection add type team-slave con-name team-slave1 ifname ens7 master team0
Connection 'team-slave1' (d9dc2e93-8239-48c7-8d84-0db28fb6c67c) successfully added.

测试team

[root@rhel7-0 ~]# nmcli con up team0

查看team状态

[root@rhel7-0 ~]# teamdctl team0 state
setup:
  runner: roundrobin
ports:
  ens6
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
  ens7
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up

尝试禁掉一个设备

[root@rhel7-0 ~]# nmcli dev dis ens6
[root@rhel7-0 ~]# teamdctl team0 state
setup:
  runner: roundrobin
ports:
  ens7
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up


[root@rhel7-0 ~]# nmcli dev connect ens6
[root@rhel7-0 ~]# teamnl team0 ports
 3: ens6: up 100Mbit FD 
 4: ens7: up 100Mbit FD