63

I've recently been "forced" to perform some sysadmin work, while this isn't something that I absolutely love doing I've been reading, experimenting and learning a lot.

There is one fundamental aspect of server configuration that I've not been able to grasp - hostnames.

In Ubuntu for instance, one should set the hostname like this (according to the Linode Library):

echo "plato" > /etc/hostname
hostname -F /etc/hostname

File: /etc/hosts

127.0.0.1        localhost.localdomain        localhost
12.34.56.78      plato.example.com            plato

I assume that plato is an arbitrary name and that plato.example.com is the FQDN.

Now my questions are:

  • Is it mandatory?
  • To what purpose?
  • Where is it needed / used?
  • Why can't I define "localhost" as the hostname for every machine?
  • Do I have to set up a DNS entry for the plato.example.com FQDN?
  • Should plato.example.com be used as the reverse DNS entry for my IP?

Also, are there any "best practices" for picking hostnames? I've seen people using Greek letters, planet names and even mythological figures... What happens when we run out of letters / planets?

I'm sorry if this is a dumb question but I've never been too enthusiastic with network configurations.

Alix Axel
  • 2,653
  • 6
  • 28
  • 28
  • 3
    I think Chris S is off base here. The question -- what is a system hostname and how does it relate to DNS -- is a valid one. – larsks Jan 28 '11 at 13:27
  • @Chris S: I get it, believe me. I tried reading it once but the whole thing stroked me as immensely confusing. I'm not aspiring to be a SA, just trying to understand the basics a little bit better. – Alix Axel Jan 28 '11 at 13:27
  • This should go to the community wiki – lynxman Jan 28 '11 at 14:36
  • 1
    @lynxman: I don't mind that, but why? – Alix Axel Jan 28 '11 at 15:15
  • 1
    Because it's very interesting :) – lynxman Jan 28 '11 at 15:41
  • Most important answer: 1. Some services rely on this name. Other useful answers: 2. Recognizing which system is being worked on when accessing remotely, e.g., when in SSH. 3. Also, some services use this name when producing logs so when checking the logs, it helps in recognizing which system produced which logs. – aderchox Nov 20 '21 at 21:55

7 Answers7

30

These days, a system may have multiple interfaces, each with multiple addresses, and each address may even have multiple DNS entries associated with it. So what does a "system hostname" even mean?

Many applications will use the system hostname as a default identifier when they communicate elsewhere. For example, if you're collecting syslog messages at a central server, the messages will all be tagged with the hostname of the originating system. In an ideal world you would probably ignore this (because you don't necessarily want to trust the client), but the default behavior -- if you named all your systems "localhost" -- would result in a bunch of log messages that you wouldn't be able to associate with a specific system.

As other folks have pointed out, the system hostname is also a useful identifier if you find yourself remotely accessing a number of system. If you've got five windows attached to a systems named "localhost" then you're going to have a hard time keeping them straight.

In a similar vein, we try to make the system hostname matches the hostname we use for administrative access to a system. This helps avoid confusion when referring to the system (in email, conversations, documentation, etc).

Regarding DNS:

You want to have proper forward and reverse DNS entries for your applications in order to avoid confusion. You need some forward entry (name -> ip address) for people to be able to access your application conveniently. Having the reverse entry match is useful for an number of reasons -- for example, it helps you correctly identify the application if you find the corresponding ip address in a log.

Note that here I'm talking about "applications" and not "systems", because -- particularly with web servers -- it's common to have multiple ip addresses on a system, associated with different hostnames and services.

Trying to maintain name to ip mappings in your /etc/hosts file quickly becomes difficult as you manage an increasing number of systems. It's very easy to for the local hosts file to fall out of sync with respect to DNS, potentially leading to confusion and in some cases malfunction (because something tries to bind to an ip address that no longer exists on the system, for example).

