Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Pasted below this question is a sample of a /etc/hosts file from a Linux (CentOS) and a Windows machine. The Linux file has two tabbed entries after the IP address (that is localhost.localdomain localhost) and Windows has only one. If I want to edit the hosts file in Windows to have the machine name (etest) instead of localhost, I simply replace the word localhost with the machine name I want. The machine need not be part of a domain.

In a Linux machine, the two entries localhost.localdomain and localhost seems to indicate that I will need the machine to be part of a domain. Is this true?

Can I simply edit both entries to etest so that it will read:

127.0.0.1       etest etest

or is it required that I substitute one entry with a domain name?

Additionally, please let me know what the second line of the /etc/hosts file on the Linux machine is for.

::1     localhost6.localdomain6 localhost6

hosts file on a Linux machine:

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1       localhost.localdomain localhost
::1     localhost6.localdomain6 localhost6

hosts file on a windows machine:

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
share|improve this question
    
I posted a similar question recently, but it did not attract much interest. unix.stackexchange.com/questions/11844/etc-hosts-for-debian. This one is better written, though. –  Faheem Mitha May 12 '11 at 15:29
add comment

2 Answers

up vote 6 down vote accepted

You always want the 127.0.0.1 address to resolve first to localhost. If there is a domain you can use that too, but then make sure localhost is listed second. If you want to add aliases for your machine that will lookup to the loopback address you can keep adding them as space separated values on that line. Specifying a domain here is optional, but don't remove "localhost" from the options.

share|improve this answer
4  
Also "::1" is the ipv6 equivalent of 127.0.0.1 (which is for ipv4). –  XQYZ May 12 '11 at 9:00
    
Does this mean that having a line like this instead of the one I posted with my question would be wiser? I have included a space separated alias (etest my hostname) leaving the localhost.localdomain localhost intact. If not, please post an example of what you suggest as the ideal entry to include my machine hostname and have localhost entries in the hosts file. –  Thomas May 15 '11 at 14:27
1  
127.0.0.1 localhost.localdomain localhost etest –  Thomas May 15 '11 at 14:27
1  
@Thomas: Your example is just as it should be. Go with that, and if you need to add more aliases for testing (for example with name virtual hosts in apache) you can keep appending them to the line. –  Caleb May 15 '11 at 20:01
add comment

The format of /etc/hosts on Linux and Windows is the same:

IP address        hostname [hostalias]...

where the brackets are the usual way of indicating that something is optional (don't actually type them) and the dots (...) mean there can be more than one.

You shouldn't have to make your host part of a domain. Try it and see. But it would be a good idea to use .localdomain if you don't have a real domain name. It can make host name resolution a little bit quicker due to the ndots option in /etc/resolv.conf.

Note that in this sense, domain means DNS domain (like google.com or stackexchange.com), not a Windows domain or anything like that.

The line starting with ::1 is for IPv6. ::1 is like 127.0.0.1 under the new addressing scheme. Run ifconfig lo and you should see it has two addresses. Note the entry starting with inet6.

$ ifconfig lo
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
...

See the hosts(5) man page for more details.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.