redhat / Centos 的nmcli命令参数和使用样例中英文对照详解

redhat 、 centos 操作系统从 7开始,提供了nmcli命令来管理网络,本文介绍nmcli命令参数和使用样例中英文对照详解。

nmcli是什么?

nmcli是用于控制NetworkManager的命令行工具。

nmcli - command-line tool for controlling NetworkManager

简介

包含了如下几个部分:

可选参数 OPTIONS
功能命令分类
可选的命令
可选的命令参数几部分。

nmcli [OPTIONS…] {help | general | networking | radio | connection | device | agent | monitor} [COMMAND] [ARGUMENTS…]

详细描述

nmcli是用于控制NetworkManager和报告网络状态的命令行工具。它可以作为nm小程序或其他图形客户端的替代品。nmcli用于创建、显示、编辑、删除、激活和停用网络连接,以及控制和显示网络设备状态。

典型用途包括:

  • 脚本:通过nmcli使用NetworkManager,而不是手动管理网络连接。nmcli支持更适合脚本处理的简洁输出格式。请注意,NetworkManager还可以执行脚本,称为“调度程序脚本”,以响应网络事件。
  • 服务器、无头机器和终端:nmcli可用于控制NetworkManager,而无需GUI,包括创建、编辑、启动和停止网络连接以及查看网络状态。
nmcli is a command-line tool for controlling NetworkManager and reporting network status. It can be utilized as a replacement for nm-applet or other graphical clients.  nmcli is used to create, display, edit, delete, activate, and deactivate network connections, as well as control and display network device status. See nmcli-examples(7) for ready to run nmcli examples.
       Typical uses include:
       ·   Scripts: Utilize NetworkManager via nmcli instead of managing network connections manually.  nmcli supports a terse output format which is better suited for script processing. Note that NetworkManager can also execute scripts, called "dispatcher scripts", in response to network events. 
       ·   Servers, headless machines, and terminals: nmcli can be used to control NetworkManager without a GUI, including creating,editing, starting and stopping network connections and viewing network status.

选项[OPTIONS]

-a | --ask

使用该参数,将暂停程序进行,要求输入任何确实的参数,所以不要非交互模式下使用。这个参数控制,比如在脚本里,需要建立网络连接时,是否需要提示您输入密码这种情况。

-a | --ask
           When using this option nmcli will stop and ask for any missing required arguments, so do not use this option for non-interactive purposes like scripts. This option controls, for example, whether you will be prompted for a password if it is required for connecting to a network.

-c | --colors {yes | no | auto}

控制颜色输出。

       -c | --colors {yes | no | auto}
           This option controls color output (using terminal escape sequences).  yes enables colors, no disables them, auto only produces colors when standard output is directed to a terminal. The default value is auto.
           The actual colors used are configured as described in terminal-colors.d(5). Please refer to the COLORS section for a list of color names supported by nmcli.
           If the environment variable NO_COLOR is set (to any value), then coloring is disabled with mode "auto". Explicitly enabling coloring overrides the environment variable.

样例。当参数为no时,一些关键参数状态没有了特殊颜色显示。

nmcli -从--color 参数样例

--complete-args

得到最后一个参数的完整格式。

       --complete-args
           Instead of conducting the desired action, nmcli will list possible completions for the last argument. This is useful to implement argument completion in shell.
           The exit status will indicate success or return a code 65 to indicate the last argument is a file name.
           NetworkManager ships with command completion support for GNU Bash.

样例

[root@redhat8_3 ~]# nmcli --complete-args d
device
[root@redhat8_3 ~]# nmcli --complete-args
[root@redhat8_3 ~]# nmcli --complete-args d
device
[root@redhat8_3 ~]# nmcli --complete-args c
connection
[root@redhat8_3 ~]# nmcli --complete-args r
radio
[root@redhat8_3 ~]# nmcli --complete-args n
networking
[root@redhat8_3 ~]# nmcli --complete-args g
general
[root@redhat8_3 ~]# nmcli --complete-args a
agent
[root@redhat8_3 ~]# nmcli --complete-args m
monitor
[root@redhat8_3 ~]#

-e | --escape {yes | no}

是否以简洁的表格模式转义:和\字符。转义字符是\ 右斜杠。

       -e | --escape {yes | no}
           Whether to escape : and \ characters in terse tabular mode. The escape character is \.
           If omitted, default is yes.

