35

I have the problem that my Ubuntu machine uses the wrong DNS server. For some reason the machine queries localhost for DNS information.

I have added the DNS server in the network settings GUI, but /etc/resolv.conf still contains 127.0.0.1 as the DNS server address. Now, I thought I could just edit the file by myself, but it explicitly says I should not edit the file by hand.

Now, since the network settings GUI didn't generate the file with the right settings, how do I generate a new resolv.conf file by myself?

2
  • 1
    If you're using Ubuntu Server 12.04 LTS, just do yourself a big favor and edit /etc/resolvconf/resolv.conf.d/head directly just as you want your legacy resolv.conf to look. I had nothing but resolver issues until I brute force'd it.
    – user139478
    Mar 11, 2013 at 18:08
  • askubuntu.com/questions/157154/… maybe a related question. Apr 26, 2013 at 16:29

4 Answers 4

40

What none of the answers posted so far addresses is the appearance that the questioner fails to understand that having nameserver 127.0.0.1 in /etc/resolv.conf is correct, assuming that a local nameserver is running. And in Ubuntu 12.04 Desktop there is, by default, a local nameserver running, namely, a dnsmasq process controlled by NetworkManager which listens on 127.0.0.1. In Ubuntu 12.10 the listen address has been changed to 127.0.1.1.

So the solution is not to make any changes to resolv.conf. It is correct, assuming that the questioner wants to use the local nameserver.

If name service is not working then the local nameserver is not being given correct forwarding addresses, or there is some other networking problem.

The questioner tried using the "network settings GUI" to "generate the file with the right settings" and this failed to work. I don't know exactly what this means, but here is the right way to enter nameserver addresses so that they end up in resolv.conf at the right time. I assume that the questioner is using NetworkManager and not ifup to configure interfaces.

Most often interfaces are configured using the DHCP protocol. In that case nothing needs to be configured on the local system. The DHCP server knows what nameserver address its clients should use and sends this information to the DHCP client, which sends it to NetworkManager, which sends it to resolvconf, which puts the information into resolv.conf. So in this case it is the DHCP server that may need to be configured.

If the network interface on the local machine is statically configured then the correct nameserver addresses have to be entered into NetworkManager at, for example, network indicator | Edit Connections... | Wireless | myconnection | Edit... | IPv4 Settings | Additional DNS servers.

It is possible that the local nameserver is not working properly. In that case the questioner should edit /etc/NetworkManager/NetworkManager.conf

sudo gedit /etc/NetworkManager/NetworkManager.conf

and comment out the line

dns=dnsmasq

in the "[main]" section. To comment out the line, put a # at the beginning of the line, then save the file. Then restart network-manager.

sudo restart network-manager

After this, non-local nameserver addresses will be entered into resolv.conf instead of the 127.* address.

If the questioner is using ifup rather than NetworkManager to configure network interfaces then jmartin2279's answer is correct: you have to add the nameserver addresses to /etc/network/interfaces in the way jmartin2279 described.

Contrary to what some other answers advise, in general you should not add nameserver, domain or search options to files in /etc/resolvconf/resolv.conf.d/. See my comments on those answers.

7
  • 1
    "The DHCP server knows what nameserver address its clients should use" — Except when you have no control over the DHCP server (think public WiFi hotspots) and want to override the nameserver being used... Setting the same configuration on a per-connection basis - over and over again for each WiFi hotspot you connect to - is not the solution. Jul 25, 2015 at 5:37
  • It seems I am needing to execute dhclient for the changes (tha nameserver lines) finally arrive to etc/resolv.conf . I am running a customized live of Xubuntu 14.04 (maybe that is the reason). Jul 26, 2015 at 16:11
  • What could the disadvantages of disabling DNSMasq be, jdthood? Of course, name resolution and internet seem to work OK after doing it. Jul 26, 2015 at 16:21
  • See bugs.launchpad.net/ubuntu/+source/dnsmasq/+bug/1003842, e.g., my comment #60.
    – jdthood
    Jul 27, 2015 at 6:03
  • My resolv.conf keeps changing to 127.0.0.1 despite my network manager having other namerservers + dnsmasq being commented out + rebooted + dnsmasq killed (as it still gets started) + network restarted :/
    – some user
    Dec 27, 2015 at 12:44
