6

I was recently setting up an MTA on a VPS, and while configuring it I realized I’m quite confused about the concept and implementation of hostnames in Unix.

I have a solid understanding of the DNS, and I’m familiar with its predecessor, the hosts file, but I have no experience with classic, internal networks, as I have always worked with servers directly connected to the Internet.

There are a lot of contradictory explanations out there if one searches the subject, so, first of all, I would like to ask for a thorough introduction to hostnames (not just simple, but complex, multi-level names), completely independent of the DNS and the Internet, and then for an explanation of their relationship to the DNS, and the perplexing “fully qualified domain name” term (is this the same as in DNS, or something else).

I would also like to know which files and commands on a typical Unix box (or at least on Debian/Ubuntu) are playing a role in managing hostnames, especially:

  • the role and correct setup of /etc/hostname (simple or complex name? is this something with an A record?)
  • the role and correct setup of /etc/hosts in regard to setting the hostname (is it not just for resolving? what names and where to point?)
  • any other files
  • how to apply changes to the hostname without restart?

More detailed answers are preferred over ones that try to simplify the subject, since there’s a lot out there from the latter type, which is the source of the confusion.

slm
  • 363,520
  • 117
  • 767
  • 871
Joó Ádám
  • 273
  • 1
  • 3
  • 11
  • 1
    `/etc/hosts` is not involved in setting your hostname, but it may set internal aliases for it. – goldilocks Nov 02 '14 at 15:05
  • A lot of material out there would disagree with you. E.g. see this guide from Linode: https://www.linode.com/docs/getting-started#setting-the-hostname Also, Postfix, for example, uses the original hostname in its logs if I don’t set a hostname in `/etc/hosts`, only in `/etc/hostname`. – Joó Ádám Nov 02 '14 at 15:20
  • 1
    Nope, that's not what is going on in that link WRT `/etc/hosts`. That's setting aliases to the hostname, as per my first comment. `plato.example.com` *must already be the hostname* in order for this to work; then you can use `plato` as an alias locally. – goldilocks Nov 02 '14 at 15:29
  • 1
    `/etc/hosts` doesn't have to do with your hostname, it is used for the resolving of DNS lookups. I think you're mixing things up here. The hostname is specifically configured through a variety of mechanisms (typically distro specific) that ultimately set it within the kernel. DNS on the other hand is a service that various tools can utilize to resolve lookups of IP->hostname or hostname->IP. – slm Nov 02 '14 at 15:31
  • 1
    See also `man 5 hosts` and `man hostname`. – goldilocks Nov 02 '14 at 15:31
  • The hostname on a Linux system is a kernel data structure parameter that can be utilized by tools when they require to know a system's "name". But they are not bound to use it. They can interrogate the system themselves, or provide this as a configurable item themselves. – slm Nov 02 '14 at 15:32
  • @goldilocks the guide sets `/etc/hostname` to `plato`, not `plato.example.com`. – Joó Ádám Nov 02 '14 at 17:00
  • @slm I know what `/etc/hosts` or the DNS is for, I’m just confused over the instructions on editing `/etc/hosts` when I set the hostname. Also, as I mentioned before, Postfix, for example, seems to require the change to function correctly (otherwise, in the log files [and who knows where else] it uses the original [compiled in?] hostname). – Joó Ádám Nov 02 '14 at 17:04
  • 1
    Sorry, that should have been FQDN not "hostname"; I'm in the bad habit of using them interchangeably. So you are right about `/etc/hosts` playing a role in setting the domain name (I've added a paragraph in my answer about this). – goldilocks Nov 02 '14 at 19:15

2 Answers2

3

As per slm's comment, the system hostname is maintained in memory by the kernel and the correct way for userland software to get it is via the gethostname() system call.

The default hostname is actually compiled into the (linux) kernel, so if you roll your own, you can set it that way. Distro kernels usually use localhost, I think (it can also be empty).

Because most people probably do not want to compile a kernel just to set the hostname, there's also a system call to set it, sethostname(); this is used by commands such as hostname. Typically during boot an init service will do this, using the value from /etc/hostname. However, this is not mandatory.

There are no restrictions on what you set the hostname to, but note that it is not the same as the fully qualified domain name (FQDN). The FQDN is the hostname plus your domain name. If you use the method below for setting the domain name, you probably should not have any dots in your hostname.

the role and correct setup of /etc/hosts in regard to setting the hostname (is it not just for resolving? what names and where to point?)

It is for DNS resolution, but Patrick has an explanation of how using it to set domain name works here. It's a lookup on the hostname, then a reverse lookup using the IP address; kind of circuitous and confusing compared to just setting it with hostname --fqdn but seemingly more commonplace.

Doing this does not stop domainname from returning (none), but maybe that is inconsequential.

goldilocks
  • 86,451
  • 30
  • 200
  • 258
  • If there are no restrictions on what I set the hostname to, how come only the first part is displayed in places like the Bash prompt or the Postfix log? This behaviour suggests it is not merely an opaque string. – Joó Ádám Nov 02 '14 at 17:09
  • Also, what is the difference between `hostname` and `hostname --fqdn`? If I set a simple name, like `plato` in `/etc/hostname`, where does the (so called) domain come from? – Joó Ádám Nov 02 '14 at 17:24
  • By "no restrictions" I meant the system won't care what it is. – goldilocks Nov 02 '14 at 17:40
  • According to `man hostname` you can set the FQDN that way too (e.g. at boot), which makes me realize how bad my habit of including it in `/etc/hostname` is. It does work though, I guess if you have not set the FQDN properly >_< Otherwise some software may end up with, e.g. `host.domain.domain`. – goldilocks Nov 02 '14 at 17:49
  • Also see [this](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=247734#27) discussion on Debian’s current practice of setting up the `hosts` file, and [this](https://lists.debian.org/debian-devel/2005/10/msg00559.html) one on the usage (or lack thereof) of the `localhost.localdomain` entry. – Joó Ádám Nov 04 '14 at 15:08
3

First the hostname of the system gets initialized during the time of system boot. Hostname is not compiled with kernel.

In Debian /etc/init.d/hostname.sh is run as part of startup scripts. This script reads the hostname from /etc/hostname and uses hostname command to set it.

The hostname of a system is to simply identify itself. Even if a system is not connected to the network the system needs a name for itself, mainly for logging and other purpose. For example all log messages have hostname on it. It is just to identify itself.

Hostname doesn't have any relationship with /etc/hosts it completely uses /etc/hostname.

The /etc/hostname should only contain the hostname part and not the FQDN.

Below can tell you the difference.

$ hostname
kannan

$ hostname -d
oongil.com

$ hostname -f
kannan.oongil.com

Now the FQDN part.

FQDN is mainly required if your system is part of a huge network and it needs to be identified itself uniquely.

/etc/hosts file contains lookup table for hostnames and FQDN of the system gets mapped here

127.0.1.1 kannan.oongil.com kannan

In Debian based systems 127.0.1.1 is mapped to the systems FQDN. And so when hostname -fqdn is invoked the system queries /etc/hosts to get this information.

The order of the content /etc/hosts is very much important.

In case if you dont want /etc/hosts to be queried for FQDN then you can change the order in /etc/nsswitch.conf.

My configuration is as below, here /etc/hosts is queried first then the DNS.

hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4

If you need to understand more about hostname and FQDN refer this Debian mailing thread.

Kannan Mohan
  • 3,191
  • 2
  • 17
  • 16