-f | --fields {field1,field2... | all | common}

指定某个命令希望输出的列。不同命令列不同。默认common是常见的,all是全部。可以从all里面选择一些期望的写道本参数里面。多个列用逗号分割。

       -f | --fields {field1,field2... | all | common}
           This option is used to specify what fields (column names) should be printed. Valid field names differ for specific commands. List available fields by providing an invalid value to the --fields option.  all is used to print all valid
 field values of the command.  common is used to print common field values of the command.

           If omitted, default is common.

样例

[root@redhat8_3 ~]# nmcli d status
DEVICE      TYPE      STATE                   CONNECTION
enp0s3      ethernet  connected               enp0s3
virbr0      bridge    connected (externally)  virbr0
lo          loopback  unmanaged               --
virbr0-nic  tun       unmanaged               --
[root@redhat8_3 ~]# nmcli -f all d status
DEVICE      TYPE      STATE                   IP4-CONNECTIVITY  IP6-CONNECTIVITY  DBUS-PATH                                  CONNECTION  CON-UUID                              CON-PATH
enp0s3      ethernet  connected               full              full              /org/freedesktop/NetworkManager/Devices/2  enp0s3      09d1338a-f596-4d9d-9992-df91df19611c  /org/freedesktop/NetworkManager/ActiveConnection/1
virbr0      bridge    connected (externally)  limited           limited           /org/freedesktop/NetworkManager/Devices/3  virbr0      6634eea4-03ef-4476-8fea-0c3d9a2a7fbc  /org/freedesktop/NetworkManager/ActiveConnection/2
lo          loopback  unmanaged               unknown           unknown           /org/freedesktop/NetworkManager/Devices/1  --          --                                    --
virbr0-nic  tun       unmanaged               none              none              /org/freedesktop/NetworkManager/Devices/4  --          --                                    --
[root@redhat8_3 ~]# nmcli -f DEVICE,CONNECTION,STATE,TYPE d status
DEVICE      CONNECTION  STATE                   TYPE
enp0s3      enp0s3      connected               ethernet
virbr0      virbr0      connected (externally)  bridge
lo          --          unmanaged               loopback
virbr0-nic  --          unmanaged               tun
[root@redhat8_3 ~]# nmcli -f DEVICE,CONNECTION,STATE d status
DEVICE      CONNECTION  STATE
enp0s3      enp0s3      connected
virbr0      virbr0      connected (externally)
lo          --          unmanaged
virbr0-nic  --          unmanaged
[root@redhat8_3 ~]#

-g | --get-values {field1,field2... | all | common}

打印某个指定字段的值。如果指定了多个,则输出在一行,字段之间用冒号分割。

       -g | --get-values {field1,field2... | all | common}
           This option is used to print values from specific fields. It is basically a shortcut for --mode tabular --terse --fields and is a convenient way to retrieve values for particular fields. The values are printed one per line without headers.
           If a section is specified instead of a field, the section name will be printed followed by colon separated values of the
 fields belonging to that section, all on the same line.

样例

[root@redhat8_3 ~]# nmcli -g DEVICE d status
enp0s3
virbr0
lo
virbr0-nic
[root@redhat8_3 ~]# nmcli -g DEVICE,STATE d status
enp0s3:connected
virbr0:connected (externally)
lo:unmanaged
virbr0-nic:unmanaged
[root@redhat8_3 ~]#

-h | --help

帮助信息

       -h | --help
           Print help information.

-m | --mode {tabular | multiline}

指定数据样式是表格形式,还是多行形式。 多行指每个字段或属性都单独一行输出。

       -m | --mode {tabular | multiline}
           Switch between tabular and multiline output:
           tabular
               Output is a table where each line describes a single entry. Columns define particular properties of the entry.
           multiline
               Each entry comprises multiple lines, each property on its own line. The values are prefixed with the property name.
           If omitted, default is tabular for most commands. For the commands producing more structured information, that cannot be
           displayed on a single line, default is multiline. Currently, they are:
           ·   nmcli connection show ID
           ·   nmcli device show

样例

