Sign up ×
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:       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.       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:
#          # source server
#              # x client host       localhost
share|improve this question
I posted a similar question recently, but it did not attract much interest. This one is better written, though. – Faheem Mitha May 12 '11 at 15:29

3 Answers 3

up vote 6 down vote accepted

You always want the 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
Also "::1" is the ipv6 equivalent of (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 localhost.localdomain localhost etest – Thomas May 15 '11 at 14:27
@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

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 or, not a Windows domain or anything like that.

The line starting with ::1 is for IPv6. ::1 is like 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:  Mask:
          inet6 addr: ::1/128 Scope:Host

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

share|improve this answer
(...) means there can be more than one: Are the extras delimited with spaces or commas or both? – Guy Jul 30 '14 at 16:55
spaces. I suppose to be more correct, it's hostname [hostalias[,hostalias]*]. – Mikel Jul 30 '14 at 19:29

I cannot speak to how Windows may differ from Linux, but the format of the local machine's definition affects the results you will obtain from the 'hostname' command.

The format I find works most consistently well is this: etest localhost

The important thing I have found is to have the FQDN first and the aliases, in any order, after it.

If you experiment with re-arranging the names after the IP address and then using the 'hostname -s' (short name) and 'hostname -f' (fully-qualified domain name or FQDN) commands you will see what I mean. It should look something like this:

$ hostname -s

$ hostname -f

'hostname' by itself should return whatever name you entered for the host in /etc/conf.d/hostname or /etc/hostname (location of the file varies by distribution, but should be found under /etc somewhere).

If you change the order of the names you may find that "hostname -f" gives you responses like "localhost" or "hostname: system error". The only arrangement I have found that works correctly is putting the FQDN first.

I always set the IPv6 local address line (::1) the same way, i.e.:

::1 etest localhost

I know some distributions set the IPv6 name to something like ip6-localhost. I don't really use IPv6 yet, so can't comment on what the best settings for this line would be. I can just say that in an IPv4 network it works to have both lines with the same names.

share|improve this answer

Your Answer


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.