Linux – Does a computer system (Linux) only have one host name

hostnamelinux

hostname is used to display the system's DNS name, and to display or set its hostname or NIS domain name.

Does a computer system (Linux) only have one host name?

In virtual hosting, several host names can be resolved to different root directories in a web server. If a computer system (Linux) can only have one host name, how is virtual hosting possible?

Thanks.

Best Answer

Yes, and no. The are two distinct things called hostnames.

The "internal" hostname is basically a string kept by the kernel. This is the one returned by the hostname command (or the gethostname() call) and it's unique within a system(*).

It's mostly used when a program wants to output some identifier for the system it's running on. E.g. \h in Bash's PS1 expands to the hostname. Similarly, syslog-style logfiles also include the hostname on log entries.

(* Though as Stephen Kitt comments, namespaces can be used to show different hostnames to processes on the same system. That's mostly used for containers, which try to act like they're distinct systems.)

Then there's also DNS names that are used by other systems to look up the IP address of another. There might be more than one DNS name that point to the same IP address, and so the same host.

The internal hostname and the DNS names don't need to be the same. Suppose someone has a webserver they've decided to call orange(*), with the IP address 192.0.2.9 . It could serve two different domains and the DNS would be set up to have www.example.org and www.example.com both point to 192.0.2.9, while the internal hostname of the system might be orange.example.org or just orange. In that case, the DNS setup would usually also have a reverse lookup on 192.0.2.9 point back to the name orange.example.org, but there's nothing to force that.

(* because they like to name their servers after fruit. Someone might use webserver1 or such, but the point is that it doesn't need to be named after one of the actual domains.)

In addition to that, virtual hosting requires that the browser tell the web server the name of the site it tried to access. Otherwise the server would not know which virtual site the client tried to reach. HTTP has the Host header for that.


What muddies the distinction between a DNS name and the internal hostname is the mDNS protocol (implemented e.g. by the avahi daemon) and other discovery protocols. mDNS makes it possible for hosts to query all other hosts on the same network for name information, and to make their own hostnames visible on other hosts without explicitly setting them up in DNS.

Related Question