[root@redhat8_3 ~]# nmcli -m tabular d status
DEVICE      TYPE      STATE                   CONNECTION
enp0s3      ethernet  connected               enp0s3
virbr0      bridge    connected (externally)  virbr0
lo          loopback  unmanaged               --
virbr0-nic  tun       unmanaged               --
[root@redhat8_3 ~]# nmcli -m multiline d status
DEVICE:                                 enp0s3
TYPE:                                   ethernet
STATE:                                  connected
CONNECTION:                             enp0s3
DEVICE:                                 virbr0
TYPE:                                   bridge
STATE:                                  connected (externally)
CONNECTION:                             virbr0
DEVICE:                                 lo
TYPE:                                   loopback
STATE:                                  unmanaged
CONNECTION:                             --
DEVICE:                                 virbr0-nic
TYPE:                                   tun
STATE:                                  unmanaged
CONNECTION:                             --
[root@redhat8_3 ~]#

-p | --pretty

用于更适合人眼阅读的方式显示,包括头,数据对齐等。

       -p | --pretty
           Output is pretty. This causes nmcli to produce easily readable outputs for humans, i.e. values are aligned, headers are printed, etc.

样例

[root@redhat8_3 ~]# nmcli -p d status
=====================
  Status of devices
=====================
DEVICE      TYPE      STATE                   CONNECTION
-------------------------------------------------------------------------------
enp0s3      ethernet  connected               enp0s3
virbr0      bridge    connected (externally)  virbr0
lo          loopback  unmanaged               --
virbr0-nic  tun       unmanaged               --
[root@redhat8_3 ~]#

-s | --show-secrets

       -s | --show-secrets
           When using this option nmcli will display passwords and secrets that might be present in an output of an operation. This option also influences echoing passwords typed by user as an input.

-t | --terse

该参数和--pretty冲突。

用于计算处理,比如脚本,更适合的方式显示。

       -t | --terse
           Output is terse. This mode is designed and suitable for computer (script) processing.

样例

[root@redhat8_3 ~]# nmcli -t d status
enp0s3:ethernet:connected:enp0s3
virbr0:bridge:connected (externally):virbr0
lo:loopback:unmanaged:
virbr0-nic:tun:unmanaged:
[root@redhat8_3 ~]#

-v | --version

显示版本号

       -v | --version
           Show nmcli version.

样例

[root@redhat8_3 ~]# nmcli -v
nmcli tool, version 1.26.0-8.el8

-w | --wait seconds

此选项设置nmcli等待NetworkManager完成操作的超时时间。它对于可能需要较长时间才能完成的命令特别有用,例如连接激活。

指定值0指示nmcli不等待,而是立即退出,状态为success。默认值取决于执行的命令

       -w | --wait seconds
           This option sets a timeout period for which nmcli will wait for NetworkManager to finish operations. It is especially useful for commands that may take a longer time to complete, e.g. connection activation.
           Specifying a value of 0 instructs nmcli not to wait but to exit immediately with a status of success. The default value depends on the executed command.

样例。如下因为网络没问题,速度都很快。

[root@redhat8_3 ~]# nmcli -w 5 d status
DEVICE      TYPE      STATE                   CONNECTION
enp0s3      ethernet  connected               enp0s3
virbr0      bridge    connected (externally)  virbr0
lo          loopback  unmanaged               --
virbr0-nic  tun       unmanaged               --
[root@redhat8_3 ~]# nmcli -w 5000 d status
DEVICE      TYPE      STATE                   CONNECTION
enp0s3      ethernet  connected               enp0s3
virbr0      bridge    connected (externally)  virbr0
lo          loopback  unmanaged               --
virbr0-nic  tun       unmanaged               --
[root@redhat8_3 ~]# nmcli -w 0 d status
DEVICE      TYPE      STATE                   CONNECTION
enp0s3      ethernet  connected               enp0s3
virbr0      bridge    connected (externally)  virbr0
lo          loopback  unmanaged               --
virbr0-nic  tun       unmanaged               --
[root@redhat8_3 ~]#

一般命令【GENERAL COMMANDS】

nmcli general {status | hostname | permissions | logging} [ARGUMENTS…]

用于显示NetworkManager的状态和权限,也可以获取和修改系统主机名,日志等级和域。

Use this command to show NetworkManager status and permissions. You can also get and change system hostname, as well as NetworkManager logging level and domains.

status

显示NetworkManager的总体状态。当没有为nmcli general提供其他命令时,这是默认操作。

Show overall status of NetworkManager. This is the default action, when no additional command is provided for nmcli general.

样例

[root@redhat8_3 ~]# nmcli general status
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN
connected  full          enabled  enabled  enabled  enabled
[root@redhat8_3 ~]# nmcli -p general status
=========================
  NetworkManager status
