Ubuntu 12.04 - Can't Resolve Hostname

39,555

The problem is purely related to DNS. As there was no DNS nameserver entries in the /etc/resolv.conf file so the name resolution was failing while pinging by hostname to hosts outside your /etc/hosts entries.

In Ubuntu 12.04 the Network Manager package provides the network related functionality (rather than the old networking program), with the resolvconf (and dnsmasq to some extent) program providing the mass DNS functionality. But surprisingly in your case the resolvconf is not installed so we have to manually update the /etc/resolv.conf file.

So by running the command

echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf

we are basically setting the Google's free DNS server (8.8.8.8) as the nameserver.

This command will insert the text "nameserver 8.8.8.8" into the "/etc/resolv.conf" file and display the text on the screen too. In this way we have a working name resolver that will resolve the hostnames we give into IP addresses.

One very important thing to note here, we are using google's DNS which is not ideal, you should use your ISP's DNS here. Ask your ISP to give you their DNS address (can be multiple) and add the address as the nameserver. Although you can keep the Google's DNS as the backup in case your ISP's one fails for some reason. Let's assume that your ISP's DNS is vv.xx.yy.zz , so you need to run the following commands to make it as the primary DNS and keeping the Google's DNS as a backup.

echo "nameserver vv.xx.yy.zz" | sudo tee /etc/resolv.conf && echo -e "nameserver 8.8.8.8" "\nnameserver 8.8.4.4" | sudo tee -a /etc/resolv.conf

See the -a switch in tee command, that is used to append rather than overwrite. Here 8.8.4.4 is also Google's DNS.

You can add as many nameservers as you want in /etc/resolv.conf but that would be overkill. Just keep it simple yet compact.

Share:
39,555

Related videos on Youtube

Threshold
Author by

Threshold

Updated on September 18, 2022

Comments

  • Threshold
    Threshold over 1 year

    I have a small dedi server on which I have installed Ubuntu 12.04. I access it via x2go since I have a desktop installed.

    All worked fine until 2 days ago when, after a reboot, I lost any Internet access and started getting "Can't Resolve Hostname" errors.

    If I try to ping google I get:

    ping: unknown host google.com
    

    To access some sites I added them to the hosts file and it works for most of them but not all. Of course this is just a temporary solution.

    If I look into "System Settings - Network", I get this:

    I am not very competent, so I don't know what other info to post but please ask anything you wish me to find out.

    Thank you.

    • heemayl
      heemayl over 9 years
      Please check the output of ps -ef | grep -i "network" - this is to check if the networking process is running at all. If its not running then do sudo service networking start and see what happens afterwards.
    • Threshold
      Threshold over 9 years
      Hi heemayl thank you for your help, I executed both of your commands since I couldn't interpret the results of the first one. This is the result: prntscr.com/5eoi78 Thank you
    • heemayl
      heemayl over 9 years
      Did you change any network related settings recently? Give the output of ifconfig -a. Do you use static IP or DHCP?
    • Benjamin Maurer
      Benjamin Maurer over 9 years
      Could you please post the output of nm-tool. Also, can you ping 8.8.8.8 (Google DNS, to make sure your internet connection is working at all). I suspect some faulty DNS config...
    • Threshold
      Threshold over 9 years
      @Benjamin Maurer Hi Benjamin, this is the output: prntscr.com/5epvgv Thank you
    • Threshold
      Threshold over 9 years
      @heemayl Hi heemayl sorry I missed your reply. No I did not change anything due to my inexperience. I just do update\upgrade when I get notified. This is the result of ifconfig -a prntscr.com/5eqahu; prntscr.com/5eqbjr I am afraid I have no idea what IP I use but I guess static. Any way I can find out? Thank you
    • heemayl
      heemayl over 9 years
      @Threshold: No problem. From the output, you have a static global IP on eth0 interface. So thats one check comes out good. Now run the following commands one by one: echo "8.8.8.8" | sudo tee /etc/resolvconf/resolv.conf.d/head , sudo service resolvconf restart , sudo service network-manager restart and then try to ping any site by hostname. Let us know the result.
    • Threshold
      Threshold over 9 years
      @heemayl Ok good to know about my ip. No luck I'm afraid though. This is the output of echo "8.8.8.8" | sudo tee /etc/resolvconf/resolv.conf.d/head , sudo service resolvconf restart , sudo service network-manager restart : prntscr.com/5eqsbq P.s. Am I posting correctly? I'd like to add all the tests results to my main post but I don't have enough reputation to post images I am told.
    • heemayl
      heemayl over 9 years
      @Threshold: I am sorry. That was my fault, i was considering you have resolconf package maintaining your DNS. Now as it is not doing so, following two commands should do the job for you: echo 8.8.8.8 | sudo tee /etc/resolv.conf , sudo service network-manager restart. Then try to ping any host.
    • Threshold
      Threshold over 9 years
      @heemayl Unfortunately still no luck. This is the output of echo 8.8.8.8 | sudo tee /etc/resolv.conf , sudo service network-manager restart : prntscr.com/5er4k0 Searching for a solution I stumbled on this old thread, which seems related to my issue:ubuntuforums.org/showthread.php?t=2001421. What do you guys think? Thank you.
    • heemayl
      heemayl over 9 years
      I am sorry. There's a typo. Lets have one last try. Do this: echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf, Then try to ping. Btw i am looking into the link you gave.
    • Threshold
      Threshold over 9 years
      @heemayl Yay it worked, you are a genius! What exactly happened, do you have any idea? Thank you very much for your help. Thanks Benjamin too for posting. How do I pick your solution and mark this solved?
    • heemayl
      heemayl over 9 years
      @Threshold: I am glad that your problem is solved and that you are happy. Now run one last command and post the output dpkg --get-selections | grep "resolv" so that i can post the elaborated answer, then select the answer as the answer to your question.
    • Threshold
      Threshold over 9 years
      @heemayl Hi heemayl, I get no output from dpkg --get-selections | grep "resolv". It just goes to next line with the prompt. Do I need to sudo it maybe or it's expected? Thanks
    • heemayl
      heemayl over 9 years
      It was expected. Please check my answer to get details idea on this issue. Select it as the answer to your question so thats others can know that the problem has been resolved
    • Benjamin Maurer
      Benjamin Maurer over 9 years
      @Threshold you can accept an answer with the "checkmark" under the vote arrows. It is weird though, nthat you don't have resolvconf installed. Did you install from an Ubuntu Desktop image? Did you install it yourself?
  • Threshold
    Threshold over 9 years
    Thanks a lot for your help and detailed answer. I actually understood what we did, instead of just pasting code in terminal, and should be able to repeat it myself should the problem arise again. I found my isp dns range so will try to set it as the primary one.In the code you posted at one point you write "nnameserver": is this a typo like I think or intentional? Thank you again.
  • heemayl
    heemayl over 9 years
    No problem. Thats intentional. Let me break this up for you. "\nnameserver 8.8.4.4" has two portions: One is \n that merely means "new line" and the other is literal nameserver 8.8.4.4. Here tee command will see the first portion (\n) and create a new line and then the second portion will be put at the start of the new line. This is to comply with the configuration file format which suggests that we must put the entries separately in a line by line manner.
  • Threshold
    Threshold over 9 years
    Ah, got it. I will try using my isp's dns in the coming days and report the results. Thank you very much.