61

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).

4 Answers 4

95

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

8
  • Yeah, thx, found it already. A bit edited your answer... But how to replace exist nameserver?
    – Kirby
    Oct 22, 2016 at 11:02
  • 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. Oct 22, 2016 at 13:08
  • 3
    @lauc.exon.nod Some options aren't available in nmtui, and you have to use nmcli to get at them. Oct 26, 2016 at 1:27
  • 1
    To remove the default ipv4 dns from a device, try "nmcli device mod <<device>> ipv4.ignore-auto-dns yes". Nov 25, 2020 at 11:57
  • 1
    Actually you should be able to do "nmcli c down <interface>; nmcli c up <interface>" on the same line with the semi-colon separator, it will work 99% of the time. The shell will run them in the same session. Obviously if you type them on different lines, the shell won't get that "up" command after downed the interface.
    – Dale
    Aug 25, 2023 at 16:08
13

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.

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

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

1
5

Just in case I have done a little script to do that automatically (here with google DNS) for every ethernet/wireless connections:

nmcli -g name,type connection  show  --active | awk -F: '/ethernet|wireless/ { print $1 }' | while read connection
do
  nmcli con mod "$connection" ipv6.ignore-auto-dns yes
  nmcli con mod "$connection" ipv4.ignore-auto-dns yes
  nmcli con mod "$connection" ipv4.dns "8.8.8.8 8.8.4.4"
  nmcli con down "$connection" && nmcli con up "$connection"
done

At the end, the wireless connections will be lost. You have to reconnect and voilà !

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .