Is file /etc/hostname ever used for host name resolution?

When setting up a local Wi-Fi network in a small place from an ISP, such as Verizon, using a router and modem, the machines in the network are assigned private IP addresses.

In such a local Wi-Fi network, I can use ssh name@<hostname> to access an SSH server on another computer <hostname>, where <hostname> is the output of command hostname. Is the output of hostname resolved to a private IP address, by some DNS server (possibly on the router?)?

But I heard that the output of hostname is unrelated to host name resolution by DNS. Then why can I successfully run ssh name@<hostname>, where <hostname> is the output of command hostname?


Solution 1:

There are a couple of mechanisms that could be at work here.

Firstly, a system will often include its locally-configured hostname as a DHCP Client Identifier, and the router (which is also the DHCP and DNS server) will dynamically add a DNS record for that client ID matching the IP it gave out for that request.

The other likely case is that the system is advertizing it's locally-configured hostname using Multicast DNS Service Discovery (via Bonjour services on macOS, or the avahi daemon on Linux), and many modern distros include mDNS in their NSS lookup chain by default.

Solution 2:

Linux systems using nss-myhostname will resolve the currently configured local hostname (which on some systems comes from /etc/hostname), when you use getaddrinfo/gethostbyname. Systems without nss-myhostname usually have the local hostname listed in /etc/hosts, and resolve the local hostname that way.

If you specifically do dns lookups (e.g. with nslookup or dig), that bypasses nss and only uses dns, so nss-myhostname doesn't get a chance to influence the result.

If you want ssh@<hostname> to work from other devices, make sure hostname resolves in dns, or is listed in /etc/hosts on those devices.