=========================
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN
--------------------------------------------------------------------------------------
connected  full          enabled  enabled  enabled  enabled
[root@redhat8_3 ~]#

hostname [hostname]

获取和更改系统主机名。如果没有参数,则返回当前配置的主机名。当您传递主机名时,它将被移交给NetworkManager以设置为新的系统主机名。

注意,术语“System”主机名也可以被其他程序或工具称为“持久”或“静态”。在大多数发行版中,主机名存储在/etc/hostname文件中。例如,systemd hostnamed服务使用术语“静态”主机名,并且它仅在启动时读取/etc/hostname文件。

           Get and change system hostname. With no arguments, this prints currently configured hostname. When you pass a hostname, it will be handed over to NetworkManager to be set as a new system hostname.

           Note that the term "system" hostname may also be referred to as "persistent" or "static" by other programs or tools. The hostname is stored in /etc/hostname file in most distributions. For example, systemd-hostnamed service uses the term "static" hostname and it only reads the /etc/hostname file when it starts.

样例

[root@redhat8_3 ~]# nmcli -p general status
=========================
  NetworkManager status
=========================
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN
--------------------------------------------------------------------------------------
connected  full          enabled  enabled  enabled  enabled
[root@redhat8_3 ~]# nmcli -p general hostname
redhat8_3
[root@redhat8_3 ~]# nmcli -p general hostname redhat8
[root@redhat8_3 ~]# su
[root@redhat8_3 ~]# nmcli -p general hostname redhat8_3
[root@redhat8_3 ~]# nmcli -p general hostname
redhat8_3
[root@redhat8_3 ~]#

permissions

显示调用者对NetworkManager提供的各种已验证操作的权限,如启用和禁用网络、更改Wi-Fi和WWAN状态、修改连接等。

           Show the permissions a caller has for various authenticated operations that NetworkManager provides, like enable and disable networking, changing Wi-Fi and WWAN state, modifying connections, etc.

样例

[root@redhat8_3 ~]# nmcli -p general permission
==============================
  NetworkManager permissions
==============================
PERMISSION                                                        VALUE
-------------------------------------------------------------------------------------------------------
org.freedesktop.NetworkManager.checkpoint-rollback                yes
org.freedesktop.NetworkManager.enable-disable-connectivity-check  yes
org.freedesktop.NetworkManager.enable-disable-network             yes
org.freedesktop.NetworkManager.enable-disable-statistics          yes
org.freedesktop.NetworkManager.enable-disable-wifi                yes
org.freedesktop.NetworkManager.enable-disable-wimax               yes
org.freedesktop.NetworkManager.enable-disable-wwan                yes
org.freedesktop.NetworkManager.network-control                    yes
org.freedesktop.NetworkManager.reload                             yes
org.freedesktop.NetworkManager.settings.modify.global-dns         yes
org.freedesktop.NetworkManager.settings.modify.hostname           yes
org.freedesktop.NetworkManager.settings.modify.own                yes
org.freedesktop.NetworkManager.settings.modify.system             yes
org.freedesktop.NetworkManager.sleep-wake                         yes
org.freedesktop.NetworkManager.wifi.scan                          yes
org.freedesktop.NetworkManager.wifi.share.open                    yes
org.freedesktop.NetworkManager.wifi.share.protected               yes
[root@redhat8_3 ~]#

logging [level level] [domains domains…]

获取并更改NetworkManager日志记录级别和域。不带任何参数,将显示当前日志记录级别和域。

要更改日志记录状态,请提供级别和或域参数。查看NetworkManager.conf(5) 获取可用的级别和域值。

           Get and change NetworkManager logging level and domains. Without any argument current logging level and domains are shown.
           In order to change logging state, provide level and, or, domain parameters. See NetworkManager.conf(5) for available level and domain values.

网络控制命令(NETWORKING CONTROL COMMANDS)

nmcli networking {on | off | connectivity} [ARGUMENTS…]

查询NetworkManager网络状态,启用和禁用网络。

   Query NetworkManager networking status, enable and disable networking.

on, off

启用或禁用NetworkManager的网络控制。禁用网络时,由NetworkManager管理的所有接口都将停用。

Enable or disable networking control by NetworkManager. All interfaces managed by NetworkManager are deactivated when networking is disabled.

样例。 注意,一旦OFF,网络就【断了】你确认要关闭网络吗?

