24

If I add a new DNS server using nmcli connection modify eth0 +ipv4.dns 8.8.8.8, I still have to update /etc/resolv.conf for the resolver to pick up the changes, right? So how do I force the update?

The only way I found was restarting the network service: /etc/init.d/network restart. Is there a less invasive way -- a nmcli command, perhaps?

4
  • What do you want the NetworkManager to update it with? May 8, 2015 at 9:25
  • Added some clarifications to the question.
    – Mihai
    May 8, 2015 at 9:28
  • Since my nmcli does not have this modify command, I can only guess :) Try: resolvconf -u May 8, 2015 at 9:36
  • @KonradGajewski If having resolconnf installed, it works. Thanks. May 11, 2022 at 11:31

7 Answers 7

23

based on http://www.certdepot.net/rhel7-configure-ipv4-addresses/

nmcli connection up eth0 

run after mod calls, worked for me.

P.S. you can use "nmcli -p connection show eth0" to see the configured vs active settings

2
  • 3
    Save your fingers! connection can be abbreviated, all the way to c. Sep 3, 2019 at 18:57
  • Interestingly, the help article by RH suggests nmcli c reload which didn't work for changing DNS entries.
    – Rots
    Oct 22, 2019 at 8:19
2

Add following to interface configuration file (/etc/sysconfig/network-scripts/ifcfg-ethX):

PEERDNS=yes
DNS1=1.2.3.4
DNS2=1.2.3.4
NM_CONTROLLED=yes

bring interface up:

ifup ethX
2

If nmcli is NOT available (for example when NetworkManager is not installed) there is another way (handy on older Linux 6 distros) to add and remove new settings at will from /etc/resolv.conf without restarting network and without manually editing /etc/resolv.conf file. The steps are:

  1. Create a virtual NIC following the steps as described here or any other reference on creating a virtual NIC (e.g. ifcfg-eth0:0).

    Note: I also change BOOTPROTO=none and NM_CONTROLLED=no in the ifcfg-eth0:0 file.

  1. Add whatever settings to the /etc/sysconfig/network-scripts/ifcfg-eth0:0 file are desired to apply to the /etc/resolv.conf file. Example:

DNS1="8.8.8.8 7.7.7.7"

DOMAIN="urdomain1.com urdomain2.com"

  1. The new settings in the ifcfg-eth0:0 can be "switched on" (added) in the /etc/resolv.conf by simply running the following command:

sudo ifup ifcfg-eth0:0

The new settings can be "switched off" (removed) by running:

sudo ifdown ifcfg-eth0:0

and this method should work on any distro whether it has nmcli or not. This is arguably a variation on Giomac's answer, but it has the additional advantage that this VNIC can be brought up and down without affecting anything except that it will update the /etc/resolv.conf in both (up and down) directions.

1

To do so, you will need to identify your network interface (i.e eth0)

# nmcli  conn
NAME  UUID                                  TYPE            DEVICE
eth0  74315f7c-1573-4150-9c0e-e06ad28cac95  802-3-ethernet  eth0
#

# nmcli conn edit eth0

===| nmcli interactive connection editor |===

Editing existing '802-3-ethernet' connection: 'eth0'

Type 'help' or '?' for available commands.
Type 'describe [<setting>.<prop>]' for detailed property description.

You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, ipv4, ipv6, dcb
nmcli> print ipv4.dns-search
ipv4.dns-search: mydomain.linux.com
nmcli>
nmcli> set ipv4.dns-search mylinuxdomain.com,anotherdomain.com
nmcli>
nmcli> save permanent

Then restart your network. I use nmcli to do so: 1. To bring down

# nmcli conn down eth0

2. To bring up

# nmcli conn up eth0
2
  • 5
    You do not need to bring the connection down. Just do a nmcli conn up eth0 to save /etc/resolv.conf.
    – MadMike
    Aug 29, 2019 at 7:02
  • 1
    Careful while executing "nmcli conn down eth0" , if you are using an azure vm then your connection to that machine will go off...
    – TechDog
    Oct 13, 2021 at 16:53
1
  • Show available device to configure: nmcli c s

    You will need the NAME or the UUID of that list. Let's assume the connection name is eth0.

  • Show the current IPv4 DNS settings of eth0: nmcli c s eth0 | grep ipv4.dns:

  • Set IPv4 DNS settings to Google DNS entries 8.8.8.8 and 8.8.4.4: sudo nmcli c m eth0 ipv4.dns "8.8.8.8 8.8.4.4"
  • Save changes to system files (like /etc/resolv.conf, etc.) with: sudo nmcli c up eth0

Bonus:

  • Do the whole thing to a remote server over ssh: ssh remote-server 'sudo nmcli c m eth0 ipv4.dns "8.8.8.8 8.8.4.4" && sudo nmcli c up eth0'
0

Searched quite some time for the correct answer, and found it in another stack overflow post (source). This annoying thing is a known systemd bug, so you need to recreate the symlink pointing to the correct file.

sudo rm -f /etc/resolv.conf
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
reboot

Before you do this, check if the file contains the correct DNS entries:

sudo cat /run/systemd/resolve/resolv.conf
0

RHEL 7

How to set DNS in CentOS/RHEL 7 & prevent NetworkManager from overwriting /etc/resolv.conf?

Source : [enter link description here][1]

Step 1

The NetworkManager configuration is located here: /etc/NetworkManager/NetworkManager.conf Open this file using vim or your favorite text editor.

Search for the [main] section in this file. It should look something like this:

... [main] #plugins=ifcfg-rh,ibft ... Add dns=none just after the [main] tag like this:

...
[main]
dns=none
#plugins=ifcfg-rh,ibft
...

Go ahead and save the file.

Step 2

Let’s restart the NetworkManager.service service so that it picks up the changes we made to the configuration.

sudo systemctl restart NetworkManager.service Note that the service name NetworkManager.service is case-sensitive.

Step 3

Now, let’s add our nameservers to /etc/resolv.conf

Open this file in you favorite text editor and specify the name servers as follows:

# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 8.8.4.4

RHEL 6

CentOS / RHEL : DNS servers in /etc/resolv.conf change after a reboot/network service restart. How to make them permanent

Source : [https://www.thegeekdiary.com/centos-rhel-dns-servers-in-etcresolv-conf-change-after-a-rebootnetwork-service-restart-how-to-make-them-permanent/][2]

You would face this issue after a reboot or a network service restart. This usually happens as the scripts /etc/sysconfig/network-scripts/ifup-post and /etc/sysconfig/network-scripts/ifdown-post checks for the parameters “RESOLV_MODS=no” or “PEERDNS=no” in the network interface configuration file such as /etc/sysconfig/network-scripts/ifcfg-*. If these either of these parameters are not present, it will replace the contents of /etc/resolv.conf with /etc/resolv.conf.save. By default, PEERDNS and RESOLV_MODS are null.

  1. The /etc/resolv.conf file will be overwritten if any network interfaces use DHCP for activation. To prevent this, ensure such interfaces have PEERDNS=no set in their ifcfg file, for example:

     #cat /etc/sysconfig/network-scripts/ifcfg-eth0
     TYPE=Ethernet
     DEVICE=eth0
     BOOTPROTO=dhcp
     PEERDNS=no
    
  2. The ifcfg-file directives DNS1 and DNS2 can also lead to modification of resolv.conf. To prevent this, either remove said directives or use chattr(1) to make resolv.conf immutable to changes, i.e.:

     #chattr +i /etc/resolv.conf
    

You must log in to answer this question.

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