122

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
0

12 Answers 12

127

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.

9
  • "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.
    – Aki
    Oct 3, 2013 at 9:49
  • 13
    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. May 29, 2014 at 9:31
  • 5
    @RobinWinslow +1 for making my day
    – Pete
    Aug 5, 2014 at 16:51
  • 11
    This answer is from the person himself who has developed resolvconf :)
    – heemayl
    Jun 22, 2015 at 21:50
  • 2
    To restart/reread /etc/network/interfaces, you can do that : ifdown eth0 && ifup eth0 ; it works even in a remote ssh session. Nov 24, 2015 at 13:55
33

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

sudo resolvconf -u
3
9

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.

4

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

4
  • 7
    I get Failed to restart resolvconf.service: Unit resolvconf.service not found.
    – Mobigital
    Jan 23, 2018 at 18:07
  • 1
    command completed however not regenerated /etc/resolv.conf on ubuntu server 14.04 May 17, 2018 at 7:59
  • 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.
    – Carlos F
    Dec 11, 2019 at 10:32
  • first of all make sure thaat apt install resolvconf
    – Saeed
    Dec 18, 2021 at 3:18
3

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.

1
  • 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.
    – mathtick
    Nov 30, 2018 at 10:34
1

This answer is similar to the one above but uses the questions example configuration to answer the question. Plus this explains why both commands are necessary.

Edit /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-nameserver 192.168.3.45
    dns-nameserver 192.168.8.10

These changes will not take place unless you reboot or reload the configuration file:

In order to update the interfaces file live it is necessary to run the following command:

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

This allows an update to the interface without a reboot or reloading.

However, the above command changes will be lost after a reboot if the changes in the /etc/network/interfaces are not made.

By the way the last answer from BDenis in this list actually works in place of the last command by parsing the /etc/network/interfaces file and piping those lines into the command sudo resolvconf -a eth0 It's actually half the answer and a really good example of inline sed parsing. If you want to see how it does this just run the first part of the command and watch it parse the /etc/network/interfaces file and spit out all the necessary information you need to run the sudo resolvconf -a eth0 command:

sed 's/#.*$//' /etc/network/interfaces | grep dns- | sed 's/dns-//g'

Notice this would produce the same out put as the command:

echo "nameserver 192.168.3.45
nameserver 192.168.8.10
search example.com"

Provided that the /etc/network/interfaces file is configured with this information:

iface eth0 inet static
    address 192.168.3.3
    netmask 255.255.255.0
    gateway 192.168.3.1
    dns-search example.com
    dns-nameserver 192.168.3.45 
    dns-nameserver 192.168.8.10
1
  • 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)
    – Tino
    Feb 16, 2016 at 12:29
1

On 18.04, the following works reliably (run as root):

systemctl stop networking
ip address flush dev <device> # just to be safe
systemctl start networking

Important: Make sure you have package resolvconf installed.
It doesn't seem to be by default, and without it some (!) changes from /etc/network/interfaces are silently ignored (e.g. dns-*).

1

On desktop version of Ubuntu 18.04, where there's no service networking, and resolvconf isn't installed by default, I've succeeded in regenerating resolv.conf by restarting network-manager:

sudo service network-manager restart
1
  • 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
    Sep 30, 2020 at 23:14
0

This worked for me:

sed -re '/nameservers|dns-search/ !d' -e 's/dns-nameservers/nameserver/' -e 's/dns-search/search/' /etc/network/interfaces  | resolvconf -a eth0.inet && resolvconf -u

Adjust accordingly.

0

I discovered that if I got rid of the [network] and generateResolvConf = false from /etc/wsl.conf, shutdown, and restarted, then /etc/resolv.conf regenerated without having to do all the reinstall stuff.

0

My config files were perfect. I restarted all the necessary services. I told resolveconf to update.

None of this dynamically updated my resolve.conf and gave me access to my local domain's name servers until I performed a "sudo reboot".

On boot up, resolv.conf was updated and I could ping and get access to a domain name on my manually added dns-nameserver.

-3

Also you can try this command:

cat /etc/network/interfaces |sed 's/#.*$//'|grep dns-|sed 's/dns-//g'|sudo resolvconf -a eth0.inet
3
  • 4
    Can you explain why this command should work? Oct 9, 2014 at 13:08
  • 1
    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.
    – Tino
    Oct 30, 2015 at 15:09
  • 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.
    – Melebius
    Dec 19, 2016 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.