出自https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/networking_guide/sec-using_the_networkmanager_command_line_tool_nmcli

 

 使用 NetworkManager 命令行工具 nmcli

用户和脚本都可使用命令行工具 nmcli 控制 NetworkManager。该命令的基本格式为:
nmcli OPTIONS OBJECT { COMMAND | help }
其中 OBJECT 可为 generalnetworkingradioconnectiondevice 之一。最常用的选项为:-t, --terse(用于脚本)、-p, --pretty 选项(用于用户)及 -h, --help 选项。在 nmcli 中采用命令完成功能,无论何时您不确定可用的命令选项时,都可以按 Tab 查看。有关选项及命令的完整列表,请查看 nmcli(1) man page。
nmcli 工具有一些内置上下文相关的帮助信息。例如:运行以下两个命令,并注意不同之处:
~]$ nmcli help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }

OPTIONS
  -t[erse]                                   terse output
  -p[retty]                                  pretty output
  -m[ode] tabular|multiline                  output mode
  -f[ields] <field1,field2,...>|all|common   specify fields to output
  -e[scape] yes|no                           escape columns separators in values
  -n[ocheck]                                 don't check nmcli and NetworkManager versions
  -a[sk]                                     ask for missing parameters
  -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
~]$ nmcli general help
Usage: nmcli general { COMMAND | help }

  COMMAND := { status | hostname | permissions | logging }

  status

  hostname [<hostname>]

  permissions

  logging [level <log level>] [domains <log domains>]
在上面的第二个示例中,这个帮助信息与对象 general 有关。
nmcli-examples(5) man page 有很多有帮助的示例,节选如下:
显示 NetworkManager 总体状态:
nmcli general status
要控制 NetworkManager 日志记录:
nmcli general logging
要显示所有链接:
nmcli connection show
要只显示当前活动链接,如下所示添加 -a, --active
nmcli connection show --active
显示由 NetworkManager 识别到设备及其状态:
nmcli device status
可简化命令并省略一些选项。例如:可将命令
nmcli connection modify id 'MyCafe' 802-11-wireless.mtu 1350
简化为
nmcli con mod MyCafe 802-11-wireless.mtu 1350
可省略 id 选项,因为在这种情况下对于 nmcli 来说连接 ID(名称)是明确的。您熟悉这些命令后可做进一步简化。例如:可将
nmcli connection add type ethernet
改为
nmcli c a type eth

注意

如有疑问,请使用 tab 完成功能。

使用 nmcli 启动和停止接口

可使用 nmcli 工具启动和停止任意网络接口,其中包括主接口。例如:
nmcli con up id bond0
nmcli con up id port0
nmcli dev disconnect iface bond0
nmcli dev disconnect iface ens3

注意

建议使用 nmcli dev disconnect iface iface-name 命令,而不是 nmcli con down id id-string 命令,因为连接断开可将该接口放到手动”模式,这样做用户让 NetworkManager 启动某个连接前,或发生外部事件(比如载波变化、休眠或睡眠)前,不会启动任何自动连接。

nmcli 互动连接编辑器

nmcli 工具有一个互动连接编辑器。请运行以下命令使用该工具:
~]$ nmcli con edit
此时会提示您从显示的列表中选择有效连接类型。输入连接类型后,就会为您显示 nmcli 提示符。如果您熟悉连接类型,也可以在 nmcli con edit 命令中添加 type 选项,从而直接进入提示符。编辑现有连接配置的格式如下:
nmcli con edit [id | uuid | path] ID
要添加和编辑新连接配置,请采用以下格式:
nmcli con edit [type new-connection-type] [con-name new-connection-name]
nmcli 提示符后输入 help 查看可用命令列表。请使用 describe 命令获取设置及其属性描述,格式如下:
describe setting.property
例如:
nmcli> describe team.config

2.3.1. 了解 nmcli 选项

很多 nmcli 命令是可以顾名思义的,但有几个选项需要进一步了解:
type — 连接类型。
允许值为:adsl, bond, bond-slave, bridge, bridge-slave, bluetooth, cdma, ethernet, gsm, infiniband, olpc-mesh, team, team-slave, vlan, wifi, wimax.
每个连接了类型都有具体类型的命令选项。按 Tab 键查看该列表,或查看 nmcli(1) man page 中的 TYPE_SPECIFIC_OPTIONS 列表。type 选项可用于如下命令:nmcli connection addnmcli connection edit
con-name — 为连接配置分配的名称。
如果未指定连接名称,则会以如下格式生成名称:
 type-ifname[-number]
