Red Hat Training
A Red Hat training course is available for RHEL 8
2.4. 使用 nmstatectl 配置静态以太网连接
要使用 Nmstate API 配置以太网连接,请使用 nmstatectl
工具。
例如,以下步骤使用以下设置为 enp7s0
设备创建 NetworkManager 连接配置集:
-
静态 IPv4 地址 -
192.0.2.1
,子网掩码为/24
-
静态 IPv6 地址 -
2001:db8:1::1
,子网掩码为/64
-
IPv4 默认网关 -
192.0.2.254
-
IPv6 默认网关 -
2001:db8:1::fffe
-
IPv4 DNS 服务器 -
192.0.2.200
-
IPv6 DNS 服务器 -
2001:db8:1::ffbb
-
DNS 搜索域 -
example.com
nmstatectl
工具确保设置配置后,结果与配置文件匹配。如果有任何失败,nmstatectl
会自动回滚更改以避免系统处于不正确的状态。
该流程以 YAML 格式定义接口配置。或者,您也可以以 JSON 格式指定配置:
先决条件
- 服务器配置中有一个物理或者虚拟以太网设备。
-
nmstate
软件包已安装。
步骤
创建一个 YAML 文件,例如,包含以下内容的
~/create-ethernet-profile.yml
:--- interfaces: - name: enp7s0 type: ethernet state: up ipv4: enabled: true address: - ip: 192.0.2.1 prefix-length: 24 dhcp: false ipv6: enabled: true address: - ip: 2001:db8:1::1 prefix-length: 64 autoconf: false dhcp: false routes: config: - destination: 0.0.0.0/0 next-hop-address: 192.0.2.254 next-hop-interface: enp7s0 - destination: ::/0 next-hop-address: 2001:db8:1::fffe next-hop-interface: enp7s0 dns-resolver: config: search: - example.com server: - 192.0.2.200 - 2001:db8:1::ffbb
将设置应用到系统:
# nmstatectl apply ~/create-ethernet-profile.yml
验证步骤
显示设备和连接的状态:
# nmcli device status DEVICE TYPE STATE CONNECTION enp7s0 ethernet connected enp7s0
显示连接配置集的所有设置:
# nmcli connection show enp7s0 connection.id: enp7s0 connection.uuid: b6cdfa1c-e4ad-46e5-af8b-a75f06b79f76 connection.stable-id: -- connection.type: 802-3-ethernet connection.interface-name: enp7s0 ...
以 YAML 格式显示连接设置:
# nmstatectl show enp7s0
其他资源
-
nmstatectl(8)
手册页 -
/usr/share/doc/nmstate/examples/
目录