17

If you are using ifup to configure the interface statically then you can add it to the /etc/network/interfaces file.

Open a terminal and type:

sudo gedit /etc/network/interface*

You should see something like:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

Edit to:

auto eth0
iface eth1 inet static
address 192.168.1.10 
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8

using your own network information. this will allow you to set the dns. you can use multiple dns servers here:

dns-nameservers 8.8.8.8 8.8.4.4

After doing this, run

ifdown eth0
ifup eth0
3
  • If it's your case, this is the solution: "If you are using ifup to configure the interface statically ...." Oct 14, 2013 at 8:59
  • Did you even test this? ifdown: interface eth0 not configured
    – Cerin
    Apr 28, 2014 at 15:58
  • 3
    if you're connected trough ssh you can't use ifdown / ifup. instead use sudo /etc/init.d/networking restart
    – Nijboer IT
    May 28, 2014 at 13:47
11

In Ubuntu 12.04 the handling of resolv.conf changed.

If you want to add your own nameserver address(es) then edit the file /etc/resolvconf/resolv.conf.d/head

sudo nano /etc/resolvconf/resolv.conf.d/head

and add your nameserver there (eg: Google open dns)

nameserver 8.8.8.8

save the file and then run

sudo resolvconf -u

No reboot or anything else. Maybe you will need to restart network-manager

sudo service network-manager restart

Done.

You can test it with the command

nslookup www.google.com

The result must be similar to

Server: 8.8.8.8
Address: 8.8.8.8#53
5
  • 4
    Adding a "nameserver" line to /etc/resolvconf/resolv.conf.d/head may work as a quick hack but it is not the correct solution to any problem. Doing that makes the resolver use that nameserver address under all circumstances, whereas the nameservers to use depend upon the circumstances. For example, if you are connected to a LAN (perhaps via VPN) then you usually want to use the LAN nameservers in order to be able to resolve private names on the LAN. And in the present case the system appears to be running a local caching nameserver, but adding "nameserver 8.8.8.8" just bypasses it.
    – jdthood
    Oct 29, 2012 at 7:48
  • @jdthood - You pointed out a problem. Care to provide a solution?
    – TJ Biddle
    Sep 16, 2013 at 0:07
  • @TJ I already posted an answer to the main question ("How do I edit my resolv.conf file?") if that's what you mean.
    – jdthood
    Sep 16, 2013 at 11:02
  • 1
    Please don't recommend this. At the very top of the file, "DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN".
    – Cerin
    Apr 28, 2014 at 16:00
  • I'd like to point out that editing /etc/resolv.conf manually overwrites changes on reboot, however, editing /etc/resolvconf/resolv.conf.d/head manually does NOT overwrite changes on reboot however it likely does on re-installation of resolvconf which is likely why the warning is there.
    – Ken
    Oct 30, 2017 at 21:54
1

To add more entries to /etc/resolv.conf, create a /etc/resolvconf/resolv.conf.d/tail file and add them there.

But if /etc/resolv.conf contains 127.0.0.1, then adding entries to /etc/resolvconf/resolv.conf.d/tail will not change anything. You have to set your DNS server addresses statically in NetworkManager which will send them to dnsmasq which listens on 127.0.0.1.

The DNS configuration for a static interface should go as “dns-nameservers”, “dns-search” and “dns-domain” entries added to the appropriate iface stanza in /etc/network/interfaces

Read this for more information.

`Moreover:

You can install a gui tool named gnome-network-admin that will set your DNS. Try it :

sudo apt-get install gnome-network-admin
1
  • 3
    (1) No, do not add "nameserver" options to /etc/resolvconf/resolv.conf.d/tail. That is ineffective (because other "nameserver" options take precedence) and suboptimal (because it's static configuration whereas the configuration should reflect the network circumstances). (2) No, do not use gnome-network-admin. It does not work properly with resolvconf. See bug #60518 (bugs.launchpad.net/ubuntu/+source/gnome-system-tools/+bug/60518).
    – jdthood
    Oct 29, 2012 at 8:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .