Linux Hosting: What is the purpose of setting hostname/FQDN in hosts file?

Solution 1:

It's going to be rough for people to answer this question because it starts with a simpler premise and proceeds to go down a deep rabbit hole from there. Let's start from the beginning.

Host file vs. DNS

I don't think this needs much explanation, so I'll keep it brief. The purpose of the hosts file is to define host to IP address mappings that do not rely upon DNS. The most important of these for a server is the device's own name, because it's silly for a problem with your DNS server to prevent your device from being able to refer to itself by name.

So long as we're only using host files, we don't need to use domains at all. DNS isn't involved, so who cares? Unfortunately as our network grows, it becomes less sustainable to have each device independently tracking the name of all devices on our network. That leads us to using DNS, and introduces some new logistical hurdles.

Hostname vs. FQDN

Hostnames are device names, or node names if you prefer. It's a uniquely identifying name that is meaningful to the device owner, and not necessarily meaningful or even exposed to people consuming the services on the device. This is what Linode means when it says that the name doesn't need to have anything to do with the services that you're hosting.

For example, let's say you own a company named Contoso, and you operate a website called example.com. The website is hosted on six machines, named dalek01 through dalek06. All the outside world needs to know is that they can get the website they want if they plug www.example.com into their web browser. Overly inquisitive Time Lords need not concern themselves with the fact that their request for www.example.com was serviced by dalek03.

By itself, a hostname doesn't necessarily have anything to do with DNS at all. It's just the name of the device, and it doesn't even necessarily have a domain suffix associated with it. So far it's simple!

Now we make things a little trickier. Story time!

  • Your company, Contoso, owns many desktop machines that communicate with the dalek cluster over a private network.
  • When there is a problem on one of the servers, you need to log into the specific device with the problem. We can't just connect to www.example.com, because it's hosted by several different machines. It's a good thing we have a uniquely identifying name for the server with a problem!
  • Since we have a large network of devices, it's most common for us to want to manage it with DNS. This means that we need to stick the hostnames dalek01 through dalek06 into a DNS domain somewhere. Fortunately we own contoso.com (named after our company), completely separate from www.example.com, which might be one of our customers.
  • Because the internet doesn't need to know our internal IP addresses, we maintain a private DNS domain called corp.contoso.com. All of our desktop machines are configured with a DNS search suffix of corp.contoso.com. This means that if we create a DNS entry called dalek01.corp.contoso.com, anyone on our network can get to that machine simply by connecting to dalek01 with their SSH client. Convenient!
  • dalek01 knows that it's called dalek01 because we put it in the hosts file. We want it to know that it's also called dalek01.corp.contoso.com, but we don't want it to rely on DNS to know its own name. That would be silly. Therefore we define an alias for dalek01.corp.contoso.com in the hosts file on dalek01 so that it knows all of its names.
  • In the meantime people continue to use www.example.com, oblivious to the fact that you have six servers named dalek01 through dalek06, the fact that your company is named Contoso (your customers aside), or that to make things convenient for your employees you created DNS records for dalek01 through dalek06.corp.contoso.com.

Putting it all together

  • Your hostname is meaningful to you and the people who run the server, not necessarily the people who use its services.
  • The hostnames need not be stored in DNS (or have a DNS domain at all) unless you own multiple devices sharing a private network.
  • It is silly for a server to rely upon DNS to talk to itself.
  • Putting your hostnames in DNS creates a need to also define that FQDN in your host file, so that it doesn't rely on DNS to talk to itself using the FQDN. (which as stated, would be silly)

Hopefully this covers all the bases.