3

When I configure a hostname on a computer, that I can look up like this:

$ hostname
example

and then ping, or ssh from another computer on the same LAN:

$ ssh example
user@example's password:

Where is my other computer looking up the address for this hostname? Does the former computer register it with the router somehow? Does the second computer just ask every ip address on the LAN "Are you example?"?

The second computer seems to know the ip address that corresponds to example even if I unplug example. Is that info cached in the second computer? Or does hostnamectl somehow register this host name with the router.

And if I plug these computers into another network now, will they still be able to find one another with these hostnames?

How does it all work?

3
  • What OSes are your computers?
    – Déjà vu
    Aug 29 '20 at 9:01
  • ubuntu and raspbian
    – Alex028502
    Aug 29 '20 at 9:01
  • I'd say that this is mDNS at work Aug 29 '20 at 9:06
4

There are several methods:

  • Zero-configuration networking protocols using IP multicast (RFC 1112) sends UDP packets to special addresses that many devices on the LAN treat as the message was sent to them. Therefore, there's no need to ask every IP address on the network. They also use reserved MAC addreses for the packets to get forwarded to every port on a switch.

    • Multicast DNS (mDNS, RFC 6762)

      • UDP port 5353
      • IPv4 224.0.0.251 / MAC 01:00:5E:00:00:FB
      • IPv6 ff02::fb / MAC 33:33:00:00:00:FB
    • Link-Local Multicast Name Resolution (LLMNR, RFC 4795)

      • UDP port 5355
      • IPv4 224.0.0.252, MAC 01:00:5E:00:00:FC
      • IPv6 ff02::1:3, MAC 33-33-00-01-00-03
  • The Dynamic Host Configuration Protocol (DHCP) might be involved:

    • Connection-specific DNS Suffix, DHCP option 15 (RFC 2132, 3.17) gives the client a domain name that will be added as a suffix to these non-FQDN queries. Therefore, your example may become example.example.com that is used for the DNS query.

    • It's possible to automatically update the DNS with DHCP Client FQDN Option (RFC 4702).

  • NetBIOS. See e.g. Robert L Bogue: How NetBIOS name resolution really works.

  • Peer Name Resolution Protocol (PNRP). A Microsoft technology, requires IPv6.

1

On the client (from) computer, run "grep hosts /etc/nsswitch.conf"

This will tell you the location and order in which the system will search for hostnames. Usually, you will see something like:

hosts: files dns

This would imply it's looking at the /etc/hosts file and then querying dns. If it instead were to say:

hosts: dns files

Then it would be looking at DNS and then the local /etc/hosts file.

There are other possibilities for this entry, so take a look and see what you can find!

3
  • hosts: files mdns4_minimal [NOTFOUND=return] dns - great ; thanks a lot. so what I am asking out is MDNS, as mentioned by a few people above
    – Alex028502
    Aug 29 '20 at 18:59
  • 1
    It works similar to ARP. ie. Roughly... your client asks the local network "who is example?" and the host with the name example responds with it's IP address (ie. 192.168.1.100) and then your host application (ie. ssh) connects to that address. The hostname it's self isn't really stored in a central location so much as it's provided when requested by the host that has that name. Each mDNS client has a cache so it doesn't have to keep asking the same question over and over again. This cache is likely in memory, but that could vary by implementation.
    – mikem
    Aug 29 '20 at 19:12
  • 1
    Perhaps this will help: en.wikipedia.org/wiki/Multicast_DNS or maybe multicastdns.org
    – mikem
    Aug 29 '20 at 19:13

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.