Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

3.3.8. 使用 nmcli 添加和配置静态以太网连接

添加静态以太网连接

要使用静态 IPv4 配置添加以太网连接:
nmcli connection add type ethernet con-name connection-name ifname interface-name ip4 address gw4 address
IPv6 地址和网关信息可使用 ip6 和gw6 选项添加
例如,创建仅使用 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.
设置两个 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 选项会在输出中添加一个标题横幅和部分中断。

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

使用互动编辑器配置静态以太网连接:
~]$ 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 临时命令,配置集只能保存在内存中,直到下次重启为止。
NetworkManager 会将其内部参数 connection.autoconnect 设为 yesNetworkManager 还会将设置写出到 /etc/sysconfig/network-scripts/ifcfg-my-office,其中对应的 BOOTPROTO 将设置为 none,ONBOOT 设为 yes
请注意,在接口下次启动之前,NetworkManager 不会注意到对 ifcfg 文件的手动更改。有关使用配置文件的更多信息,请参阅 第 2.7 节 “使用 NetworkManager 和 sysconfig 文件”第 3.5 节 “使用 ifcfg 文件配置 IP 网络”