[root@redhat8_3 ~]# nmcli networking off
[root@redhat8_3 ~]#

最后还是通过终端,把网络打开的。测试了2次,确实网络不通了。

connectivity [check]

获取网络连接状态。可选的check参数告诉NetworkManager重新检查连接,否则将显示最新的已知连接状态而不重新检查。

Get network connectivity state. The optional check argument tells NetworkManager to re-check the connectivity, else the most recent known connectivity state is displayed without re-checking.

样例

[root@redhat8_3 ~]# nmcli networking connectivity
full
[root@redhat8_3 ~]#

返回值如下

none 主机未连接到任何网络。
portal 主机位于捕获门户之后,无法访问完整的Internet。
limited 主机已连接到网络,但无法访问Internet。
full 主机已连接到网络并可以完全访问Internet。
unknown 未知的连接状态

           none
               the host is not connected to any network.
           portal
               the host is behind a captive portal and cannot reach the full Internet.
           limited
               the host is connected to a network, but it has no access to the Internet.
           full
               the host is connected to a network and has full access to the Internet.
           unknown
               the connectivity status cannot be found out.

无线电传输控制命令(RADIO TRANSMISSION CONTROL COMMANDS)

显示无线电开关状态,或启用和禁用开关。

   nmcli radio {all | wifi | wwan} [ARGUMENTS...]
   Show radio switches status, or enable and disable the switches.

wifi [on | off]

在NetworkManager中显示或设置Wi-Fi的状态。

  • 如果未提供参数,则打印Wi-Fi状态;
  • on 可启用Wi-Fi;
  • off 可禁用Wi-Fi。
           Show or set status of Wi-Fi in NetworkManager. If no arguments are supplied, Wi-Fi status is printed; on enables Wi-Fi;
           off disables Wi-Fi.

wwan [on | off]

在NetworkManager中显示或设置WWAN(移动宽带)的状态。

  • 如果未提供参数,则打印移动宽带状态;
  • on启用移动宽带;
  • off禁用。
           Show or set status of WWAN (mobile broadband) in NetworkManager. If no arguments are supplied, mobile broadband status is printed; on enables mobile broadband, off disables it.

all [on | off]

显示或设置前面提到的所有无线电开关

           Show or set all previously mentioned radio switches at the same time.

样例

[root@redhat8_3 ~]# nmcli radio
WIFI-HW  WIFI     WWAN-HW  WWAN
enabled  enabled  enabled  enabled
[root@redhat8_3 ~]# nmcli radio all off
[root@redhat8_3 ~]# nmcli radio
WIFI-HW  WIFI      WWAN-HW  WWAN
enabled  disabled  enabled  disabled
[root@redhat8_3 ~]# nmcli radio all on
[root@redhat8_3 ~]# nmcli radio
WIFI-HW  WIFI     WWAN-HW  WWAN
enabled  enabled  enabled  enabled
[root@redhat8_3 ~]#

活动监视(ACTIVITY MONITOR)

nmcli monitor

观察NetworkManager活动。监视连接状态、设备或连接配置文件中的更改。

另请参阅nmcli connection monitor和nmcli device monitor以监视某些设备或连接中的更改。

   Observe NetworkManager activity. Watches for changes in connectivity state, devices or connection profiles.
   See also nmcli connection monitor and nmcli device monitor to watch for changes in certain devices or connections.


connection 连接管理

命令格式:nmcli connection {show|up|down|modify|add|edit|clone|delete|monitor|reload|load|import|export}
这是主要使用的一个功能。

show

show有两种用法,分别是:

1. 列出活动的连接,或进行排序(+-为升降序)

# 查看所有连接状态
[root@www ~]# nmcli connection show
# 等同于nmcli connection show --order +active
[root@www ~]# nmcli connection show --active
# 以活动的连接进行排序
[root@www ~]# nmcli connection show --order +active
# 将所有连接以名称排序
[root@www ~]# nmcli connection show --order +name
# 将所有连接以类型排序(倒序)
[root@www ~]# nmcli connection show --order -type

2. 查看指定连接的详细信息

[root@www ~]# nmcli connection show eth0
# 省略......

up

激活连接,提供连接名称或uuid进行激活,若未提供,则可以使用ifname指定设备名进行激活。

# 以连接名进行激活
[root@www ~]# nmcli connection up eth0
# 以uuid进行激活
[root@www ~]# nmcli connection up 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
# 以设备接口名进行激活
[root@www ~]# nmcli connection up ifname eth0

