hostname: unknown host — hostname not resolving error 

Recently, I encountered the following error message on one of our openSUSE boxes (version 10.2 — if you are not sure which distro version you are running, then see this short post: finding out Linux release version).

hostname: Unknown host

The above error message was printed every time I opened a new terminal, so I started to investigate. The default shell on that PC was tcsh, so I checked the .tcshrc configuration file to see if the hostname command was used there… and sure enough it was. It is quite likely that you have encountered this error for a different reason, however the following solution should still apply.

For the purposes of this article let us assume that the fully qualified domain name (FQDN) of the computer used throughout this discussion is pc000123.dsplabs.com.au. OK, lets have a closer look at the hostname utility. As explained in the man pages, the hostname command is used to show or set the system’s host name. Lets reproduce the above error by running the following three commands which return the name of the system, the alias name of the host and the fully qualified domain name (FQDN), respectively.

hostname
hostname -a
hostname -f # ©2007 dsplabs.com.au

Unfortunately, only the first command succeeds as is shown in the output below.

pc000123
hostname: Unknown host
hostname: Unknown host

How do we fix the problem? Well, a solution courtesy of Ubuntu Forums is quite simple. All that is needed is a small modification to the /etc/hosts file. Lets have a look at its content using the following command.

cat /etc/hosts # ©2007 dsplabs.com.au

On openSUSE the /etc/hosts looks as follows (it should be similar on other distros).

# hosts         This file describes a number of hostname-to-address
#               mappings for the TCP/IP subsystem.  It is mostly
#               used at boot time, when no name servers are running.
#               On small systems, this file can be used instead of a
#               "named" name server.
# Syntax:
#
# IP-Address  Full-Qualified-Hostname  Short-Hostname

127.0.0.1       localhost

# special IPv6 addresses
::1             localhost ipv6-localhost ipv6-loopback

fe00::0         ipv6-localnet

ff00::0         ipv6-mcastprefix
ff02::1         ipv6-allnodes
ff02::2         ipv6-allrouters
ff02::3         ipv6-allhosts

Note that in the above the hashes '#' denote a start of a comment line. Also note that for the purposes of this article we can safely ignore all the entries relating to IPv6 (i.e. the ones after the "# special IPv6 addresses" line). Great, that leaves only one relevant line to worry about! Namely, the one starting with the 127.0.0.1 IP address and containing the localhost alias. You will need root privileges to edit /etc/hosts. To fix the hostname: Unknown host error you will have to comment out the following line:

127.0.0.1       localhost

and add the this line:

127.0.0.1       localhost pc000123.domain.local pc000123

where pc000123 should represent your short hostname. Remember that the short hostname should have been correctly returned by the hostname command (first thing we run). Now, save changes and lets have a look at the /etc/hosts file again, mine now looks like this.

# hosts         This file describes a number of hostname-to-address
#               mappings for the TCP/IP subsystem.  It is mostly
#               used at boot time, when no name servers are running.
#               On small systems, this file can be used instead of a
#               "named" name server.
# Syntax:
#
# IP-Address  Full-Qualified-Hostname  Short-Hostname

#127.0.0.1      localhost
127.0.0.1       localhost pc000123.domain.local pc000123

# special IPv6 addresses
::1             localhost ipv6-localhost ipv6-loopback

fe00::0         ipv6-localnet

ff00::0         ipv6-mcastprefix
ff02::1         ipv6-allnodes
ff02::2         ipv6-allrouters
ff02::3         ipv6-allhosts

To make sure that the fix worked, rerun the following commands.

hostname
hostname -a
hostname -f # ©2007 dsplabs.com.au

There should be no errors — fingers crossed. I got the following output.

pc000123
pc000123
pc000123.dsplabs.com.au

Hope this helps!


Did you find the above information useful and interesting? If so, please support this site by using the blog directory links at the bottom of this page. Thanks for your support!

