Understand hostname and /etc/hosts

78,771

Solution 1

  1. /etc/hostname contains name of the machine, as known to applications that run locally. /etc/hosts and DNS associate names with IP addresses. myname may be mapped to whichever IP address the machine can access itself, but mapping it to 127.0.0.1 is unæsthetic.
  2. Not /etc/hosts, but /bin/hostname serves another function with -f.
  3. Because /etc/hosts can override the common sense. Edit it with caution and don’t leave garbage there after temporary patches and experiments.
  4. Both styles (⋯.TLD. and ⋯.TLD) are acceptable. The former is unambiguous, whereas the latter is ubiquitous.
  5. Don’t know what dnsmydomain is, but unlikely. Seeking something in /etc/hosts isn’t technically “DNS”.
  6. Remember the point 1. Hostname – for applications that run locally; also for such peculiar network protocols as SMB (samba). FQDN (if not a fakery or placeholder like localhost.localdomain) – for accessing the machine from outside. FQDN must be resolvable where it is used.

Solution 2

/etc/hostname and /etc/hosts are simple, but also not so simple...

How to quickly setup Debian /etc/hostname and /etc/hosts

1) Edit /etc/hostname.

There are 2 valid ways to set this up.

Either make it:

  • machine, i.e. a simple, one word name for this processor.
    For example, 'joe' might be your self-chosen machine name.
    Note this will require an alias in /etc/hosts, further described below.

OR

  • machine.domain.net, i.e. a Fully Qualified Domain Name (FQDN).
    For example, 'joe.xyz.com'.
    (This is assuming you have a DNS Host 'A' record pointing from joe.xyz.com to your joe's IP address, i.e. at your machine named joe. Normally you set this record up where you purchased your domain name. More on that in a moment.)

Once /etc/hostname is set, then the next step is to get the commands hostname --all-fqdn and dnsdomainname to work properly, in that order, as follows:

2) Edit /etc/hosts.

In it have a line for this host's FQDN, e.g. 'machine.domain.net', prefixed with its this machine's IP address (possibly from a dynamic IP address server) like this:

#   IP           FQDN (CANONICAL_HOSTNAME)   ALIASES
#-------------- --------------------------- ------------------------
192.168.1.14    Joe.xyz.com                 joe
  • First, note that upper or lower case don't matter here. Tip: Having some caps on, however, allows us to notice where things come from in the next steps. So I have capitalized Joe.

  • Also note that for the simple hostname (i.e. /etc/hostname = 'machine') to correctly generate a FQDN, an alias named 'machine' must be present in /etc/hosts. That's why the alias joe is there. (BTW, it's ok to have that alias even if you're not using it.) Otherwise this alias isn't needed, nor used.

  • Also note that 'domain.net' must be resolvable, i.e. there must exist an A record for it in DNS. This is what is called 'resolvable' (i.e. by the DNS resolver).

3) Now install these new setting from /etc/hostname and /etc/hosts into the kernel, by running:

    $ /etc/init.d/hostname.sh       # Or by rebooting

4) Test hostname

$ hostname      # Reports the hostname previously read from in /etc/hostname.
joe

OR

$ hostname
joe.xyz.com

Tips:

  • Notice that this is not what /etc/hostname currently is, like if you edit it again right now, but rather what was read previously by the /etc/init.d/hostname.sh script, possibly as a result of a reboot.

  • Also notice that the 'j' is lower case, so this suggests it is coming from /etc/hostname, not /etc/hosts.

5) Test FQDN

$ hostname --all-fqdn   #reports the FQDN(s) matched in /etc/hosts.
Joe.xyz.com             #  It searches /etc/hosts for the joe in /etc/hostname.
                        #  Notice that the 'J' is uppercase.  
                        #     This suggest it came from /etc/hosts.

6) Test DNS domain name