larsks
  • 41,276
  • 13
  • 117
  • 170
  • Thank you for your reply, I'm still confused if I should (or need to) maintain the /etc/hosts file instead of just relying on the DNS entries. – Alix Axel Jan 28 '11 at 14:04
  • 8
    I generally ignore `/etc/hosts` in favor of DNS. This reduces the number of places I neeed to maintain information about name -> address mapping. On the bulk of systems I maintain, `/etc/hosts` contains only the single entry for `localhost`. – larsks Jan 28 '11 at 14:17
  • When you deal with clusters - like ganeti or a Linux-HA/Corosync cluster - /etc/hosts is used to talk among the members of the cluster. It's bad for it to rely solely on DNS because your DNS service may be on those clusters and being migrated or offline during a node change, so /etc/hosts and a correct hostname will help you a great time. – coredump Jan 28 '11 at 17:32
  • so, regarding "unique", we are rather "trying" to be "unique" than "must" be unique, correct? Because there are so many server out there and they are all in a same scope, to be actual "unique" is impossible. Is this understanding correct? – shenkwen Apr 15 '17 at 11:48
11

You could set every hostname to "localhost", but it's very handy to have alix@plato ~ $ in your command prompt when you manage machines over ssh. Managing servers remotely could become very confusing if you don't.

Having a correct FQDN is important for when you host a web server, or a mail server. These kinds of server applications like to know "who" they are running on.

For choosing a good naming scheme, I refer you to this very popular question .

A FQDN becomes useful only when it is meaningful to another computer. There are three levels to this:

  • One computer on your local network has an entry in his hosts file that points to that machine
  • You have a DNS server running on your local network and every local computer that uses it as a DNS server now knows plato by name.
  • You register a domain name and now the whole world knows what machine the name plato.alixaxel.com points to.

When sending email or serving web pages to the outside world, the third one is the one you want to have. For most other cases, you can make do with a local DNS or even editing hosts files.

In that case, you can just make up a domain name (plato.alixnetwork could be fine as a FQDN) for use within your local network. The only added value of having the "alixnetwork" part (the domain name) is convenience when you have another local network you wish to distinguish it from.

Anthony Geoghegan
  • 2,800
  • 1
  • 23
  • 34
Kenny Rasschaert
  • 8,925
  • 3
  • 41
  • 58
  • Thanks kenny, I guess my main doubt is why is it important for a web/mail server to have a proper FQDN. Also, what constitutes a "proper" FQDN? Should I just prepend the hostname to the domain and point it to the public IP address? I am under the impression that I've to create A/AAAA DNS entries with the hostname, no? – Alix Axel Jan 28 '11 at 13:30
  • It depends on what you want to achieve. If you don't send emails or serve webpages to the world with plato, there really is no need to create a global DNS record for that machine. – Kenny Rasschaert Jan 28 '11 at 13:44
  • "You register a domain name and now the whole world knows what machine the name plato.alixaxel.com points to." - I don't understand this. Doesn't the whole world know what machine the name plato.alixaxel.com points to by checking "alixaxel.com"s DNS resolution? What does it have to do with setting a FQDN in a random machine? – shenkwen Apr 15 '17 at 11:54
9

A basic overview. Hostname are just pointers; you might assign one specific one to be the hostname referenced by the machine, but it could have multiple. Some services, notable mail and HTTP rely on domain names to know where services should be located and how to get to them.

A long time ago, all these names (which again, are just pointers to IP addresses) were kept track of in a file called hosts. As the system grew they were unable to keep the file synchronized across all the relevant computer participating in the various interconnected networks. So the DNS system was invented. When you do a name lookup, it still checks the hosts file first, then the DNS system. Windows may also check other systems like WINS or NetBIOS.

When you put an entry in a hosts file, you are not assigning it to the computer. Assigning a hostname as the one used by the computer is done in the configuration files (on *nix systems) and System Properties in Windows systems (Windows system can also have NIC specific suffixes).

Entries in the hosts file, like the DNS system, are just a mapping from a hostname to an IP address. In order to use the hostname 'localhost' (there's nothing special about it, it's a hostname like all the rest) it must be mapped to the loopback interface (so it will always point to the local computer). To ensure this works, all computers come with this default mapping in their hosts file, but it could potentially be removed, if you didn't want to be able to use that hostname.

