Linux命令:nmcli

介绍

RHEL 和 CentOS 是默认使用 NetworkManager 来提供网络服务,这是一种动态管理网络配置的守护进程,能够让网络设备保持连接状态。可以使用 nmcli 命令来管理 NetworkManager 服务程序。,这是一款基于命令行的网络配置工具,功能丰富,参数众多。可以轻松地查看网络信息或网络状态。

命令帮助

[root@linuxprobe ~]# nmcli -h
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }

OPTIONS
  -o[verview]                                    overview mode (hide default values)
  -t[erse]                                       terse output
  -p[retty]                                      pretty output
  -m[ode] tabular|multiline                      output mode
  -c[olors] auto|yes|no                          whether to use colors in output
  -f[ields] <field1,field2,...>|all|common       specify fields to output
  -g[et-values] <field1,field2,...>|all|common   shortcut for -m tabular -t -f
  -e[scape] yes|no                               escape columns separators in values
  -a[sk]                                         ask for missing parameters
  -s[how-secrets]                                allow displaying passwords
  -w[ait] <seconds>                              set timeout waiting for finishing operations
  -v[ersion]                                     show program version
  -h[elp]                                        print this help

OBJECT
  g[eneral]       NetworkManager's general status and operations
  n[etworking]    overall networking control
  r[adio]         NetworkManager radio switches
  c[onnection]    NetworkManager's connections
  d[evice]        devices managed by NetworkManager
  a[gent]         NetworkManager secret agent or polkit agent
  m[onitor]       monitor NetworkManager changes

常用命令

查看网络信息或网络状态

[root@linuxprobe ~]# nmcli connection show
NAME    UUID                                  TYPE      DEVICE 
ens160  28b1cec8-f0a6-4da0-9f82-19348f23e305  ethernet  ens160 
virbr0  44159729-8b4f-4d7e-85d0-99cd37d047a6  bridge    virbr0 
[root@linuxprobe ~]# nmcli connection show ens160
connection.id:                          ens160
connection.uuid:                        28b1cec8-f0a6-4da0-9f82-19348f23e305
connection.stable-id:                   --
connection.type:                        802-3-ethernet
connection.interface-name:              ens160
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.autoconnect-retries:         -1 (default)
connection.multi-connect:               0 (default)
connection.auth-retries:                -1
connection.timestamp:                   1661029800
......

RHEL8 支持的网络配置切换

# nmcli connection add con-name type ifname 的格式
# 增加一个叫 company 的网络会话,在公司时候直接切换到 company 网络配置
[root@linuxprobe ~]# nmcli connection add con-name company type ethernet ifname ens160
Connection 'company' (ec661eb4-21f6-4f5b-bc43-dc44bf57f167) successfully added.
# 查看到增加成功
[root@linuxprobe ~]# nmcli c show
NAME     UUID                                  TYPE      DEVICE 
ens160   28b1cec8-f0a6-4da0-9f82-19348f23e305  ethernet  ens160 
virbr0   44159729-8b4f-4d7e-85d0-99cd37d047a6  bridge    virbr0 
company  ec661eb4-21f6-4f5b-bc43-dc44bf57f167  ethernet  -- 
# 启动网络会话
[root@linuxprobe ~]# nmcli connection up company
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)

