5

I have two network interfaces configured via DHCP. As a result, /etc/resolv.conf is populated with information coming from the DHCP server.

How can I edit this file?

I know that if I add prepend domain-name-servers 127.0.0.1 to /etc/dhcp/dhclient.conf I can obtain nameserver 127.0.0.1 as the first (and only) line of /etc/resolv.conf.

What if I want 127.0.0.1 and only one of the two nameserver addresses provided via DHCP?

jdthood
  • 12,287
  • 2
  • 48
  • 66
JustTrying
  • 337
  • 2
  • 5
  • 11

4 Answers4

9

In Ubuntu 12.04 and later, /etc/resolv.conf is dynamically generated by the resolvconf utility. (Actually, resolvconf generates /run/resolvconf/resolv.conf and /etc/resolv.conf is a symbolic link to that. That's the default configuration; it is also possible to run with a static file at /etc/resolv.conf but that is non-standard.) Nameserver information (nameserver addresses and search domain names) gets registered with resolvconf by interface configurers (ifup, NetworkManager, dhclient, etc.). On the basis of what has been registered, resolvconf generates an up-to-date resolv.conf file.

Therefore, you can't edit the resolv.conf file directly. If you want to control what ends up in resolv.conf you will have to configure the resolvconf utility. Please see the resolvconf documentation for more information.

The answer to the specific question "What if I want 127.0.0.1 and only one of the two nameserver addresses provided via DHCP?" is:

  • First, do not add prepend domain-name-servers 127.0.0.1 to /etc/dhcp/dhclient.conf. The correct protocol is for local nameservers to register their local listen address(es) with resolvconf when they are ready to provide local name service; when they do this there is no need for DHCP clients to do so too. Dnsmasq does the right thing by default. In the case of BIND 9, you have to set RESOLVCONF=yes in /etc/default/bind9 to cause it to register the address 127.0.0.1 with resolvconf.
  • Second, resolvconf by default truncates the list of nameservers after any loopback address such as 127.0.0.1. To disable this behavior, create a file /etc/default/resolvconf containing the line TRUNCATE_NAMESERVER_LIST_AFTER_LOOPBACK_ADDRESS=no.
  • Third, resolvconf by default truncates the list of nameservers after three items. There is no point in including more addresses because the glibc resolver ignores any addresses after the first three. To cause resolvconf to truncate the list after two addresses you have to edit the script /etc/resolvconf/update.d/libc to replace this line

    [ "$N" = 3 ] && return 0
    

by the following one.

    [ "$N" = 2 ] && return 0
jdthood
  • 12,287
  • 2
  • 48
  • 66
  • You says that it isn't necessary to add `prepend domain-name-servers 127.0.0.1` to `/etc/dhcp/dhclient.conf` because local nameservers themselves register their listen address(es) with resolvconf and there is no need for DHCP clients to do this. But if I don't add that line, `127.0.0.1` doesn't appear in `/etc/resolv.conf`, while I've a running BIND9 on my box and I want to use it for name resolution. – JustTrying Jan 11 '13 at 08:20
  • 1
    @JustTrying: Set `RESOLVCONF=yes` in `/etc/default/bind9` to cause BIND 9 `named` to register its local listen address `127.0.0.1` with resolvconf when it starts. I have updated the answer to include this information. – jdthood Jan 13 '13 at 13:13
3

It worked for my grandfather, it worked for my father and it works for me.

rm /etc/resolv.conf
vi /etc/resolv.conf

search yourdomain.com
nameserver 8.8.8.8
nameserver 8.8.4.4

EDIT:

rm removes the standard symbolic link.

vi creates an actual file in its place.

Mark
  • 103
  • 3
SamTzu
  • 91
  • 1
  • 8
  • Could you be so kind as to also explain why it works and what it does? (then leave a comment @Fabby and I'll come back and upvote!) – Fabby Sep 29 '15 at 23:22
  • I do not think that works if you aren't root or use `sudo ... `. Probably your grandfather or your father already know :) – girardengo Apr 01 '17 at 12:16
1

When I installed 12.04 this text helped me a lot: http://www.stgraber.org/2012/02/24/dns-in-ubuntu-12-04/

jdthood
  • 12,287
  • 2
  • 48
  • 66
user121006
  • 167
  • 3
  • 1
    Welcome to Ask Ubuntu! Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Aditya Jan 09 '13 at 18:01
  • While this entry does not answer the original question, the linked blog post by Ubuntu developer is extremely valuable and explains how to solve *any* challenge with resolv.conf – geekQ Feb 17 '17 at 09:27
0

For the record, the official resolvconf documentation is here http://anonscm.debian.org/gitweb/?p=resolvconf/resolvconf.git;a=blob;f=README;hb=HEAD

"Editing" is as simple as using the resolvconf command line like an api.

e.g.

echo nameserver 8.8.8.8 | resolvconf -a eth0.goog

Here the . is a separator and the part after the interface is the name of the config for that interface.

And if you want to remove this name server just name the interface and configuration and use -d to delete

resolvconf -d eth0.goog

In a server/cloud scenario, this is all you need. For mobile, you will want to refer to the documentation.