「Linux」- 通过 NetworkManager 配置网络地址

问题描述

该笔记将记录:如何使用 NetworkManager 配置网络,以及常见问题处理。

解决方案

IPv4, DHCP

nmcli connection add \
    connection.type '802-3-ethernet' \
    connection.id 'conn-name' \
    connection.interface-name 'eth0' \
    ipv4.method auto

nmcli connection up 'conn-name'

IPv4, Static

nmcli connection add connection.type '802-3-ethernet' \
    connection.id 'conn-name' \
    connection.interface-name eth0 \
    connection.autoconnect yes \
    ipv4.method manual \
    ipv4.addresses "192.168.1.134/24" \
    ipv4.dns "192.168.1.10 192.168.1.11" \
    ipv4.gateway "192.168.1.5"

nmcli connection up 'conn-name'

添加(或删除)多个地址:

#### 添加

nmcli connection modify "connection-name" \
    ipv4.addresses '78.34.56.23/20, 78.34.56.24/20'                             # 设置多个地址

nmcli connection modify "connection-name" \
    +ipv4.addresses '78.34.56.23/20'                                            # 追加新的地址

nmcli connection up "connection-name"                                           # 应用配置(直接 UP 即可,无需 DOWN 操作)

ip address "<interface-name>"                                                   # 验证地址已绑定到网卡

#### 删除

nmcli connection modify "connection-name" \
    -ipv4.addresses '78.34.56.23/20'                                            # 删除特定地址

nmcli connection up "connection-name"                                           # 应用配置

IPv6, Static

# 配置网卡
nmcli connection modify "System eth0" ipv6.method manual ipv6.address 2001:DB8:0:23:8:800:200C:417A/64

# 激活网卡
nmcli connection up "System eth0"

常用参数配置

在开机时,自动连接网络

如果希望连接可以自动连接网络,那指定 connection.autoconnect yes 参数即可:

nmcli connection modify "eth0" connection.autoconnect TRUE

禁止添加默认路由

配置 ipv4.never-defaultyes 即可:

nmcli con modify "eth0" ipv4.never-default yes

常见错误汇总

Error: invalid or not allowed setting ’10’

问题描述:执行命令nmcli connection add时,产生如下错误:

Error: invalid or not allowed setting '10': '10' not among [connection, 802-3-ethernet (ethernet), 802-1x, dcb, ipv4, ipv6, proxy].

原因分析:通常不会遇到该错误。在我们的场景中,由于命令嵌套,引号在被 Shell 解析后丢失,而产生该错误:

nmcli connection add ipv4.dns "10.10.50.7 10.10.50.6"
===>>>
nmcli connection add ipv4.dns 10.10.50.7 10.10.50.6

# 被解析后的命令在语法上是错误的,因此导致执行失败。

Error: failed to remove a value from ipv4.addresses: the property doesn’t contain IP address ‘x.x.x.x’.

问题描述:使用 nmcli connection show ens33 能够看到具有 x.x.x.x 地址,但是在执行命令时,产生如下错误:

# nmcli connection modify ens33 -ipv4.addresses 'x.x.x.x'
Error: failed to remove a value from ipv4.address: the property doesn't contain IP address 'x.x.x.x/24'.

原因分析:在我们的场景中,由于 ipv4.method=auto 配置,导致虽然配置静态地址但是还是会发送 DHCP 请求,而 x.x.x.x 则是 DHCP 请求后分配的地址

解决办法:关闭 ipv4.method=auto 配置,改为手动:

nmcli connection modify ens33 ipv4.method manual

参考文献

Fedora Wiki/Networking/CLI
How to tell NetworkGateway to not use an Ethernet profile as default gateway
Linux Basics: Assign Multiple IP Addresses To Single Network Interface Card On CentOS 7
How can I (from CLI) assign multiple IP addresses to one interface?
How to update the gateway with nmcli
Bug 1482772 – Cannot specify multiple ip addresses with nmcli con add