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?
based on http://www.certdepot.net/rhel7-configure-ipv4-addresses/
run after mod calls, worked for me.
P.S. you can use "nmcli -p connection show eth0" to see the configured vs active settings
Add following to interface configuration file (
/etc/sysconfig/network-scripts/ifcfg-ethX
):bring interface up:
To do so, you will need to identify your network interface (i.e eth0)
Then restart your network. I use nmcli to do so: 1. To bring down
2. To bring up
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:
sudo nmcli c m eth0 ipv4.dns "8.8.8.8 8.8.4.4"
sudo nmcli c up eth0
Bonus:
ssh remote-server 'sudo nmcli c m eth0 ipv4.dns "8.8.8.8 8.8.4.4" && sudo nmcli c up eth0'
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.
The new settings can be "switched off" (removed) by running:
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.