$ dnsdomainname         #reports the computed DNS doman name    
xyz.com                 #  (i.e. the right most part of the FQDN; 
                        #    the part to the right of the first, left most, dot)
                        #  This is coming from Joe.xyz.com in /hosts, 
                        #    after 'Joe.' is removed.

Now this should make setting up a web and email server easier.

Solution 3

I absolutely hate when someone tells me to read the manual but in this case please do take a look at the man page for hostname and most if not all your questions should be answered. It is written much better than I could possibly explain.

To provide some assistance, some of the excerpts that hit on your questions are: - Your hostname is what you see in /etc/hosts. - The FQDN returned is based on what resolver returns, hence why you see the value in /etc/hosts being returned as resolver checks the hosts file first, it then does any necessary DNS queries.

The one that I do not believe is touched on in the hostname man page (based on my re-reading of the man page myself) is why the entries in the files do not end with a dot.

That convention is used when dealing with DNS records. The hostname and hosts files are not actually DNS records. If you were configuring these entries in bind to be resolved for DNS lookups then the use of a dot can come into play (check out this good resource: http://www.zytrax.com/books/dns/apa/dot.html).

Also, keep in mind that all these values are not broadcast from the system. So if you want another system to access based on the FQDN, it is based on the hosts file on that other system, or lacking hosts entries based on the DNS lookups that the external system does on the FQDN to allow it to resolve back to the system.

Solution 4

In answer to

What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?

Let me point out, first, that /etc/hosts is a static definition of name to address mapping for the host on which it is installed, and is ONLY used by that host to convert name to IP address.

So, there are 2 points of view to consider - the host 'myname' and everybody else.

For that 'myhost' host, either reference the current host using 'localhost' (which is defined in /etc/hosts as 127.0.0.1), or add 'myname' to the /etc/hosts line which defines localhost so that 'myname' always maps to, well, localhost. Like so:

127.0.0.1 localhost myname

Then it doesn't matter what IP address the INTERFACE gets given, you'll always have an address that works (an added bonus is that you don't need to hit the hardware to talk from 'myname' to 'myname').

Now, to allow OTHER hosts to find 'myhost' when 'myhost' has a dynamic IP address, you must use DNS, and said DNS server must handle DHCP-assigned mappings. (Well, ok, there ARE other solutions I can think of, but everything that comes to mind fits in the 'beyond silly' category, IMHO. Well, ok, there are things you can do to the host to have IT tell the DNS where it is, but that's a question worthy of a completely different question/answer stream here, IMHO)

Share:
78,771

Related videos on Youtube

Gradient
Author by

Gradient

Updated on September 18, 2022

Comments

  • Gradient
    Gradient almost 2 years

    In /etc/hostname, I have myname.

    In /etc/hosts, I have :

    127.0.0.1   localhost.localdomain   localhost myname
    ::1     localhost.localdomain   localhost myname
    

    I have a vague understanding of what really is a hostname and what it is used for.

    These more precise questions could help me to understand it better:

    1. What really is my hostname in the example above? myname or something else? If I had instead myname.domain.com in /etc/hostname, what would be my real hostname then?

    2. In the example, the command hostname returns myname, whereas the command hostname -f returns localhost.localdomain. I had in mind that /etc/hosts was only a mapping between names and ip addresses. It seems to serve another function here. What is a fqdn and what is it used for? How is it retrieved? Why is it localhost.localdomain?

    3. If I had instead myname.domain.com in /etc/hostname, hostname -f would return myname.domain.com. Why?

    4. Why aren't fqdn ending with a dot in these files?

    5. Are hostname -d and dnsmydomain equivalent?

    6. In what context can I use my hostname (myname) and when do I have to use my fqdn (localhost.localdomain)?

  • yorkshiredev
    yorkshiredev over 9 years
    Related, about the final dot in /etc/hosts: serverfault.com/questions/336056/setting-fqdn-hostname-and-d‌​ot
  • user156516
    user156516 about 6 years
    What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?