连接名称是 connection profile 的名称,不应与代表某个设备的名称混淆(比如 wlan0、ens3、em1 等等)。虽然用户可为根据接口为链接命名,但这是两回事。一个设备可以有多个连接配置文件。这对移动设备,或者在不同设备间反复切换网线时很有帮助。与其编辑该配置,不如创建不同的配置文件,并根据需要将其应用到接口中。id 选项也是指连接配置文件名称。
id — 用户为连接配置文件分配的身份字符串。
可在 nmcli connection 命令中用来识别某个连接的 ID。输出结果中的 NAME 字段永远代表连接 ID(名称)。它指的是 con-name 给出的同一连接配置文件名称。
uuid — 系统为连接配置文件分配的独有身份字符串。
可在 nmcli connection 命令中用来识别某个连接的 UUID。

2.3.2. 使用 nmcli 连接到网络

请使用以下命令列出目前可用的网络连接:
~]$ nmcli con show
NAME              UUID                                  TYPE            DEVICE
Auto Ethernet     9b7f2511-5432-40ae-b091-af2457dfd988  802-3-ethernet  --
ens3              fb157a65-ad32-47ed-858c-102a48e064a2  802-3-ethernet  ens3
MyWiFi            91451385-4eb8-4080-8b82-720aab8328dd  802-11-wireless wlan0
注:输出结果中的 NAME 字段永远代表连接 ID(名称)。它不是接口名称(尽管看起来很像)。在上述示例的第二个连接中,NAME 字段中的 ens3 代表用户为在 ens3 接口中应用的配置文件分配的连接 ID。在所示最后一个连接中,用户将连接 ID MyWiFi 分配给接口 wlan0。
添加以太网连接意味着生成一个配置文件,并将其分配给某个设备。生成新配置文件前,请检查可用设备,方法如下:
~]$ nmcli dev status
DEVICE  TYPE      STATE         CONNECTION
ens3    ethernet  disconnected  --
ens9    ethernet  disconnected  --
lo      loopback  unmanaged     --

添加动态以太网连接

要使用动态 IP 配置添加以太网配置文件,以便 DHCP 分配网络配置,可使用如下格式的命令:
nmcli connection add type ethernet con-name connection-name ifname interface-name
例如:请使用以下命令创建名为 my-office 的动态连接配置文件:
~]$ nmcli con add type ethernet con-name my-office ifname ens3
Connection 'my-office' (fb157a65-ad32-47ed-858c-102a48e064a2) successfully added.
NetworkManager 会将内部参数 connection.autoconnect 设定为 yesNetworkManager 还会将设置保存到 /etc/sysconfig/network-scripts/ifcfg-my-office 文件中,在该文件中会将 ONBOOT 指令设定为 yes
注:再次激活该接口前,NetworkManager 不会意识到对 ifcfg 文件的手动更改。有关使用配置文件的详情,请查看 第 1.9 节 “使用 sysconfig 文件进行网络配置”
请使用以下命令激活以太网连接:
~]$ nmcli con up my-office
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
检查这些设备及连接的状态:
~]$ nmcli device status
DEVICE  TYPE      STATE         CONNECTION
ens3    ethernet  connected     my-office
ens9    ethernet  disconnected  --
lo      loopback  unmanaged     --
要更改主机发送到 DHCP 服务器的主机名,请按照以下方法修改 dhcp-hostname 属性:
~]$ nmcli con modify my-office my-office ipv4.dhcp-hostname host-name ipv6.dhcp-hostname host-name
要更改主机发送到 DHCP 服务器的 IPv4 客户端 ID,请按照以下方法修改 dhcp-client-id 属性:
~]$ nmcli con modify my-office my-office ipv4.dhcp-client-id client-ID-string
没有用于 IPv6dhcp-client-id 属性,dhclientIPv6 生成识别符。详情请查看 dhclient(8) man page。
要忽略 DHCP 服务器发送到主机的 DNS 服务器,请按照以下操作修改 ignore-auto-dns 属性:
~]$ nmcli con modify my-office my-office ipv4.ignore-auto-dns yes ipv6.ignore-auto-dns yes
有关属性及其设置的详情请查看 nm-settings(5) man page。

例 2.1. 使用互动编辑器配置动态以太网连接

运行以下命令使用互动编辑器配置动态以太网连接:
~]$ nmcli con edit type ethernet con-name ens3

===| nmcli interactive connection editor |===

Adding a new '802-3-ethernet' connection

Type 'help' or '?' for available commands.
Type 'describe [<setting>.<prop>]' for detailed property description.

You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, ipv4, ipv6, dcb
nmcli> describe ipv4.method