down

停用连接,提供连接名或uuid进行停用,若未提供,则可以使用ifname指定设备名进行激活。

# 以连接名进行激活
[root@www ~]# nmcli connection down eth0
# 以uuid进行激活
[root@www ~]# nmcli connection down 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
# 以设备接口名进行激活
[root@www ~]# nmcli connection down ifname eth0

modify

这些属性可以用nmcli connection show eth0进行获取,然后可以修改、添加或删除属性,若要设置属性,只需指定属性名称后跟值,空值将删除属性值,同一属性添加多个值使用+。同一属性删除指定值用-加索引。

添加多个ip

# 添加三个
[root@www ~]# nmcli connection modify eth0 +ipv4.addresses 192.168.100.102/24
[root@www ~]# nmcli connection modify eth0 +ipv4.addresses 192.168.100.103/24
[root@www ~]# nmcli connection modify eth0 +ipv4.addresses 192.168.100.104/24
# 查看
[root@www ~]# nmcli -f IP4 connection show eth0
IP4.ADDRESS[1]:                         192.168.100.101/24
IP4.GATEWAY:                            192.168.100.100
IP4.DNS[1]:                             8.8.8.8
# 启用配置
[root@www ~]# nmcli connection up eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/18)
# 再次查看
[root@www ~]# nmcli -f IP4 connection show eth0
IP4.ADDRESS[1]:                         192.168.100.101/24
IP4.ADDRESS[2]:                         192.168.100.102/24
IP4.ADDRESS[3]:                         192.168.100.103/24
IP4.ADDRESS[4]:                         192.168.100.104/24
IP4.GATEWAY:                            192.168.100.100
IP4.DNS[1]:                             8.8.8.8

删除指定ip

[root@www ~]# nmcli -f IP4 connection show eth0
IP4.ADDRESS[1]:                         192.168.100.101/24
IP4.ADDRESS[2]:                         192.168.100.102/24
IP4.ADDRESS[3]:                         192.168.100.103/24
IP4.ADDRESS[4]:                         192.168.100.104/24
IP4.GATEWAY:                            192.168.100.100
IP4.DNS[1]:                             8.8.8.8
# 删除索当前索引为2的地址
[root@www ~]# nmcli connection modify eth0 -ipv4.addresses 2
# 查看
[root@www ~]# nmcli -f IP4 connection show eth0
IP4.ADDRESS[1]:                         192.168.100.101/24
IP4.ADDRESS[2]:                         192.168.100.102/24
IP4.ADDRESS[3]:                         192.168.100.103/24
IP4.ADDRESS[4]:                         192.168.100.104/24
IP4.GATEWAY:                            192.168.100.100
IP4.DNS[1]:                             8.8.8.8
# 再次激活
[root@www ~]# nmcli connection up eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/19)
# 查看
[root@www ~]# nmcli -f IP4 connection show eth0
IP4.ADDRESS[1]:                         192.168.100.101/24
IP4.ADDRESS[2]:                         192.168.100.102/24
IP4.GATEWAY:                            192.168.100.100
IP4.DNS[1]:                             8.8.8.8

add

这是创建一个新的连接,需要指定新创建连接的属性,语法与modify相同。

[root@www ~]# nmcli con add con-name eth1 type ethernet  autoconnect yes ifname eth0
# con-name     连接名称
# type              连接类型
# autoconnect 是否自动连接
# ifname          连接到的设备名称

更多的类型或方法可以使用nmcli connection add help查看。

clone

克隆连接,克隆一个存在的连接,除了连接名称和uuid是新生成的,其他都是一样的。

[root@www ~]# nmcli connection clone eth0 eth0_1

delete

删除连接,这将删除一个连接。

[root@www ~]# nmcli connection delete eth0_1

load

从磁盘加载/重新加载一个或多个连接文件,例如你手动创建了一个/etc/sysconfig/network-scripts/ifcfg-ethx连接文件,你可以将其加载到网络管理器,以便管理。

[root@www ~]# echo -e "TYPE=Ethernet\nNAME=ethx" > /etc/sysconfig/network-scripts/ifcfg-ethx
[root@www ~]# nmcli connection show
NAME  UUID                                  TYPE            DEVICE 
eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  802-3-ethernet  eth0 
[root@www ~]# nmcli connection load /etc/sysconfig/network-scripts/ifcfg-ethx 
[root@www ~]# nmcli connection show
NAME  UUID                                  TYPE            DEVICE 
eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  802-3-ethernet  eth0   
ethx  d45d97fb-8530-60e2-2d15-d92c0df8b0fc  802-3-ethernet  --

