「Linux」- 网络配置(CentOS 7.x NetworkManager) @20210329

该笔记记录:如何使用 NetworkManager 配置网络

有线网络(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'

有线网络(静态地址)

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'

常用参数配置

在开机时,自动连接网络

Fedora Wiki/Networking/CLI

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

nmcli connection modify "eth0" connection.autoconnect TRUE

禁止添加默认路由

How to tell NetworkGateway to not use an Ethernet profile as default gateway

设置 ipv4.never-default 属性为 yes 即可:

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

相关文章

「Linux」- 为网卡分配多个地址(CentOS 7.x)
「Linux」- 网络配置(CentOS 6.x)

posted @ 2021-03-29 13:05  研究林纳斯写的  阅读(833)  评论(0编辑  收藏  举报