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

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

And it works.

But is there any way to do this through nmcli?

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


Useful nmcli manual

here is the syntax to modify an existing connection.

nmcli con mod <connectionName> ipv4.dns ""

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

you should not really edit /etc/resolv.conf manually as it is generated by NetworkManager service.

  • Yeah, thx, found it already. A bit edited your answer... But how to replace exist nameserver? – Kirby Oct 22 '16 at 11:02
  • there is "+ipv4.dns" and "ipv4.dns" can be used, but second one (without "+") doesn't replace. – Kirby Oct 22 '16 at 11:04
  • 2
    I'd rather use nmtui or if possible the GUI version as well. There is no need to be a command line ninja, if you have a more convenient option which is doing the same thing. – lauc.exon.nod Oct 22 '16 at 13:08
  • 2
    @lauc.exon.nod Some options aren't available in nmtui, and you have to use nmcli to get at them. – Michael Hampton Oct 26 '16 at 1:27
  • Link is dead.... – ceving Jan 14 at 16:52

there is good TUI tool developed by red hat named nmtui that you really should try. it is pre-installed on various distros, nowadays, but if it is not on yours, try:

  sudo yum install networkmanager-tui

it uses a curses based text interface - accessible from the command line. nmcli is only especially necessary when writing scripts, and has larger room for error due to the larger variety of possible input.

  • I cannot use it until set up new dns, because there is no Internet connection. Anyway, thanks for this point. – Kirby Oct 22 '16 at 11:31
  • it is included in various distros, nowadays – infinite-etcetera Dec 7 '16 at 15:45
  • Possibly so. I just didn't have it my docker container. – Kirby Mar 19 '18 at 12:45

In addition to setting the ipv4.dns property described above...

To exclude the DHCP provided DNS servers...set the ipv4.ignore-auto-dns property to yes.

nmcli con mod <connectionName> ipv4.ignore-auto-dns yes

To enable the changes, bring the connection down then up:

nmcli con down <connectionName>
nmcli con up <connectionName>

Verify with cat /etc/resolv.conf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.