If you have any Linux related problems or questions then please feel free to post them on our Linux Forums: http://linux.dsplabs.com.au/forums.




VPS Hosting Referral Code DZZCC3

Add me to Technorati Favorites Vote for me on Blog Catalog

12 Responses to “hostname: unknown host — hostname not resolving error”

  1. Macandrew Says:

    Thanks, this helped clear up this issue for me w/Ubuntu after upgrade to 8.04.

  2. usability seeker Says:

    Hi,
    Thanks for your effort. I followed your instructions above after I have installed samba and start getting "Can not resolve host name" error. These instructions solved host name problem but did not name resolving problem . I may suggest to be more specific about are these steps:

    1- Are they for home networks or for a Pc connected to a domain server? In may case I am willing to set home network (Windows + Ubuntu).

    2- I could not set up wireless connection.

    3- samaba keep complaining about resolving host name.

    I found a link (http://www.linuxplanet.com/linuxplanet/tutorials/6495/2/) about setting Ubuntu - Windows network which solved these issues.

    Regards

  3. Raphael Kraus Says:

    Removing the localhost alias is a ******BAD******* idea!

    Instead, leave it there, and add the other aliases too

    E.g.

    127.0.0.1 localhost
    10.0.x.x myhost myhost.domain

    OR EVEN

    127.0.0.1 localhost myhost myhost.domain

    All the above are better as you /ALWAYS/ want to be able to reference localhost

  4. Kamil Says:

    Good point, thanks Raphael. I have updated the post accordingly.

  5. Debian Hosts Datei - Paules-PC-Forum.de Says:

    […] Linux Blog hostname: Unknown host — hostname not resolving error hostname -f returns "unknown host" - Ubuntu […]

  6. David Says:

    Hi There,

    Thanks for your great article. What is a Domain?. I have 2 computers at home and I want to share data between them. They both have fixed IP addresses, but I don't know what a domain is. Do I need one? Also, I found that I had to add the hostname entry at the BOTTOM of the /etc/hosts file for it to work. So my file looks like this:

    127.0.0.1 localhost
    127.0.1.1 david-desktop
    192.168.0.10 david-desktop
    # The following lines are desirable for IPv6 capable hosts
    ::1 ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts
    192.168.0.145 hp-laptop

  7. Kamil Says:

    Hi David,

    You do not need domains / domain names to do simple networking. You can just use ip addresses of your hosts, i.e. 192.168.0.10 and 192.168.0.145, or the corresponding aliases that you defined in your hosts file, i.e. david-desktop and hp-laptop, respectively. See http://elouai.com/hosts-linux.php for further information on hosts files. If you were running a website or some sort of internet service then you could register a domain name for your pc/server, f.e. www.david-desktop.com.

    To test if the network is working you would ping the host you wanted to access remotely, f.e. from hp-laptop you would run

    ping 192.168.0.10

    or

    ping david-desktop

    (as long as the hosts name on your laptop defines that alias). From there I would recommend using sshfs to do the file sharing. A good article to get you started is: http://www.linuxjournal.com/article/8904 .

    A domain can mean a few different things, take a look at: http://searchsoa.techtarget.com/sDefinition/0,,sid26_gci211987,00.html# for a few definitions and at http://en.wikipedia.org/wiki/Domain_name for a description of domain names.

    As an example, use the nslookup tool to resolve a domain name to an ip address or vice versa:

    kamil@m1730:~$ nslookup linux.dsplabs.com.au
    Name: linux.dsplabs.com.au
    Address: 203.28.48.8

    (also see man nslookup)

    Hope this helps, good luck!

  8. Felipe Says:

    THX! You really solved my problem explaining just not, what to do but explaining what and why to do.

  9. James Says:

    thanx for this.. im just a newbei to this.. so tnx alot

  10. Keelby Says:

    We are so thankful for you.

  11. Srini Says:

    Thx a lot

  12. Himanshu Says:

    Thanks for this article mate!!

Leave a Reply