15

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.

  • DNS A or AAA records point to the same IP address for multiple domains. The hostname of the machine is not necessarily important. – RubberStamp Feb 11 at 23:03
  • (1) Is the output of hostname ever used in resolving hostname to IP address? (2) A means IPv4 address, AAAA IPv6. What is AAA? – Tim Feb 11 at 23:15
  • mDNS services, such as avahi depend on the machine hostname. In that case, hostname.local would resolve to the local IP address of the machine. I see someone wrote an answer which is an expansion of my first comment... too simultaneous for my browser notifications. – RubberStamp Feb 11 at 23:25
  • AAA is a 'A' short typo – RubberStamp Feb 11 at 23:33
  • Virtual Host has nothing to do with hostname. It purely depends on web server behavior. The server responses differently to different Host field of HTTP request header. I don't think typical web server would do anything related to DNS or hostname to offer virtual host functionality. – 炸鱼薯条德里克 Feb 12 at 10:23
25

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.

  • Thanks. (1) "the DNS setup would usually have 192.0.2.9 point back to the name orange.example.org" Doesn't resolving get IP address for a given host name, why the opposite? (2) Is the output of hostname ever used in resolving hostname to IP address? – Tim Feb 11 at 23:14
  • (2) ... When set up a local wifi network in your home from an ISP using a router and modem, the machines in the network can be identified by the outputs of their hostname commands, correct? Does the router act like a DNS to resolve the outputs of hostname to private IP addresses in the network? – Tim Feb 11 at 23:20
  • 1
    as for (1), there's also reverse lookups that take the IP address and return a name. (For a system with a single name and a single address, you'd want to have both resolve to the other, just for clarity's sake) – ilkkachu Feb 11 at 23:30
  • 2
    Small networks may be using mDNS. For better or worse, it lets system owners choose their own hostnames, accessible by other systems on the local network, without the need to register with a DNS server. – Mark Plotnick Feb 12 at 1:20
  • 1
    In many home networks, the local router acts as a DNS resolver. Hosts send their hostname when they request an address using DHCP, and the router remembers that name and resolves it for all hosts on the network. As Mark and RubberStamp mention, there’s also Bonjour (implemented in Avahi) which allows for service discovery on the LAN (printers in particular). Note too that hosts can have multiple IP addresses, and hostnames are now namespaced so processes can see different hostnames on the same host. – Stephen Kitt Feb 12 at 6:35
7

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

No.

[I'll completely ignore that anybody may give your host any name they like by having a DNS record pointing to its address, or that you can have more than one alias for it in /etc/hosts, or any philosophical considerations about what a name really is]

On a linux system, the hostname is simply a per-process resource (the "UTS namespace") that is inherited by its children by default, but could be disjoined from with unshare(2) or clone(2) by using the CLONE_NEWUTS flag. You should look into the namespaces(7) manpage.

Just like pids, port numbers, mount points, etc the hostname is no longer a global identifier, and it could be virtualized just the same as the address space (virtual memory) or the file descriptor table of a process.

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?

Virtual hosting is something completely different, and it's usually done by configuring a wildcard DNS record (eg. *.foo.com => 13.13.13.13) with a web server running on 13.13.13.13 and serving different directories / resources based on value of the http Host: header[1] (which is highly configurable; most web servers are able to serve different resources based on any http header, not just Host:). In all this, the OS of the system the web server is running on plays no part.

[1] or on the value of the server name indication in case of https, which allows the same webserver to use different certificates for different virtual hosts.

  • I have to disagree with you on this. For virtual hosting, we generally have a specific set of cname records pointing to the single host. We do not use a wildcard DNS record as we have many systems within the domain needing virtual hosts. Excluding UTS namespaces (how widely used is this anyway?), you have hostnamectl reporting a single hostname for a given server. DNS and hostnamectl are orthogonal and have little to do with each other. – doneal24 Feb 12 at 20:53
  • 1
    @DougO'Neal What are you disagreeing with? That wildcard DNS records are used in the way I describe it? They're wildly used like that (sorry for the pun), whether you like it or not. That the uts info is virtualized per-process in linux? That's simply the way it is. As to the idea that namespaces are only used / to be used by canned containerization/virtualization solutions (as suggested in the other answer), sorry but that is "only girls play with dolls" or "manual transmissions are only used in heavy trucks" quality ;-) – Uncle Billy Feb 13 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.