How do I get resolvconf to regenerate resolv.conf after I change /etc/network/interfaces?

387,700

Solution 1

service networking restart is not always a reliable way of down-upping all interfaces.

The resolvconf -u command only updates resolv.conf from resolvconf's own database. You need to update the database.

To update the database you have to call resolvconf with the -a or -d option. That happens behind the scenes when you run ifup or ifdown. So, normally, as with any other change to /etc/network/interfaces, to activate changes to the dns-* options you have to ifdown the interface in question and ifup it again. Or you can reboot.

If you want to make changes to an interface without ifdownupping it (perhaps because you are administering the machine remotely and happen to be connected via that interface, natch) then you can achieve the same result by running resolvconf directly from the command line. This requires a bit more knowledge of resolvconf's semantics. Assume the relevant /e/n/i stanza is

iface IIII FFFF static
    address ...
    ...
    dns-nameservers X.X.X.X Y.Y.Y.Y
    dns-search SSSS

where FFFF is an address family ("inet" or "inet6").

To activate these dns-* options you run resolvconf as follows (yes, with newlines in the string piped to resolvconf).

echo "nameserver X.X.X.X
nameserver Y.Y.Y.Y
search SSSS" | sudo resolvconf -a IIII.FFFF

For the stanza given in the question this would be the following.

echo "nameserver 192.168.3.45
nameserver 192.168.8.10
search example.com" | sudo resolvconf -a eth0.inet

Consult the resolvconf(8) manual page and the resolvconf package README file (/usr/share/doc/resolvconf/README.gz) for more information.

Solution 2

Although the manpage isn't installed by default it's documented via the update scripts option, just run:

sudo resolvconf -u

Solution 3

For those of you managing your servers remotely you can:

  1. update the dns-nameservers line in /etc/network/interfaces
  2. # ifdown eth01; ifup eth01

Notice that this has to be on one line divided with ; (the linux command line separator). You should not even lose your current connection. The exception is making a typo in the interfaces file. If this happens ifup will fail and you will have to have physical access or another ethxx connection.

Solution 4

service resolvconf restart will regenerate /etc/resolv.conf file without much fuss.

Solution 5

Simple answer :

Just install resolvconf. apt install resolvconf

After that, ifup eth0 updates the dns in etc/resolv.conf, according to the dns-nameservers line in /etc/network/interfaces.

Share:
387,700

Related videos on Youtube

null
Author by

null

Updated on September 18, 2022

Comments

  • null
    null over 1 year

    After updating /etc/network/interfaces with something very similar to below, how do I get /etc/resolv.conf to update? I tried (as root) resolver -u, service networking restart but they didn't work. I also fixed the symlink for resolv.conf and tried resolver -u again. Finally in frustration I rebooted, which did fix the problem by rebuilding /etc/resolv.conf.

    Ubuntu 12.04, 64bit server, all the latest patches installed.

    Example /etc/network/interfaces:

    iface eth0 inet static
        address 192.168.3.3
        netmask 255.255.255.0
        gateway 192.168.3.1
        dns-search example.com
        dns-nameservers 192.168.3.45 192.168.8.10
    
  • Aki
    Aki over 10 years
    "Don't do "service networking restart"; that's not a reliable way of down-upping all interfaces." Works fine for me. Good to know the clean way of doing it though.
  • NGRhodes
    NGRhodes about 10 years
  • Robin Winslow
    Robin Winslow almost 10 years
    If you shouldn't do "service networking restart" can I suggest you don't put it in the first line of this question? When hurriedly looking for a solution, the first thing I did was copy and paste the first command I saw.
  • Pete
    Pete almost 10 years
    @RobinWinslow +1 for making my day
  • αғsнιη
    αғsнιη over 9 years
    Can you explain why this command should work?
  • heemayl
    heemayl almost 9 years
    This answer is from the person himself who has developed resolvconf :)
  • Tino
    Tino over 8 years
    This does not work correctly if you have more than one nameserver IP in the line dns-nameserver IP1 IP2. The line must then be repeated in resolv.conf, but isn't.
  • Pierre-Damien
    Pierre-Damien over 8 years
    To restart/reread /etc/network/interfaces, you can do that : ifdown eth0 && ifup eth0 ; it works even in a remote ssh session.
  • Tino
    Tino about 8 years
    does not work for me. The second nameserver (which is needed for dnsmasq) is ignored (for an network manager auto interface). So somewhere there are some bits missing what to do in this case.
  • Tino
    Tino about 8 years
    Sadly cannot use that. My interface is managed by network manager and DHCP, so adding it to /etc/network/interfaces is not a valid option in that case. (Ubuntu 14.04)
  • John
    John over 7 years
    If dhcp settings were used on an interface such as eth0 in /etc/network/interfaces, one could initiate dhcp client and re-request IP address by running dhclient -r eth0; dhclient eth0
  • Melebius
    Melebius over 7 years
    @Pierre-Damien Unless there is a mistake like in sudo ifdown eth0 && ifup eth0 (missing sudo on second command). Still there are more reliable ways.
  • Melebius
    Melebius over 7 years
    Moreover, /etc/network/interfaces uses dns-nameservers (notice the s at the end) while resolvconf uses nameserver. The command does not deal with it. And even if everything was correct, the pipeline sed | grep | sed definitely wouldn’t be the most efficient way.
  • Mobigital
    Mobigital over 6 years
    I get Failed to restart resolvconf.service: Unit resolvconf.service not found.
  • Jossef Harush Kadouri
    Jossef Harush Kadouri almost 6 years
    command completed however not regenerated /etc/resolv.conf on ubuntu server 14.04
  • mathtick
    mathtick over 5 years
    unknown interface. Everybody really knows how to obfuscate the details here ... I think we should aim to containerize all the commands. Full script for each, no prose.
  • Carlos F
    Carlos F over 4 years
    On Ubuntu 18.04, there was no fuss, like you said, but the file was not regenerated either... Be aware of this, make a backup before following instructions.
  • TonyG
    TonyG over 3 years
    For anyone looking at this, network-manager isn't installed "by default" either. So if you're going to install that you might as well install resolvconf too.
  • TonyG
    TonyG over 3 years
    Old answer, but just to confirm, this works in Ubuntu v20, no need to install other packages, just resolvconf.
  • Saeed
    Saeed over 2 years
    first of all make sure thaat apt install resolvconf
  • Admin
    Admin almost 2 years
    Failed to restart networking.service: Unit networking.service is masked.