How to change the DNS servers in Ubuntu

You might want to change the DNS server if your ISP’s servers can sometimes be slow or outdated.

sudo gedit /etc/resolv.conf

and change or add the lines:

nameserver your_DNS1
nameserver your_DNS2

You can add as many lines like these, but two should be enough.
Test the used domain name servers careful!

#dns-nameserver 192.168.0.1
#dns-nameservers 192.168.0.1 192.168.0.2
#dns-search somedomain.org
#dns-domain 192.168.0.1

Edit: Serge’s comment (the first one) made me search a little bit.
After searching google it wasn’t totally clear if the DNS should be configured in resolv.cof or /etc/network/interfaces, so I took the GUI for a spin.
I’ve set up the DNS trough the GUI and checked the changes inside both locations.
The DNS servers I addes, became listed inside the /etc/resolv.conf file.
So after all, I was right.
But as the configuration file starts with the line #Generated by NetworkManager, I was wondering if these settings would get overwritten by the NetworkManager.
So I wiped all configured interfaces from my computer and let the NetworkManager try its evil.
It worked like a charm and just added the line ‘domain morraye.local’ to the file, without erasing my dns settings.
This was tested on Ubuntu 9.04 and Ubuntu 9.10.

You might also want to play with these settings in your resolv.conf file

# dns-domain 208.67.222.222
# dns-nameserver 208.67.222.222
# dns-nameservers 208.67.222.222 208.67.220.220
# dns-search wikipedia.org

Note: When you’re behind a router, you should change the dns server at your router (unless it uses pass-trough DNS lookups)

14 thoughts on “How to change the DNS servers in Ubuntu”

  1. That would be nice on most server setups, but on a desktop you’ll read this in /etc/resolv.conf :

    # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
    # DO NOT EDIT THIS FILE BY HAND — YOUR CHANGES WILL BE OVERWRITTEN

    You’ll better config this through Networkmanager or in /etc/network/interfaces

  2. You’re right.
    I almost forgot I was working on the server. :(
    In fact, on my desktop, I read:
    # Generated by NetworkManager
    (both on Ubuntu 9.04 and 9.10)

    * edited post to update with my findings ;)

  3. You need to set your DNS servers in /etc/dhcpd3/dhclient.conf to have them supersede those provided by DHCP configuration. NetworkManager overwrites /etc/resolv.conf every few minutes.

  4. I can’t believe that a 2009 OS makes it so difficult to change DNS. I have an unworkable Ubuntu 09.10 because the DNS needs changing and I cannot do this. Windows? Piece of cake to do it.

  5. @PeterC You should better read the post and the comments, and unless you grasp the whole contet, don’t jump to such a wrong conclusion.

    The original post and comments describes how to *manually* change dns settings, under the hood, or on a server. The original mistake was that on a desktop environment, you have a proper GUI tool to do that, just like you would on Windows.

    Also, it’s not because something looks harder to do, that automatically means it is a less good way. User friendliness and intuitivity are subjective matters.

    Linux let’s you have more control when you want to, and that is often the primary reason why people like it.

  6. What an unhelpful post.

    By all means be critical of my comment on the difficulty of changing DNS in Ubuntu 9.10. But in doing so, say how to do it. So far as I can tell, there is no GUI in 9.10 for changing DNS, and I have changed DNS frequently in Windows and other Linux OS / earlier Ubuntu OS.

    Google the subject. Look at the results. Nowhere, that I can see, is there reference to a GUI: rather, there are all sorts of contortions in obscure parts of the OS config and other files.

    My point is that to a non-expert, the DNS cannot readily and simply be changed.

  7. Sorry, I didn’t know I was supposed to help you.

    You forget that “Linux” is much more than only a Desktop oriented system. If you google on this subjkect, you obviously will find much more references on manually changing dns, or on changing dns on other systems different tnan the one at your hand.

    Network-manager, which is the newest tool for managing network (and dns) on a Desktop nowadays, also is pretty new, and lotys of references on changing dns are not up to date or just describing something different than.

    Now, to get back to your problem, as said, yes, it can be difficult to find the right description when googling it, for aforementioned reasons. For those same reasons, this original blog post was giving bad information by the way. Yes that is a problem with Linux, things are not black and white. Linux gives you more control, but that comes at a price: it’s harder to master. You certainly have a point there, and I’m the first to say Desktop Linux has a stability problem (on all sorts of levels)

    But looking at my own 9.10 desktop right now, I go to the menu, System, Preferences, Network COnnections, and I get the GUI you failed to find. There’s also an icon in the notification area, and when plugging your ethernet cable, you see that icon move. Clicking on that icon cab get you to the same GUI. That GUI let’s you modify DNS.

    You can hardly say this is extremely hard to find, can you?

    (I’d even say that this GUI is many time easier than the network centre in Windows since Vista, but that’s off course subjective.)

  8. The reason why most of the Linux related help you’ll find googling is terminal based, is because it’s become somehow a trend.
    It’s not only handy to know where to fix stuff when you broke your system and/or don’t have a gui available.

    But most common things can be done trough the GUI, as Serge mentioned.

  9. Hi,

    I used http://www.dnsserverlist.org/ to get a list of dns servers. Then I edited the host.conf file adding the lines:

    nameserver 216.52.1.33
    nameserver 128.107.241.184

    When I try to connect to the internet I get the error:

    /etc/host.conf: line 4: bad command `nameserver 216.52.1.33′
    /etc/host.conf: line 5: bad command `nameserver 128.107.241.184′

    I’m on Ubuntu 9.10

  10. Luis,

    You edited the wrong file, I think. The nameserver stuff should go in “/etc/resolv.conf”. Not in the host.conf…

    X

  11. Luis,

    the /etc/host.conf file is not ment to place the nameservers.
    This file specifies how the nameresolving should happen and not where the servers are located.
    The keywords are order, trim, multi, nospoof, spoof and reorder.
    Mine, for example looks like this:

    # The "order" line is only used by old versions of the C library.
    order hosts,bind
    multi on

    As Bashibk pointed out,
    you should edit the /etc/resolv.conf file.

    This is mine:
    # Generated by NetworkManager
    domain morraye.local
    search morraye.local
    nameserver 192.168.1.1
    nameserver 208.67.220.220

    Note that the domain and search properties point out to morraye.local!
    This is not what you should use.
    It’s just my home domain (not the .local) that I’ve set up.
    The primary nameserver is pointing to 192.168.1.1 as this is my router that is caching the resolved DNS query’s.
    This is (for me) the quickest way to resolve, thus the quickest way to surf the internet ;)
    The second nameserver is one of the opendsn dns servers, but I’m going to change that soon.

    For more information about the /etc/resolv.conf file and all of its options, you can check the man-page on linux.die.net or the one by SUN found here.

  12. is the configuration permanently?
    because after restarting my pc, the configuration in resolv.conf always return to configuration in beginning. and i am always set it back.
    how to set for permanently?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>