[root@linuxprobe ~]# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.203.128  netmask 255.255.255.0  broadcast 192.168.203.255
        inet6 fe80::4f89:ded:1c89:e510  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:20:d5:c5  txqueuelen 1000  (Ethernet)
        RX packets 849  bytes 89179 (87.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1518  bytes 142147 (138.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@linuxprobe ~]# nmcli c show
NAME     UUID                                  TYPE      DEVICE 
company  ec661eb4-21f6-4f5b-bc43-dc44bf57f167  ethernet  ens160 
virbr0   44159729-8b4f-4d7e-85d0-99cd37d047a6  bridge    virbr0 
ens160   28b1cec8-f0a6-4da0-9f82-19348f23e305  ethernet  -- 

删除网络会话

[root@linuxprobe ~]# nmcli c delete company
Connection 'company' (ec661eb4-21f6-4f5b-bc43-dc44bf57f167) successfully deleted.

[root@linuxprobe ~]# nmcli c show
NAME    UUID                                  TYPE      DEVICE 
ens160  28b1cec8-f0a6-4da0-9f82-19348f23e305  ethernet  ens160 
virbr0  44159729-8b4f-4d7e-85d0-99cd37d047a6  bridge    virbr0

绑定两块网卡

创建 bound 网卡

# 创建类型为 bond,名称为 bond0,网卡名为 bond0,的绑定设备,绑定模式为 balance-rr
[root@linuxprobe ~]# nmcli c add type bond con-name bond0 ifname bond0 bond.options "mode=balance-rr"
Connection 'bond0' (7e409a8a-988c-42bf-98d5-81af3781fb9b) successfully added.
[root@linuxprobe ~]# nmcli c show
NAME    UUID                                  TYPE      DEVICE 
ens160  28b1cec8-f0a6-4da0-9f82-19348f23e305  ethernet  ens160 
ens192  1a8438af-c259-4562-b6bb-92bcad612b51  ethernet  ens192 
virbr0  f26f942f-5a83-4ff2-9a40-9e9c9e8d8c00  bridge    virbr0 
bond0   7e409a8a-988c-42bf-98d5-81af3781fb9b  bond      bond0 
  • rr 为 round-robin 的缩写,全称为“轮询模式”,会根据设备顺序依次传输数据包,负载均衡,带宽性能更好。一旦某网卡故障,马上切换到另一个网卡设备,保证可用性
  • active-backup 主备模式,平时只有一块网卡工作,另一块随时待命,一旦某块故障,另一块顶替上去

向 bond0 设备添加从属网卡

[root@linuxprobe ~]# nmcli c add type ethernet slave-type bond con-name bond-port1 ifname ens160 master bond0
Connection 'bond-port1' (f066ad2a-7e90-435b-94bc-9acd7b2c651c) successfully added.

[root@linuxprobe ~]# nmcli c add type ethernet slave-type bond con-name bond-port2 ifname ens192 master bond0
Connection 'bond-port2' (18c1c0bc-0844-4b50-8b1b-81c036c8fe60) successfully added.

[root@linuxprobe ~]# nmcli c show
NAME        UUID                                  TYPE      DEVICE 
ens160      28b1cec8-f0a6-4da0-9f82-19348f23e305  ethernet  ens160 
ens192      1a8438af-c259-4562-b6bb-92bcad612b51  ethernet  ens192 
virbr0      f26f942f-5a83-4ff2-9a40-9e9c9e8d8c00  bridge    virbr0 
bond0       7e409a8a-988c-42bf-98d5-81af3781fb9b  bond      bond0  
bond-port1  f066ad2a-7e90-435b-94bc-9acd7b2c651c  ethernet  --     
bond-port2  18c1c0bc-0844-4b50-8b1b-81c036c8fe60  ethernet  -- 

配置 bond0 设备网络信息

[root@linuxprobe ~]# nmcli c modify bond0 ipv4.address 192.168.203.200/24
[root@linuxprobe ~]# nmcli c modify bond0 ipv4.gateway 192.168.203.1
[root@linuxprobe ~]# nmcli c modify bond0 ipv4.dns 192.168.203.1
[root@linuxprobe ~]# nmcli c modify bond0 ipv4.dns-search linuxprobe.com
[root@linuxprobe ~]# nmcli c modify bond0 ipv4.method manual
  • ipv4支持的配置项:method, dns, dns-search, dns-options, dns-priority, addresses, gateway, routes, route-metric, route-table, ignore-auto-routes, ignore-auto-dns, dhcp-client-id, dhcp-timeout, dhcp-send-hostname, dhcp-hostname, dhcp-fqdn, never-default, may-fail, dad-timeout

启动网卡

[root@linuxprobe ~]# nmcli c up bond0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)

# d 是 device
[root@linuxprobe ~]# nmcli d status
DEVICE      TYPE      STATE      CONNECTION 
bond0       bond      connected  bond0      
ens160      ethernet  connected  ens160     
ens192      ethernet  connected  ens192     
virbr0      bridge    connected  virbr0     
lo          loopback  unmanaged  --         
virbr0-nic  tun       unmanaged  -- 

[root@linuxprobe ~]# ifconfig
bond0: flags=5123<UP,BROADCAST,MASTER,MULTICAST>  mtu 1500
        inet 192.168.203.200  netmask 255.255.255.0  broadcast 192.168.203.255
        ether 82:bb:8f:6f:0c:ab  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

此时,访问 192.168.203.200 ,实际是 ens160 和 ens192 同时在提供服务。