3.3.7. 使用 nmcli 添加和配置动态以太网连接
添加动态以太网连接
使用动态
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.
打开以太网连接:
~]$ 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
要将主机发送的
IPv4
客户端 ID 更改为 DHCP
服务器,请修改 dhcp-client-id
属性:
~]$ nmcli con modify my-office my-office ipv4.dhcp-client-id client-ID-string
IPv6
没有 dhcp-client-id
属性,dhclient 为 IPv6
创建标识符。详情请查看 thedhclient(8)
手册页。
要忽略
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。
例 3.9. 使用交互编辑器配置动态以太网连接
使用互动编辑器配置动态以太网连接:
~]$ 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 临时命令,配置集只能保存在内存中,直到下次重启为止。