=== [method] ===
[NM property description]
IPv4 configuration method.  If 'auto' is specified then the appropriate automatic method (DHCP, PPP, etc) is used for the interface and most other properties can be left unset.  If 'link-local' is specified, then a link-local address in the 169.254/16 range will be assigned to the interface.  If 'manual' is specified, static IP addressing is used and at least one IP address must be given in the 'addresses' property.  If 'shared' is specified (indicating that this connection will provide network access to other computers) then the interface is assigned an address in the 10.42.x.1/24 range and a DHCP and forwarding DNS server are started, and the interface is NAT-ed to the current default network connection.  'disabled' means IPv4 will not be used on this connection.  This property must be set.

nmcli> set ipv4.method auto
nmcli> save
Saving the connection with 'autoconnect=yes'. That might result in an immediate activation of the connection.
Do you still want to save? [yes] yes
Connection 'ens3' (090b61f7-540f-4dd6-bf1f-a905831fc287) successfully saved.
nmcli> quit
~]$
默认动作是持久保存连接配置文件。需要时可使用 save temporary 命令,在下次重启前只将该配置文件保存在内存中。

添加静态以太网连接

要添加使用静态 IPv4 配置的以太网连接,可使用以下命令:
nmcli connection add type ethernet con-name connection-name ifname interface-name ip4 address gw4 address
可使用 ip6gw6 选项添加 IPv6 地址和网关信息。
例如:使用以下命令创建只使用 IPv4 地址和网关的静态以太网连接:
~]$ nmcli con add type ethernet con-name test-lab ifname ens9 ip4 10.10.10.10/24 \
gw4 10.10.10.254
还可为该设备同时指定 IPv6 地址和网关,如下:
~]$ nmcli con add type ethernet con-name test-lab ifname ens9 ip4 10.10.10.10/24 \
gw4 10.10.10.254 ip6 abbe::cafe gw6 2001:db8::1
Connection 'test-lab' (05abfd5e-324e-4461-844e-8501ba704773) successfully added.
NetworkManager 会将其内部参数 ipv4.method 设定为 manual,将 connection.autoconnect 设定为 yesNetworkManager 还会将设置写入 /etc/sysconfig/network-scripts/ifcfg-my-office 文件,其中会将对应 BOOTPROTO 设定为 none,并将 ONBOOT 设定为 yes
注:再次激活该接口前,NetworkManager 不会意识到对 ifcfg 文件的手动更改。有关使用配置文件的详情,请查看 第 1.9 节 “使用 sysconfig 文件进行网络配置”
使用以下命令设定两个 IPv4 DNS 服务器地址:
~]$ nmcli con mod test-lab ipv4.dns "8.8.8.8 8.8.4.4"
注:这样会替换之前设置的 DNS 服务器。要设置两个 IPv6 DNS 服务器地址,请运行以下命令:
~]$ nmcli con mod test-lab ipv6.dns "2001:4860:4860::8888 2001:4860:4860::8844"
注:这样会替换之前设置的 DNS 服务器。也可以使用 + 作为前缀,在之前的任意设置中添加额外 DNS 服务器,如下:
~]$ nmcli con mod test-lab +ipv4.dns "8.8.8.8 8.8.4.4"
~]$ nmcli con mod test-lab +ipv6.dns "2001:4860:4860::8888 2001:4860:4860::8844"
请使用以下命令激活新的以太网连接:
~]$ nmcli con up test-lab ifname ens9
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
检查这些设备及连接的状态:
~]$ nmcli device status
DEVICE  TYPE      STATE      CONNECTION
ens3    ethernet  connected  my-office
ens9    ethernet  connected  test-lab
lo      loopback  unmanaged  --
请使用以下命令查看新配置的连接详情:
~]$ nmcli -p con show test-lab
===============================================================================
                     Connection profile details (test-lab)
===============================================================================
connection.id:                          test-lab
connection.uuid:                        05abfd5e-324e-4461-844e-8501ba704773
connection.interface-name:              ens9
connection.type:                        802-3-ethernet
connection.autoconnect:                 yes
connection.timestamp:                   1410428968
connection.read-only:                   no
connection.permissions:                 
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.secondaries:                 
connection.gateway-ping-timeout:        0[output truncated]
使用 -p, --pretty 选项在输出结果中添加标题和分段。

例 2.2. 使用互动编辑器配置静态以太网连接

请运行以下命令,使用互动编辑器配置静态以太网连接:
~]$ nmcli con edit type ethernet con-name ens3

===| nmcli interactive connection editor |===

Adding a new '802-3-ethernet' connection

Type 'help' or '?' for available commands.
Type 'describe [>setting<.>prop<]' for detailed property description.

