How do I force NetworkManager to update /etc/resolv.conf?
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?
Solution 1:
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
Solution 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
Solution 3:
-
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'
Solution 4:
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:
-
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.
- 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"
- 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.