Format of /etc/hosts on Linux (different from Windows?)

119,832

Solution 1

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.

Solution 2

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.

Solution 3

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:

127.0.0.1 etest.mydomain.com 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
etest

$ hostname -f
etest.mydomain.com

'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.mydomain.com 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:
119,832

Related videos on Youtube

Thomas
Author by

Thomas

Updated on September 18, 2022

Comments

  • Thomas
    Thomas almost 2 years

    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
    
  • UrBestFriend
    UrBestFriend about 13 years
    Also "::1" is the ipv6 equivalent of 127.0.0.1 (which is for ipv4).
  • Thomas
    Thomas about 13 years
    127.0.0.1 localhost.localdomain localhost etest
  • tcoolspy
    tcoolspy about 13 years
    @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.
  • Guy
    Guy almost 10 years
    (...) means there can be more than one: Are the extras delimited with spaces or commas or both?
  • Mikel
    Mikel almost 10 years
    spaces. I suppose to be more correct, it's hostname [hostalias[,hostalias]*].
  • Lambert
    Lambert over 6 years
    Please clarify your answer and use proper formatting. You don't need to put your name in your answer.