Further, as others have noted, it's very useful to assign a hostname to a computer. When connected to the computer, you can have it display its hostname when you login, or as your prompt, or any number of other places. This makes identifying the computer you're connected to easier. If you setup that hostname in DNS or put it in all the hosts files, you will be able to connect to the computer by referencing its hostname instead of having to know its IP address all the time. (Even more useful if the computer is using DHCP, as the address could change. If the computer updates DNS then the DNS record would point to the new IP address; you could still connect without knowing the new IP address because you know the DNS name).

There are many other uses of both hosts and DNS, but I suspect you've got more questions than answers if you read all this.

Anthony Geoghegan
  • 2,800
  • 1
  • 23
  • 34
Chris S
  • 77,337
  • 11
  • 120
  • 212
  • 4
    +1 for taking the time to write up a good answer despite your initial feeling of frustration with the question. – Kenny Rasschaert Jan 28 '11 at 13:49
  • Awesome explanation! If my understanding is correct placing the FQDN in the `/etc/hosts` is not necessary, having the hostname in a DNS entry would achieve the same result, right? – Alix Axel Jan 28 '11 at 14:02
  • 2
    @Alix, correct, if the entry is in DNS (and DNS is working properly) then having it in the `hosts` file would be redundant. Also the entry in the `hosts` file would override DNS (useful for testing sometimes, annoying when you forget about it though). Note that this is all in regards to A and AAAA records, there are other types of records for hostnames, but I left them out for clarity. – Chris S Jan 28 '11 at 14:24
  • Thank you Chris, this makes everything much more clearer. – Alix Axel Jan 28 '11 at 14:35
5

Ever host should be given a meaningful name. The hostname can server multiple purposes:

1- It helps you recognizing on which you are currently working.

2- Using names configured in /etc/hosts and/or DNS records is easier than memorizing many IP addresses.

3- Localhost is a reserved name to refer to the current machine (address 127.0.0.1).

4- DNS records are useful to make your servers publicly accessible.

Choosing a suitable name for each server helps you much in your administration. Also, it helps your clients accessing your servers.

Khaled
  • 35,688
  • 8
  • 69
  • 98
2

Just as a side note: Properly working forward and reverse DNS resolution is the absolute cornerstone of every IT installation on this planet. Never ever underestimate the necessity of a well maintained DNS and proper hostname resolution!

pfo
  • 5,630
  • 23
  • 36
  • I can't understand why it is important. I have a linode server that hosts many websites. Not understanding the purpose of setting hostname, I just leave it as it is, which is "ubuntu". This server has been running for a couple of years and I never feel any inconvenience. I am not questioning your answer, I am just trying to understand why hostname is important. In my case, when would I wish that I had properly set a hostname? – shenkwen Apr 15 '17 at 12:03
  • 1
    @shenkwen - if you honestly only have one server, you can name it whatever your heart desires, and it obviously won't matter. As soon as the number of servers becomes greater than 1, you have new problems to manage, and proper naming is an important tool. – mfinni Dec 02 '17 at 00:23
2

Disclaimer: the main question is about Linux systems, so feel free to ignore this answer if you are not interested in the Windows side of the issue.

Anyway, in Windows systems, apart from all the points mentioned in other answers, the hostname is actually used by the O.S. itself, for networking and authentication purposes; specifically:

  • Each system, be it or not a domain member, is required to have an unique name in the same network (i.e. in the same IP subnet), otherwise a naming conflict will ensue and various network services (mainly file and print sharing) will not work.
  • All systems that are members of the same Active Directory domain are required to have an unique name, regardless of network boundaries.
  • In a domain environment, the hostname of a system acts as a security principal and can be used for remote authentication (just think about it as an user account for the machine); it can be assigned permissions and access rights and can be placed in groups for security purposes. This impacts all processes running on the system using the built-in LocalSystem and NetworkService user accounts, which can authenticate to other systems using the credentials of the system they are running on; this allows f.e. a process running as NetworkService on SystemA to access a shared folder on SystemB by granting permissions on the folder to the user account of SystemA.
Massimo
  • 68,714
  • 56
  • 196
  • 319
-1

Many sites and/or supposed 'admins' are now stating that the 'hostname' attribute contain the FQDN of the OS instance, which 'breaks' any resolution which appends the 'domainname' to it:

hostname
system1.domain1.org

ping hostname
- cannot resolve system1.domain1.org.domain1.org

zantar
  • 1