You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, ipv4, ipv6, dcb
nmcli> set ipv4.addresses 192.168.122.88/24
Do you also want to set 'ipv4.method' to 'manual'? [yes]: yes
nmcli>
nmcli> save temporary
Saving the connection with 'autoconnect=yes'. That might result in an immediate activation of the connection.
Do you still want to save? [yes] no
nmcli> save
Saving the connection with 'autoconnect=yes'. That might result in an immediate activation of the connection.
Do you still want to save? [yes] yes
Connection 'ens3' (704a5666-8cbd-4d89-b5f9-fa65a3dbc916) successfully saved.
nmcli> quit
~]$
默认动作是持久保存连接配置文件。需要时可使用 save temporary 命令,在下次重启前只将该配置文件保存在内存中。

将配置文件锁定至具体设备

要将配置文件锁定至某个具体接口设备,请在以上示例使用的命令中包含接口名称。例如:
nmcli connection add type ethernet con-name connection-name ifname interface-name
要让某个配置文件在所有兼容以太网设备中都无法使用,请使用以下命令:
nmcli connection add type ethernet con-name connection-name ifname "*"
注:即使不想设置具体接口,也必须使用 ifname 参数。使用通配符 * 指定可在任意兼容设备中使用的配置文件。
要将配置文件锁定至某个具体 MAC 地址,请使用以下命令:
nmcli connection add type ethernet con-name "connection-name" ifname "*" mac 00:00:5E:00:53:00

添加 Wi-Fi 连接

请使用以下命令查看可用 Wi-Fi 访问点:
~]$ nmcli dev wifi list
  SSID            MODE  CHAN  RATE     SIGNAL  BARS  SECURITY
  FedoraTest     Infra  11    54 MB/s  98      ▂▄▆█  WPA1
  Red Hat Guest  Infra  6     54 MB/s  97      ▂▄▆█  WPA2
  Red Hat        Infra  6     54 MB/s  77      ▂▄▆_  WPA2 802.1X
* Red Hat        Infra  40    54 MB/s  66      ▂▄▆_  WPA2 802.1X
  VoIP           Infra  1     54 MB/s  32      ▂▄__  WEP
  MyCafe         Infra  11    54 MB/s  39      ▂▄__  WPA2
请使用以下命令生成使用静态 IP 配置,但允许自动 DNS 地址分配的 Wi-Fi 连接:
~]$ nmcli con add con-name MyCafe ifname wlan0 type wifi ssid MyCafe \
ip4 192.168.100.101/24 gw4 192.168.100.1
请使用以下命令设定 WPA2 密码,例如 caffeine”:
~]$ nmcli con modify MyCafe wifi-sec.key-mgmt wpa-psk
~]$ nmcli con modify MyCafe wifi-sec.psk caffeine
有关密码安全的详情,请查看《Red Hat Enterprise Linux 7 安全指南》
请使用以下命令更改 Wi-Fi 状态:
~]$ nmcli radio wifi [on | off ]

更改具体属性

请使用以下命令检查具体属性,比如 mtu
~]$ nmcli connection show id 'MyCafe' | grep mtu
802-11-wireless.mtu:                     auto
请使用以下命令更改设置的属性:
~]$ nmcli connection modify id 'MyCafe' 802-11-wireless.mtu 1350
请使用以下命令确认更改:
~]$ nmcli connection show id 'MyCafe' | grep mtu
802-11-wireless.mtu:                     1350
注:NetworkManager 在设置中将 802-3-ethernet802-11-wireless 视为参数,将 mtu 视为属性。有关属性及其设置的详情,请查看 nm-settings(5) man page。

2.3.3. 使用 nmcli 配置静态路由

要使用 nmcli 工具配置静态路由,则必须使用命令后或者互动式编辑器。

例 2.3. 使用 nmcli 配置静态路由

请使用命令行为现有以太网连接配置静态路由,输入如下命令:
~]# nmcli connection modify eth0 +ipv4.routes "192.168.122.0/24 10.10.10.1"
这样会将 192.168.122.0/24 子网的流量指向位于 10.10.10.1 的网关

例 2.4. 使用 nmcli 编辑器配置静态路由

请使用以下命令使用互动式编辑器配置静态路由:
~]$ nmcli con edit type ethernet con-name ens3

===| nmcli interactive connection editor |===

Adding a new '802-3-ethernet' connection

Type 'help' or '?' for available commands.
Type 'describe [>setting<.>prop<]' for detailed property description.

You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, ipv4, ipv6, dcb
nmcli> set ipv4.routes 192.168.122.0/24 10.10.10.1
nmcli>
nmcli> save persistent
Saving the connection with 'autoconnect=yes'. That might result in an immediate activation of the connection.
Do you still want to save? [yes] yes
Connection 'ens3' (704a5666-8cbd-4d89-b5f9-fa65a3dbc916) successfully saved.
nmcli> quit
~]$
posted on 2021-03-03 16:23  陌鉎こ城sHi  阅读(405)  评论(0编辑  收藏  举报