Why do I need to set a hostname?

5,236

Solution 1

Certain applications will use the hostname for certain parameters unless explicitly set. Postfix, for example, will identify itself using the hostname of the machine unless you specify otherwise in the config file.

The hosts file is used for name resolution. When resolving domain names, your server will check its hosts file before making a DNS request.

That line you posted will essentially make your server resolve "plato.example.com" and "plato" to that IP address. Thats why you'll generally see the first line with 127.0.0.1 localhost localhost.localdomain .... so the server will always resolve its hostname to itself.

Solution 2

This makes the system able to resolve it's own name even if DNS is offline. A long time ago there was no DNS, everyone shared hosts files with the name/IP pairs for every machine they needed to connect to in them.

If you look at /etc/nsswitch.conf you'll see that for hosts it (by default anyways) has a line that says hosts: files dns

This means that first it will check the hosts file and if it does not find the name there it will go to DNS. This can save you a lot of network traffic if you have an app that looks up the local hosts's name or IP frequently (well, and you are not caching name lookups which now you normally do).

Share:
5,236

Related videos on Youtube

gerky
Author by

gerky

Updated on September 18, 2022

Comments

  • gerky
    gerky over 1 year

    I know there's quite a few questions about host names. But even after reading them, I didn't really understand the concept of host names entirely. So here's my question:

    I've been following this guide in setting up a VPS with Linode.

    The first step is to set a host name. From what I understand, a host name is just an arbitrary name you can set to identify your machine in a network. Also, the FQDN is the host name plus the domain name (which can or can not be related to web domains hosted on the server). Please correct me if I'm wrong.

    Then it instructs me to modify /etc/hosts and add in something like:

    12.34.56.78    plato.example.com    plato
    

    So my question is, what exactly does this line accomplish? I've done it before but never really understood what it did. Also, if the host name and the domain name used in the FQDN is just arbitrary, where can they be used? Actual use cases would be very helpful and detailed explanation would be great. Thanks!

  • gerky
    gerky over 10 years
    Thanks, I think I'm starting to get it. But regarding the host name and the domain name being arbitrary, do they only mean something within the local network? How do they compare to top level domains like .com?
  • Safado
    Safado over 10 years
    Generally yes, but it can also depend on the service that the server is running. In most cases though, it's just a way for you to identify what server it is. And it doesn't necessarily have to match a public domain name that belongs to it. For example, you can have a webserver that responds to 5 different domain names, yet the hostname of the server is webserver001. OR you could set the hostname of the server to be the same as one of the domains it responds to through the httpd server. Either way, as long as when you see the hostname, you know what server is being referenced.
  • Safado
    Safado over 10 years
    Try these two Related links from the right side of the page. Setting the hostname and Hostnames - What are they all about