Editing dns-nameservers in /etc/network/interfaces without a restart [duplicate]

After editing dns-nameservers in /etc/network/interfaces, how do I tell resolvconf(8) to read the new value without restarting networking? (causing a service interruption)

The -u option doesn't seem to work, it just updates back to the same values.


Solution 1:

You are right, "resolvconf -u" does not suffice to activate the change you made. That command only updates resolv.conf from resolvconf's database whereas you need to update the database.

Assume the interface in question is eth0. Assume that in /etc/network/interfaces you have a stanza that looks like this.

iface eth0 inet static
    [...]
    dns-nameservers 1.1.1.1 2.2.2.2

Now you change the "dns-nameservers" line. To activate this change do (note the && avoid breaking a potentially open ssh connection)

ifdown eth0 && ifup eth0

or reboot.

Solution 2:

I bumped into this twice recently.

The first time, I did sudo ifdown eth0 which of course killed my ssh connection and left the machine ignoring its NIC. Ouch. I had to go in through the IPMI interface on the server to get control again.

The second time, I learned from my earlier mistakes and did sudo ifdown eth0 ; sudo ifup eth0. The ssh window died, of course, but the machine quickly responded to a new ssh connection and my DNS modifications were in effect. I did the same thing on a second server but this time I waited before typing anything into the ssh window. The window stayed up and the DNS changes had been applied. Awesome.

The critical thing was to use the shell's semicolon operator so that both commands would be on one line. This way, the command to restore the interface has already been entered by the time the interface goes down. I suppose I could have written a script and executed that, but this seemed easier.

UPDATE: There is another way to do this. You can also restart the Ubuntu networking service in one step: sudo /etc/init.d/networking restart or sudo service network-interface restart INTERFACE=eth0. Thanks JFA for the inspiration.

Solution 3:

Just went through this same issue; even a reboot would be lose the change on manually invoking the libc hook.

So the most stable way I've found is, after putting the desired content into /etc/network/interfaces, to edit /etc/resolvconf/resolv.conf.d/original to include the lines wanted, make sure tail (in that directory) is not present, cd /etc/resolvconf/resolv.conf.d and then invoke /etc/resolvconf/update.d/libc.

Note that if tail is present (by default, pointed to original then the contents derived from /etc/network/interfaces will be followed by the original setup too.

That these changes can most safely be applied by a reboot is, frankly, insane. The current system takes what used to be "edit this file, perhaps deploying from a configuration management system" and hides it behind multiple layers of abstraction and no clean way to invoke for maintenance outside the normal boot framework.