monitor

监视连接配置文件活动。每当指定的连接更改时, 此命令都会打印一行。要监视的连接由其名称、UUID 或 D 总线路径标识。如果 ID 不明确, 则可以使用关键字 id、uuid 或路径。有关 ID 指定关键字的说明, 请参阅上面的连接显示。

监视所有连接配置文件, 以防指定无。当所有监视的连接消失时, 该命令将终止。如果要监视连接创建, 请考虑使用带有 nmcli 监视器命令的全局监视器。

[root@www ~]# nmcli connection monitor eth0

device 设备管理

命令格式:nmcli device {status|show|set|connect|reapply|modify|disconnect|delete|monitor|wifi|lldp}
显示和管理设备接口。该选项有很多功能,例如连接wifi,创建热点,扫描无线,邻近发现等,下面仅列出常用选项。详细功能可使用nmcli device help查看。

status

打印设备状态,如果没有将命令指定给nmcli device,则这是默认操作。

[root@www ~]# nmcli device status
DEVICE  TYPE      STATE      CONNECTION 
eth0    ethernet  connected  eth0       
lo      loopback  unmanaged  --         
[root@www ~]# nmcli device
DEVICE  TYPE      STATE      CONNECTION 
eth0    ethernet  connected  eth0       
lo      loopback  unmanaged  --

show

显示所有设备接口的详细信息。

# 不指定设备接口名称,则显示所有接口的信息
[root@www ~]# nmcli device show eth0
GENERAL.DEVICE:                         eth0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:0C:29:99:9A:A1
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     eth0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/9
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.100.101/24
IP4.ADDRESS[2]:                         192.168.100.102/24
IP4.GATEWAY:                            192.168.100.100
IP4.DNS[1]:                             8.8.8.8

set

设置设备属性

[root@www ~]# nmcli device set ifname eth0 autoconnect yes

connect

连接设备。提供一个设备接口,网络管理器将尝试找到一个合适的连接, 将被激活。它还将考虑未设置为自动连接的连接。(默认超时为90s)

[root@www ~]# nmcli dev connect eth0
Device 'eth0' successfully activated with '5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03'.

reapply

使用上次应用后对当前活动连接所做的更改来更新设备。

[root@www ~]# nmcli device reapply eth0
Connection successfully reapplied to device 'eth0'.

modify

修改设备上处于活动的设备,但该修改只是临时的,并不会写入文件。(语法与 nmcli connection modify 相同)

[root@www ~]# nmcli device modify eth0 +ipv4.addresses 192.168.100.103/24
Connection successfully reapplied to device 'eth0'.
[root@www ~]# nmcli dev show eth0
[root@www ~]# nmcli device modify eth0 -ipv4.addresses 1
Connection successfully reapplied to device 'eth0'.

disconnect

断开当前连接的设备,防止自动连接。但注意,断开意味着设备停止!但可用 connect 进行连接

[root@www ~]# nmcli device disconnect eth0

delete

删除设备,该命令从系统中删除接口。请注意, 这仅适用于诸如bonds, bridges, teams等软件设备。命令无法删除硬件设备 (如以太网)。超时时间为10秒

[root@www ~]# nmcli device delete bonds

monitor

监视设备活动。每当指定的设备更改状态时, 此命令都会打印一行。

监视所有设备以防未指定接口。当所有指定的设备消失时, 监视器将终止。如果要监视设备添加, 请考虑使用带有 nmcli 监视器命令的全局监视器。

[root@www ~]# nmcli device monitor eth0

nmcli 返回状态码

mcli 如果成功退出状态值为0,如果发生错误则返回大于0的值。

  • 0: 成功-指示操作已成功
  • 1: 位置或指定的错误
  • 2: 无效的用户输入,错误的nmcli调用
  • 3: 超时了(请参阅 --wait 选项)
  • 4: 连接激活失败
  • 5: 连接停用失败
  • 6: 断开设备失败
  • 7: 连接删除失败
  • 8: 网络管理器没有运行
  • 10: 连接、设备或接入点不存在
  • 65: 当使用 --complete-args 选项,文件名应遵循。