大网数据广告活动
服务器运维问答

How to manage DNS in NetworkManager via console (nmcli)?

· 52CE 我爱测

I have CentOS 7.2 (guest in VirtualBox, vagrant box centos/7 , no GUI). I see there is a nameserver in file: $ cat /etc/resolv.conf # Generated by NetworkManager nameserver 10.0.2.3 But how to add or replace with new one? I have …

问题原文

原帖链接 · Kirby · CC BY-SA 4.0

I have CentOS 7.2 (guest in VirtualBox, vagrant box centos/7, no GUI).

I see there is a nameserver in file:

$ cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 10.0.2.3

But how to add or replace with new one?

I have done this manually directly in the network:

$ vi /etc/sysconfig/network-scripts/ifcfg-eth0
PEERDS=no
DNS1=8.8.4.4
DNS2=8.8.8.8

And it works.

But is there any way to do this through nmcli?

P.S. No nmtui installed (in a selected system).

已采纳解答

原帖链接 · lauc.exon.nod · CC BY-SA 4.0

Here is the command to modify an existing connection.

nmcli con mod "$connectionName" ipv4.dns "8.8.8.8 8.8.4.4"

connectionName can be found by command: nmcli con. In the question case, it will be "System eth0"

If you want to ignore automatically configured nameservers and search domains, ie the settings passed from DHCP.

nmcli con mod "$connectionName" ipv4.ignore-auto-dns yes

Finally, to enable the changes, bring the connection down then up:

nmcli con down "$connectionName"
nmcli con up "$connectionName"
restart the NetworkManager service (if you don't want be disconnected) :
service NetworkManager restart

Verify with cat /etc/resolv.conf. You should not edit /etc/resolv.conf manually as it is generated by NetworkManager service, it is likely to get overridden at any given time.

Useful nmcli manual

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。